最近接手搜索引擎后,精简调优代码后,测试同学发现个问题,就是最终_score的计算和最初不同了,多了2分。
经过explain后,查看发现的新增的两个字段造成的分值变化,默认的boost是1,两个自然是多了2。
那么这个分是怎么来的呢?经过调试发现,我增加的筛选是类似如下的方式:
$this->query['function_score']['query']['bool']['must'][] = [
'term' => [ 'hdsd' => 1 ]
];
而类比之前的代码,改为
$this->query['function_score']['query']['bool']['filter'][] = [
'term' => [ 'hdsd' => 1 ]
];
之后,就不会累加默认的boost分数了。分数至此,恢复了最初的状态。
翻阅了下官方的文档,找到了说明,至此问题明了了。
表格说明摘录如下:
Occur | Description |
---|---|
must |
The clause (query) must appear in matching documents and will contribute to the score. |
filter |
The clause (query) must appear in matching documents. However unlikemust the score of the query will be ignored. Filter clauses are executedin filter context, meaning that scoring is ignored and clauses are considered for caching. |
should |
The clause (query) should appear in the matching document. If thebool query is in a query context and has a must orfilter clause then a document will match the bool query even if none of theshould queries match. In this case these clauses are only used to influencethe score. If the bool query is a filter contextor has neither must or filter then at least one of the should queriesmust match a document for it to match the bool query. This behavior may beexplicitly controlled by setting the minimum_should_match parameter. |
must_not |
The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned. |
以上文档整理自: Bool Query
如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。
如本文对您有用,捐赠和留言 将是对我最好的支持~(捐赠可转为站内积分)
如愿意,请向朋友推荐本站,谢谢。
尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。