【问题标题】:How to compound terms query and bool query together in Elasticsearch如何在 Elasticsearch 中将术语查询和布尔查询组合在一起
【发布时间】:2019-02-27 00:51:34
【问题描述】:

最初,我会在布尔查询中添加filter。但是,当我转向terms filter 时,documents 表示现在应该将其替换为terms query。所以我理解这一点,因为我们需要用terms querybool query 构造一个复合查询。如果我是正确的,我应该如何编写查询?

注意:我使用的是 elasticsearch 的 Python API。

【问题讨论】:

    标签: elasticsearch elasticsearch-dsl elasticsearch-dsl-py


    【解决方案1】:

    现在有两种类型的上下文:query context and filter context。查询上下文中的任何查询子句都会影响匹配文档的分数,即文档与查询的匹配程度,而过滤器上下文中的任何查询子句确定文档是否匹配并且不影响分数。

    所以在下面的查询中我标记了两个上下文:

    {
      "query": { 
        "bool": { 
          "must": [
            { "match": { "field1":   "terms1"}},              <------query context
          ],
          "filter": [ 
            { "terms":  { "someId": [3242, 12343, 1234] }},   <-------inside filter block every query clause will act as filter (filter context)
          ]
        }
      }
    }
    

    因此,要将术语查询用作过滤器,您需要执行以下步骤:

    1. 创建一个布尔查询说boolQuery
    2. 创建一个术语查询坐在termsQuery
    3. termsQuery 添加到boolQuery 的过滤器中。
    4. boolQuery 设置为查询。

    翻译如下:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "field1": ["terms1", "term2", "terms3"]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    • 听起来我完全误解了文件。
    • 不用担心。人们在这里消除疑虑和帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多