【问题标题】:How to have Range and Match query in one elastic search query using python?如何使用 python 在一个弹性搜索查询中进行范围和匹配查询?
【发布时间】:2018-09-08 10:48:46
【问题描述】:

我必须在某个“键”范围内找到具有该字符串的匹配文档,例如:“sky”。当我编写单独的匹配和范围查询时,我从 ES 获取输出,但合并在一起时会引发异常。

范围查询:

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

匹配查询:

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

组合查询:

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

组合查询在执行时抛出错误:

引发 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].')

合并两个查询的正确方法是什么?

【问题讨论】:

    标签: python elasticsearch pyelasticsearch


    【解决方案1】:

    您需要像这样使用bool/must 查询

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

    【讨论】:

      猜你喜欢
      • 2015-05-06
      • 2020-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2017-11-09
      相关资源
      最近更新 更多