最近接手搜索引擎后,精简调优代码后,测试同学发现个问题,就是最终_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分数了。分数至此,恢复了最初的状态。

翻阅了下官方的文档,找到了说明,至此问题明了了。

表格说明摘录如下:

OccurDescription
mustThe clause (query) must appear in matching documents and will
contribute to the score.
filterThe 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.
shouldThe 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_notThe 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

Related Posts: Elasticsearch Function Score Query 分数差异 :

avatar