首页 > 解决方案 > ElasticSearch bool 查询结合 should 和 filter

问题描述

在elasticsearch你知道为什么

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must": [
              conditions1
            ]
          }
        },
        {
          "bool": {
            "must": [
              conditions2
            ]
          }
        }
      ],
      "filter": [
        conditions3
      ]
    }
  }
}

如果条件 3 为真,即使条件 2 和条件 1 为假,也总能找到文档

标签: elasticsearch

解决方案


根据https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html上的文档:

如果 bool 查询在查询上下文中并且有一个 must 或 filter 子句,那么即使所有 should 查询都不匹配,文档也会匹配 bool 查询。在这种情况下,这些子句仅用于影响分数。如果 bool 查询是一个过滤上下文或者既没有 must 也没有 filter,那么至少一个 should 查询必须匹配一个文档才能匹配 bool 查询。此行为可以通过设置 minimum_should_match 参数来显式控制。


推荐阅读