|
| 1 | +--- |
| 2 | +mapped_pages: |
| 3 | + - https://www.elastic.co/guide/en/elasticsearch/reference/current/rescore-search-results.html |
| 4 | +applies_to: |
| 5 | + stack: all |
| 6 | +--- |
| 7 | + |
| 8 | +# Rescore search results [rescore-search-results] |
| 9 | + |
| 10 | +Rescoring can help to improve precision by reordering just the top |
| 11 | +(e.g. 100 - 500) documents returned by initial retrieval phase |
| 12 | +(query, knn search) by using a secondary (usually more costly) algorithm, |
| 13 | +instead of applying the costly algorithm to all documents in the index. |
| 14 | + |
| 15 | +A `rescore` request is executed on each shard before it returns its results |
| 16 | +to be sorted by the node handling the overall search request. |
| 17 | + |
| 18 | +The rescore API has 3 options: |
| 19 | + |
| 20 | +1. `query` rescorer that executes a provided `rescore_query` on the top documents |
| 21 | +2. `script` rescorer that uses a script to modify the scores of the top documents |
| 22 | +3. `learning_to_rank` rescorer that uses an LTR model to re-rank the top documents |
| 23 | + |
| 24 | +All rescores have the `window_size` parameter that controls how many top |
| 25 | +documents will be considered for rescoring. The default is 10. |
| 26 | + |
| 27 | +::::{note} |
| 28 | +When implementing pagination, keep the `window_size` consistent across pages. |
| 29 | +Changing it while advancing through results (by using different `from` values) |
| 30 | +can cause the top hits to shift, leading to a confusing user experience. |
| 31 | +:::: |
| 32 | + |
| 33 | +### Query Rescorer [query-rescorer] |
| 34 | + |
| 35 | +The query rescorer executes a second query only on the top documents returned |
| 36 | +from the previous phase. The number of docs which is examined on each shard |
| 37 | +can be controlled by the `window_size` parameter. |
| 38 | + |
| 39 | +By default, the scores from the original query and the rescore query are combined |
| 40 | +linearly to produce the final `_score` for each document. |
| 41 | +The relative importance of the original query and of the rescore query can be |
| 42 | +controlled with the `query_weight` and `rescore_query_weight` respectively. |
| 43 | +Both default to `1`. |
| 44 | + |
| 45 | +For example: |
| 46 | + |
| 47 | +```console |
| 48 | +POST /_search |
| 49 | +{ |
| 50 | + "query" : { |
| 51 | + "match" : { |
| 52 | + "message" : { |
| 53 | + "operator" : "or", |
| 54 | + "query" : "the quick brown" |
| 55 | + } |
| 56 | + } |
| 57 | + }, |
| 58 | + "rescore" : { |
| 59 | + "window_size" : 10, |
| 60 | + "query" : { |
| 61 | + "rescore_query" : { |
| 62 | + "match_phrase" : { |
| 63 | + "message" : { |
| 64 | + "query" : "the quick brown", |
| 65 | + "slop" : 2 |
| 66 | + } |
| 67 | + } |
| 68 | + }, |
| 69 | + "query_weight" : 0.7, |
| 70 | + "rescore_query_weight" : 1.2 |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +::::{note} |
| 77 | +An error will be thrown if an explicit [`sort`](/reference/elasticsearch/rest-apis/sort-search-results.md) |
| 78 | +(other than `_score` in descending order) is provided with a `rescore` query. |
| 79 | +:::: |
| 80 | + |
| 81 | + |
| 82 | +The way the scores are combined can be controlled with the `score_mode`: |
| 83 | + |
| 84 | +| Score Mode | Description | |
| 85 | +| --- |-------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |
| 86 | +| `total` | Add the original score and the rescore query score. The default. | |
| 87 | +| `multiply` | Multiply the original score by the rescore query score. Useful for [`function query`](/reference/query-languages/query-dsl/query-dsl-function-score-query.md) rescores. | |
| 88 | +| `avg` | Average the original score and the rescore query score. | |
| 89 | +| `max` | Take the max of original score and the rescore query score. | |
| 90 | +| `min` | Take the min of the original score and the rescore query score. | |
| 91 | + |
| 92 | +### Script rescorer [script-rescorer] |
| 93 | + |
| 94 | +`script` rescorer uses a script to rescore the top documents returned |
| 95 | +from the previous phase. The script has access to the original score as well |
| 96 | +as values of document fields. |
| 97 | + |
| 98 | +For example, the following script rescores documents based on the document's |
| 99 | +original query score and the value of field `num_likes`: |
| 100 | + |
| 101 | +```console |
| 102 | +POST /_search |
| 103 | +{ |
| 104 | + "query" : { |
| 105 | + "match" : { |
| 106 | + "message" : { |
| 107 | + "operator" : "or", |
| 108 | + "query" : "the quick brown" |
| 109 | + } |
| 110 | + } |
| 111 | + }, |
| 112 | + "rescore" : { |
| 113 | + "window_size" : 10, |
| 114 | + "script" : { |
| 115 | + "script" : { |
| 116 | + "source": "doc['num_likes'].value * params.multiplier + _score", |
| 117 | + "parameters": { |
| 118 | + "multiplier": 0.1 |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +### Learning to rank rescorer [learning-to-rank-rescorer] |
| 127 | +`learning_to_rank` uses an LTR model to rescore the top documents. You must |
| 128 | +provide the `model_id` of a deployed model, as well as any named parameters |
| 129 | +required by the query templates for features used by the model. |
| 130 | + |
| 131 | +```console |
| 132 | +GET my-index/_search |
| 133 | +{ |
| 134 | + "query": { <1> |
| 135 | + "multi_match": { |
| 136 | + "fields": ["title", "content"], |
| 137 | + "query": "the quick brown fox" |
| 138 | + } |
| 139 | + }, |
| 140 | + "rescore": { |
| 141 | + "learning_to_rank": { |
| 142 | + "model_id": "ltr-model", |
| 143 | + "params": { |
| 144 | + "query_text": "the quick brown fox" |
| 145 | + } |
| 146 | + }, |
| 147 | + "window_size": 100 |
| 148 | + } |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +### Multiple rescores [multiple-rescores] |
| 153 | + |
| 154 | +You can apply multiple rescoring operations in sequence. The first rescorer |
| 155 | +works on the top documents from the initial retrieval phase, while the second |
| 156 | +rescorer works on the output of the first rescorer, and so on. A common practice |
| 157 | +is to use a larger window for the first rescorer and smaller windows for more |
| 158 | +expensive subsequent rescorers. |
| 159 | + |
| 160 | +```console |
| 161 | +POST /_search |
| 162 | +{ |
| 163 | + "query": { |
| 164 | + "match": { |
| 165 | + "message": { |
| 166 | + "operator": "or", |
| 167 | + "query": "the quick brown" |
| 168 | + } |
| 169 | + } |
| 170 | + }, |
| 171 | + "rescore": [ |
| 172 | + { |
| 173 | + "window_size": 10, |
| 174 | + "query": { |
| 175 | + "rescore_query": { |
| 176 | + "match_phrase": { |
| 177 | + "message": { |
| 178 | + "query": "the quick brown", |
| 179 | + "slop": 2 |
| 180 | + } |
| 181 | + } |
| 182 | + }, |
| 183 | + "query_weight": 0.7, |
| 184 | + "rescore_query_weight": 1.2 |
| 185 | + } |
| 186 | + }, |
| 187 | + { |
| 188 | + "window_size": 5, |
| 189 | + "query": { |
| 190 | + "score_mode": "multiply", |
| 191 | + "rescore_query": { |
| 192 | + "function_score": { |
| 193 | + "script_score": { |
| 194 | + "script": { |
| 195 | + "source": "Math.log10(doc.count.value + 2)" |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + ] |
| 203 | +} |
| 204 | +``` |
0 commit comments