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

2019-09-20 17:52发布

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?

1条回答
该账号已被封号
2楼-- · 2019-09-20 18:30

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
}
查看更多
登录 后发表回答