代码之家  ›  专栏  ›  技术社区  ›  Bs He

如何在Elasticsearch中将术语查询和布尔查询组合在一起

  •  0
  • Bs He  · 技术社区  · 6 年前

    最初,我会添加一个 filter 在布尔查询中。然而,当我转向 terms filter 这个 documents 指示应替换为 terms query 现在我的理解是,我们需要用这两种语言构造一个复合查询 术语查询 bool query . 如果我是正确的,我应该如何编写查询?

    注意:我使用elasticsearch的Python API。

    0 回复  |  直到 6 年前
        1
  •  7
  •   Nishant    6 年前

    现在有两种类型的上下文: 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. 创建术语查询sat termsQuery
    3. 添加 termsQuery公司 过滤
    4. 设置 布尔查询 作为查询。

    这意味着:

    {
      "query": {
        "bool": {
          "filter": [
            {
              "terms": {
                "field1": ["terms1", "term2", "terms3"]
              }
            }
          ]
        }
      }
    }
    
    推荐文章