Elasticsearch bool query小结

栏目: 后端 · 发布时间: 7年前

内容简介:最近有一个线上的es查询问题,最后确定在使用其中找到相关的查询语句:

最近有一个线上的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] | Elasticshould 的解释:

The clause (query) should appear in the matching document. If the bool query is in aquery 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 the bool query is afilter 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 theminimum_should_match parameter.

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

filter
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 子句的交集,符合预期的结果,并且有相应的匹配程度分值。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS商业网站布局之道

CSS商业网站布局之道

朱印宏 / 清华大学出版社 / 2007-1 / 75.00元

本书是一本CSS技术专著。 主要从布局角度全面、系统和深入地讲解CSS在标准网站布局之中的应用。很多读者经过初步的学习之后就能够使用CSS设计出一些漂亮的网页样式,于是便乐在其中,踌躇满志,这是好事,但千万不要自我陶醉,因为你还未领略CSS的博大精深。用CSS容易,难的是全部都用CSS。CSS的精髓是布局,而不是样式,布局是需要缜密的逻辑思维和系统设计的,而样式只需要简单地编写代码或复制即可。本书......一起来看看 《CSS商业网站布局之道》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具