理解二次評分
二次評分是指重新計算查詢返回文檔中指定個數文檔的得分,es會截取查詢返回的前N個,並使用預定義的二次評分方法來重新計算他們的得分
小結
- 有時候,我們需要顯示查詢結果,並且使得頁面上靠前文檔的順序能受到一些額外的規則控制,但遺憾的是,我們並不能通過二次評分來實現,也許有些讀者會想到window-size參數,然而實際上這個參數與返回列表中靠前文檔並無關系,他只是制定了每個分片應該返回的文檔數,而且window_size不能小於頁面大小
- 二次評分功能並不能與排序一起使用,這是因為排序發生在二次評分之前,所以排序沒有考慮后續新計算出來的文檔得分
- 如果search_type為scan或count,二次評分不會被執行
二次評分參數配置
在resource對象中,必須配置下面的參數:
- window_size 窗口大小,默認值是from和size參數值之和,它指定了每個分片上參與二次評分的文檔個數
- query_weight 查詢權重,默認值是1,原始查詢得分與二次評分的得分相加之前將乘以改值
- rescore_query_weight 二次評分查詢的權重值,默認值是1,二次評分查詢得分在與原始查詢得分相加之前,乘以該值
- rescore_mode 二次評分模式,默認為total,可用的選項有total、max、min、avg和mutiply
- total 得分是兩種查詢之he
- max 兩種查詢中的最大值
- min 兩種查詢中的最小值
- avg 兩種查詢的平均值
- multiply 兩種查詢的乘積
以下是官網手冊(v5.1)
Rescoring can help to improve precision by reordering just the top (eg 100 - 500) documents returned by the query
and post_filter
phases, using a secondary (usually more costly) algorithm, instead of applying the costly algorithm to all documents in the index.
A rescore
request is executed on each shard before it returns its results to be sorted by the node handling the overall search request.
Currently the rescore API has only one implementation: the query rescorer, which uses a query to tweak the scoring. In the future, alternative rescorers may be made available, for example, a pair-wise rescorer.

the rescore
phase is not executed when sort
is used.

when exposing pagination to your users, you should not change window_size
as you step through each page (by passing different from
values) since that can alter the top hits causing results to confusingly shift as the user steps through pages.
Query rescoreredit
The query rescorer executes a second query only on the Top-K results returned by the query
and post_filter
phases. The number of docs which will be examined on each shard can be controlled by the window_size
parameter, which defaults to from
and size
.
By default the scores from the original query and the rescore query are combined linearly to produce the final _score
for each document. The relative importance of the original query and of the rescore query can be controlled with the query_weight
and rescore_query_weight
respectively. Both default to 1
.
For example:
curl -s -XPOST 'localhost:9200/_search' -d '{ "query" : { "match" : { "field1" : { "operator" : "or", "query" : "the quick brown", "type" : "boolean" } } }, "rescore" : { "window_size" : 50, "query" : { "rescore_query" : { "match" : { "field1" : { "query" : "the quick brown", "type" : "phrase", "slop" : 2 } } }, "query_weight" : 0.7, "rescore_query_weight" : 1.2 } } } '
The way the scores are combined can be controlled with the score_mode
:
Score Mode | Description |
---|---|
|
Add the original score and the rescore query score. The default. |
|
Multiply the original score by the rescore query score. Useful for |
|
Average the original score and the rescore query score. |
|
Take the max of original score and the rescore query score. |
|
Take the min of the original score and the rescore query score. |
Multiple Rescoresedit
It is also possible to execute multiple rescores in sequence:
curl -s -XPOST 'localhost:9200/_search' -d '{ "query" : { "match" : { "field1" : { "operator" : "or", "query" : "the quick brown", "type" : "boolean" } } }, "rescore" : [ { "window_size" : 100, "query" : { "rescore_query" : { "match" : { "field1" : { "query" : "the quick brown", "type" : "phrase", "slop" : 2 } } }, "query_weight" : 0.7, "rescore_query_weight" : 1.2 } }, { "window_size" : 10, "query" : { "score_mode": "multiply", "rescore_query" : { "function_score" : { "script_score": { "script": { "lang": "painless", "inline": "Math.log10(doc['numeric'].value + 2)" } } } } } } ] } '
The first one gets the results of the query then the second one gets the results of the first, etc. The second rescore will "see" the sorting done by the first rescore so it is possible to use a large window on the first rescore to pull documents into a smaller window for the second rescore.