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