|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: Rescore |
| 4 | +nav_order: 99 |
| 5 | +--- |
| 6 | + |
| 7 | +# Rescore |
| 8 | + |
| 9 | +The `rescore` parameter improves search precision by reordering only the highest-ranked documents returned from your initial query. Rather than applying an expensive algorithm to all documents in the index, rescoring focuses computational resources on reranking a smaller window of top results using a secondary scoring method. |
| 10 | + |
| 11 | +When you include a `rescore` parameter in your search request, OpenSearch processes the results in the following sequence: |
| 12 | + |
| 13 | +1. **Initial search**: The primary query and any post-filters execute across all relevant documents. |
| 14 | +2. **Shard-level rescoring**: Each shard applies the rescoring algorithm to its top results. |
| 15 | +3. **Final coordination**: The coordinating node combines rescored results from all shards. |
| 16 | + |
| 17 | +This approach provides better relevance while maintaining acceptable performance. |
| 18 | + |
| 19 | +When using the `rescore` parameter, note the following important considerations: |
| 20 | + |
| 21 | +- You cannot use explicit sorting (other than `_score` in descending order) with rescoring. OpenSearch returns an error if you attempt to combine custom sorting with a rescore query. |
| 22 | + |
| 23 | +- When implementing pagination, maintain the same `window_size` across all pages. Changing the window size between pages can cause result inconsistencies as users navigate through search results. |
| 24 | + |
| 25 | +## Query rescoring |
| 26 | + |
| 27 | +Query rescoring applies a secondary query to refine the scores of top-ranked documents. You can control how many documents each shard examines using the `window_size` parameter (default is `10`). |
| 28 | + |
| 29 | +### Basic rescoring syntax |
| 30 | + |
| 31 | +```json |
| 32 | +POST /_search |
| 33 | +{ |
| 34 | + "query": { |
| 35 | + "match": { |
| 36 | + "content": { |
| 37 | + "query": "OpenSearch query optimization", |
| 38 | + "operator": "or" |
| 39 | + } |
| 40 | + } |
| 41 | + }, |
| 42 | + "rescore": { |
| 43 | + "window_size": 100, |
| 44 | + "query": { |
| 45 | + "rescore_query": { |
| 46 | + "match_phrase": { |
| 47 | + "content": { |
| 48 | + "query": "OpenSearch query optimization", |
| 49 | + "slop": 1 |
| 50 | + } |
| 51 | + } |
| 52 | + }, |
| 53 | + "query_weight": 0.8, |
| 54 | + "rescore_query_weight": 1.3 |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | +``` |
| 59 | +{% include copy-curl.html %} |
| 60 | + |
| 61 | +### Rescore parameters |
| 62 | + |
| 63 | +The `rescore` object supports the following parameters. |
| 64 | + |
| 65 | +Parameter | Type | Description |
| 66 | +--- | --- | --- |
| 67 | +`window_size` | Integer | Number of top documents to rescore per shard. Default is `10`. |
| 68 | +`query_weight` | Float | Weight applied to the original query score. Default is `1.0`. |
| 69 | +`rescore_query_weight` | Float | Weight applied to the rescore query score. Default is `1.0`. |
| 70 | +`score_mode` | String | Method for combining original and rescore query scores. Default is `total`. See [Score combination modes](#score-combination-modes). |
| 71 | + |
| 72 | +### Score combination modes |
| 73 | + |
| 74 | +The `score_mode` parameter determines how OpenSearch combines the original score with the rescore query score. This parameter accepts the following values. |
| 75 | + |
| 76 | +Mode | Description | Use case |
| 77 | +--- | --- | --- |
| 78 | +`total` | Adds original score + rescore score | General relevance improvement (default) |
| 79 | +`multiply` | Multiplies original score × rescore score | Effective with function queries that return values between 0 and 1 |
| 80 | +`avg` | Averages the two scores | Balanced approach when both scores are equally important |
| 81 | +`max` | Uses the higher of the two scores | Ensures documents with high scores in either query rank well |
| 82 | +`min` | Uses the lower of the two scores | Conservative approach that requires both queries to agree |
| 83 | + |
| 84 | +## Multiple rescoring stages |
| 85 | + |
| 86 | +You can chain multiple rescoring operations to apply increasingly sophisticated ranking algorithms: |
| 87 | + |
| 88 | +```json |
| 89 | +POST /_search |
| 90 | +{ |
| 91 | + "query": { |
| 92 | + "match": { |
| 93 | + "title": { |
| 94 | + "query": "search engine technology", |
| 95 | + "operator": "or" |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + "rescore": [ |
| 100 | + { |
| 101 | + "window_size": 200, |
| 102 | + "query": { |
| 103 | + "rescore_query": { |
| 104 | + "match_phrase": { |
| 105 | + "title": { |
| 106 | + "query": "search engine technology", |
| 107 | + "slop": 2 |
| 108 | + } |
| 109 | + } |
| 110 | + }, |
| 111 | + "query_weight": 0.6, |
| 112 | + "rescore_query_weight": 1.4 |
| 113 | + } |
| 114 | + }, |
| 115 | + { |
| 116 | + "window_size": 50, |
| 117 | + "query": { |
| 118 | + "score_mode": "multiply", |
| 119 | + "rescore_query": { |
| 120 | + "function_score": { |
| 121 | + "script_score": { |
| 122 | + "script": { |
| 123 | + "source": "Math.log10(doc['popularity'].value + 2)" |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + ] |
| 131 | +} |
| 132 | +``` |
| 133 | +{% include copy-curl.html %} |
| 134 | + |
| 135 | +In this multi-stage example: |
| 136 | +1. The **first rescoring stage** examines 200 documents per shard, applying phrase matching to improve relevance. |
| 137 | +2. The **second rescoring stage** takes the top 50 results from the first stage and applies popularity-based scoring using a logarithmic function. |
| 138 | + |
| 139 | +Each stage processes the results from the previous stage, creating a refinement pipeline where computationally expensive operations only operate on the most promising candidates. |
0 commit comments