Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
249 views
in Technique[技术] by (71.8m points)

How to update Elasticsearch 1.x to 5.x queries?

I have a query in Elasticsearch 1.x like this:

{"filter": {"and": {"filters": [{"exists": {"field": "body"}}, {"query": {"term": {"accept": "true"}}},
                                      {"exists": {"field": "thumbnail"}}, {"query": {"terms": {
        "content": ["577f6ca06dd5340a97e89923"]}}}, {"range": {
        "date": {"lt": "2018-01-05T04:07:48.901933", "gte": "1963-04-04T04:07:48.901933"}}}, 
                                      {"query": {"terms": {
        "agency": ["577ff7176dd5340a97e899b7"]}}}, {"query": {"terms": {
        "subject": ["578c6c7f6dd5345f3db18e7b"]}}}, {"query": {
        "terms": {"geographic": ["577f78fe6dd5340a97e89948"]}}}]}},
       "fields": ["_id", "link", "title"], "from": 0,
       "sort": {"date": {"order": "desc"}}, "size": 30}

It works in 1.x version but in 5.x version rises an error:

elasticsearch.exceptions.RequestError: TransportError(400, u'parsing_exception', u'Unknown key for a START_OBJECT in [filter].')

What should I do?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this query, it should work:

{
  "query": {
    "bool": {
      "filter": [
        {
          "exists": {
            "field": "body"
          }
        },
        {
          "term": {
            "accept": "true"
          }
        },
        {
          "exists": {
            "field": "thumbnail"
          }
        },
        {
          "terms": {
            "content": [
              "577f6ca06dd5340a97e89923"
            ]
          }
        },
        {
          "range": {
            "date": {
              "lt": "2018-01-05T04:07:48.901933",
              "gte": "1963-04-04T04:07:48.901933"
            }
          }
        },
        {
          "terms": {
            "agency": [
              "577ff7176dd5340a97e899b7"
            ]
          }
        },
        {
          "terms": {
            "subject": [
              "578c6c7f6dd5345f3db18e7b"
            ]
          }
        },
        {
          "terms": {
            "geographic": [
              "577f78fe6dd5340a97e89948"
            ]
          }
        }
      ]
    }
  },
  "_source": [
    "_id",
    "link",
    "title"
  ],
  "from": 0,
  "sort": {
    "date": {
      "order": "desc"
    }
  },
  "size": 30
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...