4
4
<titleabbrev>Script score</titleabbrev>
5
5
++++
6
6
7
- The `script_score` allows you to modify the score of documents that are
8
- retrieved by a query. This can be useful if, for example, a score
9
- function is computationally expensive and it is sufficient to compute
10
- the score on a filtered set of documents.
7
+ Uses a <<modules-scripting,script>> to provide a custom score for returned
8
+ documents.
11
9
12
- To use `script_score`, you have to define a query and a script -
13
- a function to be used to compute a new score for each document returned
14
- by the query. For more information on scripting see
15
- <<modules-scripting, scripting documentation>>.
10
+ The `script_score` query is useful if, for example, a scoring function is expensive and you only need to calculate the score of a filtered set of documents.
16
11
17
12
18
- Here is an example of using `script_score` to assign each matched document
19
- a score equal to the number of likes divided by 10:
13
+ [[script-score-query-ex-request]]
14
+ ==== Example request
15
+ The following `script_score` query assigns each returned document a score equal to the `likes` field value divided by `10`.
20
16
21
17
[source,js]
22
18
--------------------------------------------------
@@ -37,23 +33,56 @@ GET /_search
37
33
// CONSOLE
38
34
// TEST[setup:twitter]
39
35
40
- ==== Accessing the score of a document within a script
36
+
37
+ [[script-score-top-level-params]]
38
+ ==== Top-level parameters for `script_score`
39
+ `query`::
40
+ (Required, query object) Query used to return documents.
41
+
42
+ `script`::
43
+ +
44
+ --
45
+ (Required, <<modules-scripting-using,script object>>) Script used to compute the score of documents returned by the `query`.
46
+
47
+ IMPORTANT: Final relevance scores from the `script_score` query cannot be
48
+ negative. To support certain search optimizations, Lucene requires
49
+ scores be positive or `0`.
50
+ --
51
+
52
+ `min_score`::
53
+ (Optional, float) Documents with a <<relevance-scores,relevance score>> lower
54
+ than this floating point number are excluded from the search results.
55
+
56
+
57
+ [[script-score-query-notes]]
58
+ ==== Notes
59
+
60
+ [[script-score-access-scores]]
61
+ ===== Use relevance scores in a script
41
62
42
63
Within a script, you can
43
64
{ref}/modules-scripting-fields.html#scripting-score[access]
44
65
the `_score` variable which represents the current relevance score of a
45
66
document.
46
67
68
+ [[script-score-predefined-functions]]
69
+ ===== Predefined functions
70
+ You can use any of the available {painless}/painless-contexts.html[painless
71
+ functions] in your `script`. You can also use the following predefined functions
72
+ to customize scoring:
73
+
74
+ * <<script-score-saturation>>
75
+ * <<script-score-sigmoid>>
76
+ * <<random-score-function>>
77
+ * <<decay-functions-numeric-fields>>
78
+ * <<decay-functions-geo-fields>>
79
+ * <<decay-functions-date-fields>>
47
80
48
- ==== Predefined functions within a Painless script
49
- You can use any of the available
50
- <<painless-api-reference, painless functions>> in the painless script.
51
- Besides these functions, there are a number of predefined functions
52
- that can help you with scoring. We suggest you to use them instead of
53
- rewriting equivalent functions of your own, as these functions try
54
- to be the most efficient by using the internal mechanisms.
81
+ We suggest using these predefined functions instead of writing your own.
82
+ These functions take advantage of efficiencies from {es}' internal mechanisms.
55
83
56
- ===== saturation
84
+ [[script-score-saturation]]
85
+ ====== Saturation
57
86
`saturation(value,k) = value/(k + value)`
58
87
59
88
[source,js]
@@ -64,7 +93,8 @@ to be the most efficient by using the internal mechanisms.
64
93
--------------------------------------------------
65
94
// NOTCONSOLE
66
95
67
- ===== sigmoid
96
+ [[script-score-sigmoid]]
97
+ ====== Sigmoid
68
98
`sigmoid(value, k, a) = value^a/ (k^a + value^a)`
69
99
70
100
[source,js]
@@ -76,7 +106,7 @@ to be the most efficient by using the internal mechanisms.
76
106
// NOTCONSOLE
77
107
78
108
[[random-score-function]]
79
- ===== Random score function
109
+ ====== Random score function
80
110
`random_score` function generates scores that are uniformly distributed
81
111
from 0 up to but not including 1.
82
112
@@ -106,7 +136,6 @@ by merges.
106
136
--------------------------------------------------
107
137
// NOTCONSOLE
108
138
109
-
110
139
Note that documents that are within the same shard and have the
111
140
same value for field will get the same score, so it is usually desirable
112
141
to use a field that has unique values for all documents across a shard.
@@ -116,7 +145,7 @@ updated since update operations also update the value of the `_seq_no` field.
116
145
117
146
118
147
[[decay-functions-numeric-fields]]
119
- ===== Decay functions for numeric fields
148
+ ====== Decay functions for numeric fields
120
149
You can read more about decay functions
121
150
{ref}/query-dsl-function-score-query.html#function-decay[here].
122
151
@@ -139,8 +168,8 @@ You can read more about decay functions
139
168
// NOTCONSOLE
140
169
<1> Using `params` allows to compile the script only once, even if params change.
141
170
142
-
143
- ===== Decay functions for geo fields
171
+ [[decay-functions-geo-fields]]
172
+ ====== Decay functions for geo fields
144
173
145
174
* `double decayGeoLinear(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
146
175
@@ -162,8 +191,8 @@ You can read more about decay functions
162
191
--------------------------------------------------
163
192
// NOTCONSOLE
164
193
165
-
166
- ===== Decay functions for date fields
194
+ [[decay-functions-date-fields]]
195
+ ====== Decay functions for date fields
167
196
168
197
* `double decayDateLinear(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
169
198
@@ -189,29 +218,38 @@ NOTE: Decay functions on dates are limited to dates in the default format
189
218
and default time zone. Also calculations with `now` are not supported.
190
219
191
220
192
- ==== Faster alternatives
193
- Script Score Query calculates the score for every hit (matching document).
194
- There are faster alternative query types that can efficiently skip
195
- non-competitive hits:
221
+ [[script-score-faster-alt]]
222
+ ===== Faster alternatives
223
+ The `script_score` query calculates the score for
224
+ every matching document, or hit. There are faster alternative query types that
225
+ can efficiently skip non-competitive hits:
196
226
197
- * If you want to boost documents on some static fields, use
198
- <<query-dsl-rank-feature-query, Rank Feature Query>>.
227
+ * If you want to boost documents on some static fields, use the
228
+ <<query-dsl-rank-feature-query, `rank_feature`>> query.
229
+ * If you want to boost documents closer to a date or geographic point, use the
230
+ <<query-dsl-distance-feature-query, `distance_feature`>> query.
199
231
232
+ [[script-score-function-score-transition]]
233
+ ===== Transition from the function score query
234
+ We are deprecating the <<query-dsl-function-score-query, `function_score`>>
235
+ query. We recommend using the `script_score` query instead.
200
236
201
- ==== Transition from Function Score Query
202
- We are deprecating <<query-dsl-function-score-query, Function Score>>, and
203
- Script Score Query will be a substitute for it.
237
+ You can implement the following functions from the `function_score` query using
238
+ the `script_score` query:
204
239
205
- Here we describe how Function Score Query's functions can be
206
- equivalently implemented in Script Score Query:
240
+ * <<script-score>>
241
+ * <<weight>>
242
+ * <<random-score>>
243
+ * <<field-value-factor>>
244
+ * <<decay-functions>>
207
245
208
246
[[script-score]]
209
- ===== `script_score`
247
+ ====== `script_score`
210
248
What you used in `script_score` of the Function Score query, you
211
249
can copy into the Script Score query. No changes here.
212
250
213
251
[[weight]]
214
- ===== `weight`
252
+ ====== `weight`
215
253
`weight` function can be implemented in the Script Score query through
216
254
the following script:
217
255
@@ -227,13 +265,13 @@ the following script:
227
265
// NOTCONSOLE
228
266
229
267
[[random-score]]
230
- ===== `random_score`
268
+ ====== `random_score`
231
269
232
270
Use `randomScore` function
233
271
as described in <<random-score-function, random score function>>.
234
272
235
273
[[field-value-factor]]
236
- ===== `field_value_factor`
274
+ ====== `field_value_factor`
237
275
`field_value_factor` function can be easily implemented through script:
238
276
239
277
[source,js]
@@ -283,8 +321,8 @@ through a script:
283
321
|=======================================================================
284
322
285
323
[[decay-functions]]
286
- ===== `decay functions`
287
- Script Score query has equivalent <<decay-functions, decay functions>>
324
+ ====== `decay` functions
325
+ The `script_score` query has equivalent <<decay-functions, decay functions>>
288
326
that can be used in script.
289
327
290
328
0 commit comments