Elasticsearch bool query小结

背景

最近有一个线上的es查询问题,最后确定在使用bool query多条件组合查询时出现should子句查询失效,于是查找资料来确定问题所在。

其中Elasticsearch: 5.5.0

问题

找到相关的查询语句:

"query": {
    "bool": {     // bool query 查询
      "should": [ // should子句
        {
          "match_phrase": {
            "name": {
              "query": "星起",
              "boost": 30,
              "slop": 5
            }
          }
        }
      ],
      "filter": { // #filter子句
        "bool": {
          "must": [
            {
              "terms": {
                "round": ["A轮"]
              }
            },
          ]
        }
      }
    }
  }

问题在于:使用 bool query组合查询时,shouldfilter 组合查询的结果只匹配了filter子句,并不匹配should子句,达不到shouldfilter取交集的预期。

解决方法

翻了一下官方文档:Bool Query | Elasticsearch Reference [5.5] | Elastic
should的解释:

The clause (query) should appear in the matching document. If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score. If thebool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.

大体的意思就是:should子句是在匹配文档中使用的,如果bool查询是在query上下文,并且有must 或者 filter子句时不管should查询是否匹配,都不影响must或者filter子句的查询。这些子句只是影响查询的score而已。如果bool查询是在filter上下文 或者 既没有must也没有filter则应至少一个should查询必须匹配bool查询。也可以显式设置minimum_should_match这个参数来解决。
从官方文档可以看出,有2种方式可以在bool query取各数据的交集:

  1. 将查询的条件,移到filter上下文里
  2. 使用设置minimum_should_match参数
解决方案

用上面提到2种方式,我们分别尝试一下是否可以达到预期目标。

方案一

使用filter上下文:

"query": {
    "bool": {
      "filter": { // filter上下文
        "bool": {
          "should": [ // should子句
            {
              "match_phrase": {
                "name": {
                  "query": "星起",
                  "boost": 30,
                  "slop": 5
                }
              }
            }
          ],
          "filter": { // filter子句
            "bool": {
              "must": [
                {
                  "terms": {
                    "round": ["A轮"]
                  }
                }
              ]
            }
          }
        }
      }
    }
  }

测试结果如下:

"hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "index_name",
        "_type": "hub/product",
        "_id": "id",
        "_score": 0.0, // filter下分值为0.0
        "_source": {
          "round": "A轮",
          "name": "星起Starup",
          "created_at": "2015-12-25T22:20:36.210+08:00",
          "sector_name": "企业服务"
        },
        "highlight": {
          "name": ["<em>星起</em>Starup"]
        },
        "sort": []
      }
    ]
  }

测试结果满足shouldfilter子句交集,需要注意结果的分值为0.0, 没有对查询结果匹配程度打分。

方案二

使用minimum_should_match,至少匹配一项should子句,可以如下设置:

"query": {
    "bool": {     
      "should": [ // should 子句
        {
          "match_phrase": {
            "name": {
              "query": "星起",
              "boost": 30,
              "slop": 5
            }
          }
        }
      ],
      "minimum_should_match": 1, // 最少匹配一项should中条件子句
      "filter": { // filter子句
        "bool": {
          "must": [
            {
              "terms": {
                "round": ["A轮"]
              }
            },
          ]
        }
      }
    }
  }

测试结果如下:

"hits": {
    "total": 1,
    "max_score": null,
    "hits": [
      {
        "_index": "index_name",
        "_type": "hub/product",
        "_id": "id",
        "_score": 757.66394,
        "_source": {
          "round": "A轮",
          "name": "星起Starup",
          "created_at": "2015-12-25T22:20:36.210+08:00",
          "sector_name": "企业服务"
        },
        "highlight": {
          "name": ["<em>星起</em>Starup"]
        },
        "sort": [757.66394]
      }
    ]
  }

数据为shouldfilter子句的交集,符合预期的结果,并且有相应的匹配程度分值。

总结

从上面2种解决方案可以看出,Elasticsearch在查询上还是比较灵活,平时除了需要熟悉官方的文档,还要结合业务的需求,才能找到正确解决问题的方法。

转自:https://blog.lovecoding.org/2018/12/18/es-bool-query/

参考
  1. Bool Query | Elasticsearch Reference [5.5] | Elastic
  2. Boolean Operations and Filters in the Bool Query in Elasticsearch | Elastic
  3. ElasticSearch查询 - 博客园
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 声明:本文转自我的个人博客,有兴趣的可以查看原文。转发请注明来源。 这是一篇科普文。 1. 背景 Elastics...
    此星爷非彼星爷阅读 5,449评论 0 14
  • Neil Zhu,简书ID Not_GOD,University AI 创始人 & Chief Scientist...
    朱小虎XiaohuZhu阅读 14,494评论 0 5
  • pyspark.sql模块 模块上下文 Spark SQL和DataFrames的重要类: pyspark.sql...
    mpro阅读 13,193评论 0 13
  • Elasticsearch安装和配置问题 启动时候报错的问题 max file descriptors [4096...
    AlienPaul阅读 13,015评论 0 1
  • 第六天 一、今天,令到我开心的事情是什么?与我的型号,侧翼,健康和动态什么关系?今天这次,我与以往不同的处理方式是...
    林金琼阅读 1,310评论 1 2

友情链接更多精彩内容