Skip to content

Commit 138865a

Browse files
committed
[DOCS] Reformat script score query (#45087)
1 parent 7f74790 commit 138865a

File tree

1 file changed

+88
-47
lines changed

1 file changed

+88
-47
lines changed

docs/reference/query-dsl/script-score-query.asciidoc

Lines changed: 88 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
<titleabbrev>Script score</titleabbrev>
55
++++
66

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.
119

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.
1611

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`.
1916

2017
[source,js]
2118
--------------------------------------------------
@@ -35,23 +32,57 @@ GET /_search
3532
--------------------------------------------------
3633
// CONSOLE
3734

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
3961

4062
Within a script, you can
4163
{ref}/modules-scripting-fields.html#scripting-score[access]
4264
the `_score` variable which represents the current relevance score of a
4365
document.
4466

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
5586
`saturation(value,k) = value/(k + value)`
5687

5788
[source,js]
@@ -62,7 +93,8 @@ to be the most efficient by using the internal mechanisms.
6293
--------------------------------------------------
6394
// NOTCONSOLE
6495

65-
===== sigmoid
96+
[[script-score-sigmoid]]
97+
====== Sigmoid
6698
`sigmoid(value, k, a) = value^a/ (k^a + value^a)`
6799

68100
[source,js]
@@ -74,7 +106,7 @@ to be the most efficient by using the internal mechanisms.
74106
// NOTCONSOLE
75107

76108
[[random-score-function]]
77-
===== Random score function
109+
====== Random score function
78110
`random_score` function generates scores that are uniformly distributed
79111
from 0 up to but not including 1.
80112

@@ -104,7 +136,6 @@ by merges.
104136
--------------------------------------------------
105137
// NOTCONSOLE
106138

107-
108139
Note that documents that are within the same shard and have the
109140
same value for field will get the same score, so it is usually desirable
110141
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.
114145

115146

116147
[[decay-functions-numeric-fields]]
117-
===== Decay functions for numeric fields
148+
====== Decay functions for numeric fields
118149
You can read more about decay functions
119150
{ref}/query-dsl-function-score-query.html#function-decay[here].
120151

@@ -137,8 +168,8 @@ You can read more about decay functions
137168
// NOTCONSOLE
138169
<1> Using `params` allows to compile the script only once, even if params change.
139170

140-
141-
===== Decay functions for geo fields
171+
[[decay-functions-geo-fields]]
172+
====== Decay functions for geo fields
142173

143174
* `double decayGeoLinear(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
144175

@@ -160,8 +191,8 @@ You can read more about decay functions
160191
--------------------------------------------------
161192
// NOTCONSOLE
162193

163-
164-
===== Decay functions for date fields
194+
[[decay-functions-date-fields]]
195+
====== Decay functions for date fields
165196

166197
* `double decayDateLinear(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
167198

@@ -186,33 +217,43 @@ You can read more about decay functions
186217
NOTE: Decay functions on dates are limited to dates in the default format
187218
and default time zone. Also calculations with `now` are not supported.
188219

189-
===== Functions for vector fields
220+
[[script-score-functions-vector-fields]]
221+
====== Functions for vector fields
190222
<<vector-functions, Functions for vector fields>> are accessible through
191223
`script_score` query.
192224

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:
197230

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.
200235

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.
201240

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:
205243

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>>
208249

209250
[[script-score]]
210-
===== `script_score`
251+
====== `script_score`
211252
What you used in `script_score` of the Function Score query, you
212253
can copy into the Script Score query. No changes here.
213254

214255
[[weight]]
215-
===== `weight`
256+
====== `weight`
216257
`weight` function can be implemented in the Script Score query through
217258
the following script:
218259

@@ -228,13 +269,13 @@ the following script:
228269
// NOTCONSOLE
229270

230271
[[random-score]]
231-
===== `random_score`
272+
====== `random_score`
232273

233274
Use `randomScore` function
234275
as described in <<random-score-function, random score function>>.
235276

236277
[[field-value-factor]]
237-
===== `field_value_factor`
278+
====== `field_value_factor`
238279
`field_value_factor` function can be easily implemented through script:
239280

240281
[source,js]
@@ -284,8 +325,8 @@ through a script:
284325
|=======================================================================
285326

286327
[[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>>
289330
that can be used in script.
290331

291332
include::{es-repo-dir}/vectors/vector-functions.asciidoc[]

0 commit comments

Comments
 (0)