Skip to content

Commit 99abe35

Browse files
committed
[DOCS] Reformat script score query (#45087)
1 parent 15ac9ed commit 99abe35

File tree

1 file changed

+82
-44
lines changed

1 file changed

+82
-44
lines changed

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

Lines changed: 82 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +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

1712

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

2117
[source,js]
2218
--------------------------------------------------
@@ -37,23 +33,56 @@ GET /_search
3733
// CONSOLE
3834
// TEST[setup:twitter]
3935

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
4162

4263
Within a script, you can
4364
{ref}/modules-scripting-fields.html#scripting-score[access]
4465
the `_score` variable which represents the current relevance score of a
4566
document.
4667

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

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

56-
===== saturation
84+
[[script-score-saturation]]
85+
====== Saturation
5786
`saturation(value,k) = value/(k + value)`
5887

5988
[source,js]
@@ -64,7 +93,8 @@ to be the most efficient by using the internal mechanisms.
6493
--------------------------------------------------
6594
// NOTCONSOLE
6695

67-
===== sigmoid
96+
[[script-score-sigmoid]]
97+
====== Sigmoid
6898
`sigmoid(value, k, a) = value^a/ (k^a + value^a)`
6999

70100
[source,js]
@@ -76,7 +106,7 @@ to be the most efficient by using the internal mechanisms.
76106
// NOTCONSOLE
77107

78108
[[random-score-function]]
79-
===== Random score function
109+
====== Random score function
80110
`random_score` function generates scores that are uniformly distributed
81111
from 0 up to but not including 1.
82112

@@ -106,7 +136,6 @@ by merges.
106136
--------------------------------------------------
107137
// NOTCONSOLE
108138

109-
110139
Note that documents that are within the same shard and have the
111140
same value for field will get the same score, so it is usually desirable
112141
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.
116145

117146

118147
[[decay-functions-numeric-fields]]
119-
===== Decay functions for numeric fields
148+
====== Decay functions for numeric fields
120149
You can read more about decay functions
121150
{ref}/query-dsl-function-score-query.html#function-decay[here].
122151

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

142-
143-
===== Decay functions for geo fields
171+
[[decay-functions-geo-fields]]
172+
====== Decay functions for geo fields
144173

145174
* `double decayGeoLinear(String originStr, String scaleStr, String offsetStr, double decay, GeoPoint docValue)`
146175

@@ -162,8 +191,8 @@ You can read more about decay functions
162191
--------------------------------------------------
163192
// NOTCONSOLE
164193

165-
166-
===== Decay functions for date fields
194+
[[decay-functions-date-fields]]
195+
====== Decay functions for date fields
167196

168197
* `double decayDateLinear(String originStr, String scaleStr, String offsetStr, double decay, JodaCompatibleZonedDateTime docValueDate)`
169198

@@ -189,29 +218,38 @@ NOTE: Decay functions on dates are limited to dates in the default format
189218
and default time zone. Also calculations with `now` are not supported.
190219

191220

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

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

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

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

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

208246
[[script-score]]
209-
===== `script_score`
247+
====== `script_score`
210248
What you used in `script_score` of the Function Score query, you
211249
can copy into the Script Score query. No changes here.
212250

213251
[[weight]]
214-
===== `weight`
252+
====== `weight`
215253
`weight` function can be implemented in the Script Score query through
216254
the following script:
217255

@@ -227,13 +265,13 @@ the following script:
227265
// NOTCONSOLE
228266

229267
[[random-score]]
230-
===== `random_score`
268+
====== `random_score`
231269

232270
Use `randomScore` function
233271
as described in <<random-score-function, random score function>>.
234272

235273
[[field-value-factor]]
236-
===== `field_value_factor`
274+
====== `field_value_factor`
237275
`field_value_factor` function can be easily implemented through script:
238276

239277
[source,js]
@@ -283,8 +321,8 @@ through a script:
283321
|=======================================================================
284322

285323
[[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>>
288326
that can be used in script.
289327

290328

0 commit comments

Comments
 (0)