|
| 1 | +:ref_current: https://www.elastic.co/guide/en/elasticsearch/reference/2.3 |
| 2 | + |
| 3 | +:github: https://github.com/elastic/elasticsearch-net |
| 4 | + |
| 5 | +:nuget: https://www.nuget.org/packages |
| 6 | + |
| 7 | +[[rescore-usage]] |
| 8 | +== Rescore Usage |
| 9 | + |
| 10 | +Rescoring can help to improve precision by reordering just the top (eg 100 - 500) documents |
| 11 | +returned by the query and post_filter phases, using a secondary (usually more costly) algorithm, |
| 12 | +instead of applying the costly algorithm to all documents in the index. |
| 13 | + |
| 14 | +See the Elasticsearch documentation on {ref_current}/search-request-rescore.html[Rescoring] for more detail. |
| 15 | + |
| 16 | +[[single-rescore-query]] |
| 17 | +[float] |
| 18 | +== Single rescore query |
| 19 | + |
| 20 | +=== Fluent DSL Example |
| 21 | + |
| 22 | +[source,csharp] |
| 23 | +---- |
| 24 | +s => s |
| 25 | +.From(10) |
| 26 | +.Size(20) |
| 27 | +.Query(q => q |
| 28 | + .MatchAll() |
| 29 | +) |
| 30 | +.Rescore(r => r |
| 31 | + .WindowSize(20) |
| 32 | + .RescoreQuery(rq => rq |
| 33 | + .ScoreMode(ScoreMode.Multiply) |
| 34 | + .Query(q => q |
| 35 | + .ConstantScore(cs => cs |
| 36 | + .Filter(f => f |
| 37 | + .Terms(t => t |
| 38 | + .Field(p => p.Tags.First()) |
| 39 | + .Terms("eos", "sit", "sed") |
| 40 | + ) |
| 41 | + ) |
| 42 | + ) |
| 43 | + ) |
| 44 | + ) |
| 45 | +) |
| 46 | +---- |
| 47 | + |
| 48 | +=== Object Initializer Syntax Example |
| 49 | + |
| 50 | +[source,csharp] |
| 51 | +---- |
| 52 | +new SearchRequest<Project> |
| 53 | +{ |
| 54 | + From = 10, |
| 55 | + Size = 20, |
| 56 | + Query = new QueryContainer(new MatchAllQuery()), |
| 57 | + Rescore = new Rescore |
| 58 | + { |
| 59 | + WindowSize = 20, |
| 60 | + Query = new RescoreQuery |
| 61 | + { |
| 62 | + ScoreMode = ScoreMode.Multiply, |
| 63 | + Query = new ConstantScoreQuery |
| 64 | + { |
| 65 | + Filter = new TermsQuery |
| 66 | + { |
| 67 | + Field = Infer.Field<Project>(p => p.Tags.First()), |
| 68 | + Terms = new[] { "eos", "sit", "sed" } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | +} |
| 75 | +---- |
| 76 | + |
| 77 | +[source,javascript] |
| 78 | +.Example json output |
| 79 | +---- |
| 80 | +{ |
| 81 | + "from": 10, |
| 82 | + "size": 20, |
| 83 | + "query": { |
| 84 | + "match_all": {} |
| 85 | + }, |
| 86 | + "rescore": { |
| 87 | + "window_size": 20, |
| 88 | + "query": { |
| 89 | + "score_mode": "multiply", |
| 90 | + "rescore_query": { |
| 91 | + "constant_score": { |
| 92 | + "filter": { |
| 93 | + "terms": { |
| 94 | + "tags": [ |
| 95 | + "eos", |
| 96 | + "sit", |
| 97 | + "sed" |
| 98 | + ] |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +---- |
| 107 | + |
| 108 | +[[multiple-rescore-queries]] |
| 109 | +[float] |
| 110 | +== Multiple rescore queries |
| 111 | + |
| 112 | +=== Fluent DSL Example |
| 113 | + |
| 114 | +[source,csharp] |
| 115 | +---- |
| 116 | +s => s |
| 117 | +.From(10) |
| 118 | +.Size(20) |
| 119 | +.Query(q => q |
| 120 | + .MatchAll() |
| 121 | +) |
| 122 | +.Rescore(r => r |
| 123 | + .WindowSize(20) |
| 124 | + .RescoreQuery(rq => rq |
| 125 | + .ScoreMode(ScoreMode.Multiply) |
| 126 | + .Query(q => q |
| 127 | + .ConstantScore(cs => cs |
| 128 | + .Filter(f => f |
| 129 | + .Terms(t => t |
| 130 | + .Field(p => p.Tags.First()) |
| 131 | + .Terms("eos", "sit", "sed") |
| 132 | + ) |
| 133 | + ) |
| 134 | + ) |
| 135 | + ) |
| 136 | + ) |
| 137 | +) |
| 138 | +.Rescore(rr => rr |
| 139 | + .RescoreQuery(rq => rq |
| 140 | + .ScoreMode(ScoreMode.Total) |
| 141 | + .Query(q => q |
| 142 | + .FunctionScore(fs => fs |
| 143 | + .Functions(f => f |
| 144 | + .RandomScore(1337) |
| 145 | + ) |
| 146 | + ) |
| 147 | + ) |
| 148 | + ) |
| 149 | +) |
| 150 | +---- |
| 151 | + |
| 152 | +=== Object Initializer Syntax Example |
| 153 | + |
| 154 | +[source,csharp] |
| 155 | +---- |
| 156 | +new SearchRequest<Project> |
| 157 | +{ |
| 158 | + From = 10, |
| 159 | + Size = 20, |
| 160 | + Query = new QueryContainer(new MatchAllQuery()), |
| 161 | + Rescore = new MultiRescore |
| 162 | + { |
| 163 | + new Rescore |
| 164 | + { |
| 165 | + WindowSize = 20, |
| 166 | + Query = new RescoreQuery |
| 167 | + { |
| 168 | + ScoreMode = ScoreMode.Multiply, |
| 169 | + Query = new ConstantScoreQuery |
| 170 | + { |
| 171 | + Filter = new TermsQuery |
| 172 | + { |
| 173 | + Field = Infer.Field<Project>(p => p.Tags.First()), |
| 174 | + Terms = new[] { "eos", "sit", "sed" } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + }, |
| 179 | + new Rescore |
| 180 | + { |
| 181 | + Query = new RescoreQuery |
| 182 | + { |
| 183 | + ScoreMode = ScoreMode.Total, |
| 184 | + Query = new FunctionScoreQuery |
| 185 | + { |
| 186 | + Functions = new List<IScoreFunction> |
| 187 | + { |
| 188 | + new RandomScoreFunction |
| 189 | + { |
| 190 | + Seed = 1337 |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
| 198 | +---- |
| 199 | + |
| 200 | +[source,javascript] |
| 201 | +.Example json output |
| 202 | +---- |
| 203 | +{ |
| 204 | + "from": 10, |
| 205 | + "size": 20, |
| 206 | + "query": { |
| 207 | + "match_all": {} |
| 208 | + }, |
| 209 | + "rescore": [ |
| 210 | + { |
| 211 | + "window_size": 20, |
| 212 | + "query": { |
| 213 | + "score_mode": "multiply", |
| 214 | + "rescore_query": { |
| 215 | + "constant_score": { |
| 216 | + "filter": { |
| 217 | + "terms": { |
| 218 | + "tags": [ |
| 219 | + "eos", |
| 220 | + "sit", |
| 221 | + "sed" |
| 222 | + ] |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + }, |
| 229 | + { |
| 230 | + "query": { |
| 231 | + "score_mode": "total", |
| 232 | + "rescore_query": { |
| 233 | + "function_score": { |
| 234 | + "functions": [ |
| 235 | + { |
| 236 | + "random_score": { |
| 237 | + "seed": 1337 |
| 238 | + } |
| 239 | + } |
| 240 | + ] |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + } |
| 245 | + ] |
| 246 | +} |
| 247 | +---- |
| 248 | + |
0 commit comments