Site icon 时鹏亮的Blog

Elasticsearch Function Score Query 分数差异

请知悉:本文最近一次更新为 5年 前,文中内容可能已经过时。

最近接手搜索引擎后,精简调优代码后,测试同学发现个问题,就是最终_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 unlike
must the score of the query will be ignored. Filter clauses are executed
in filter context, meaning that scoring is ignored
and clauses are considered for caching.
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 the bool 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 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


如您从本文得到了有价值的信息或帮助,请考虑扫描文末二维码捐赠和鼓励。

尊重他人劳动成果。转载请务必附上原文链接,我将感激不尽。


与《Elasticsearch Function Score Query 分数差异》相关的博文:

Exit mobile version