How to have Range and Match query in one elastic search query using python?

别说谁变了你拦得住时间么 提交于 2019-12-02 10:19:11

问题


I have to find the matching documents which have the string, for example: "sky", within some "key" range. When I write separate match and range query, I get the output from the ES but it throws an exception when merged together.

range query:

res = es.search(index="dummy",
                body={"from":0, "size":0,"query": {"range":{"key":{"gte":"1000"}}}})

match query:

res = es.search(index="dummy",
                body={"from":0, "size":0,"query": {"match":{"word":"sky"}}})

combined query:

res = es.search(index="dummy",
                body={
                  "from":0,
                  "size":0,
                  "query": {
                    "range":{
                      "key":{"gte":"1000"}
                    }
                  },
                  "match":{"word":"sky"}
                })

The combined query when executed throws the error:

raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) elasticsearch.exceptions.RequestError: TransportError(400, u'parsing_exception', u'Unknown key for a START_OBJECT in [match].')

What is the correct way of merging both the queries?


回答1:


You need to do it like this using a bool/must query

res = es.search(index="dummy", body={
  "from": 0,
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "key": {
              "gte": "1000"
            }
          }
        },
        {
          "match": {
            "word": "sky"
          }
        }
      ]
    }
  }
})


来源:https://stackoverflow.com/questions/49556729/how-to-have-range-and-match-query-in-one-elastic-search-query-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!