Skip to content

Commit fdb6603

Browse files
Change rational to saturation in script_score (elastic#37766)
This change of the function name is necessary for conformity with feature queries. Closes elastic#37714
1 parent c8565fe commit fdb6603

File tree

4 files changed

+101
-29
lines changed

4 files changed

+101
-29
lines changed

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ that can help you with scoring. We suggest you to use them instead of
5252
rewriting equivalent functions of your own, as these functions try
5353
to be the most efficient by using the internal mechanisms.
5454

55-
===== rational
56-
`rational(value,k) = value/(k + value)`
55+
===== saturation
56+
`saturation(value,k) = value/(k + value)`
5757

5858
[source,js]
5959
--------------------------------------------------
6060
"script" : {
61-
"source" : "rational(doc['likes'].value, 1)"
61+
"source" : "saturation(doc['likes'].value, 1)"
6262
}
6363
--------------------------------------------------
6464
// NOTCONSOLE
@@ -78,21 +78,22 @@ to be the most efficient by using the internal mechanisms.
7878
[[random-functions]]
7979
===== Random functions
8080
There are two predefined ways to produce random values:
81+
`randomNotReproducible` and `randomReproducible`.
8182

82-
1. `randomNotReproducible()` uses `java.util.Random` class
83+
`randomNotReproducible()` uses `java.util.Random` class
8384
to generate a random value of the type `long`.
8485
The generated values are not reproducible between requests' invocations.
8586

86-
[source,js]
87-
--------------------------------------------------
88-
"script" : {
89-
"source" : "randomNotReproducible()"
90-
}
91-
--------------------------------------------------
92-
// NOTCONSOLE
87+
[source,js]
88+
--------------------------------------------------
89+
"script" : {
90+
"source" : "randomNotReproducible()"
91+
}
92+
--------------------------------------------------
93+
// NOTCONSOLE
9394

9495

95-
2. `randomReproducible(String seedValue, int seed)` produces
96+
`randomReproducible(String seedValue, int seed)` produces
9697
reproducible random values of type `long`. This function requires
9798
more computational time and memory than the non-reproducible version.
9899

@@ -102,13 +103,13 @@ in the memory. For example, values of the document's `_seq_no` field
102103
is a good candidate, as documents on the same shard have unique values
103104
for the `_seq_no` field.
104105

105-
[source,js]
106-
--------------------------------------------------
107-
"script" : {
108-
"source" : "randomReproducible(Long.toString(doc['_seq_no'].value), 100)"
109-
}
110-
--------------------------------------------------
111-
// NOTCONSOLE
106+
[source,js]
107+
--------------------------------------------------
108+
"script" : {
109+
"source" : "randomReproducible(Long.toString(doc['_seq_no'].value), 100)"
110+
}
111+
--------------------------------------------------
112+
// NOTCONSOLE
112113

113114

114115
A drawback of using `_seq_no` is that generated values change if
@@ -121,13 +122,13 @@ you can use a field with unique values across shards,
121122
such as `_id`, but watch out for the memory usage as all
122123
these unique values need to be loaded into memory.
123124

124-
[source,js]
125-
--------------------------------------------------
126-
"script" : {
127-
"source" : "randomReproducible(doc['_id'].value, 100)"
128-
}
129-
--------------------------------------------------
130-
// NOTCONSOLE
125+
[source,js]
126+
--------------------------------------------------
127+
"script" : {
128+
"source" : "randomReproducible(doc['_id'].value, 100)"
129+
}
130+
--------------------------------------------------
131+
// NOTCONSOLE
131132

132133

133134
[[decay-functions]]
@@ -152,7 +153,7 @@ You can read more about decay functions
152153
}
153154
--------------------------------------------------
154155
// NOTCONSOLE
155-
<1> Use `params` to compile a script only once for different values of parameters
156+
<1> Using `params` allows to compile the script only once, even if params change.
156157

157158

158159
===== Decay functions for geo fields

modules/lang-painless/src/main/resources/org/elasticsearch/painless/spi/org.elasticsearch.score.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# This file contains a whitelist for functions to be used in Score context
2121

2222
static_import {
23-
double rational(double, double) from_class org.elasticsearch.script.ScoreScriptUtils
23+
double saturation(double, double) from_class org.elasticsearch.script.ScoreScriptUtils
2424
double sigmoid(double, double, double) from_class org.elasticsearch.script.ScoreScriptUtils
2525
double randomReproducible(String, int) from_class org.elasticsearch.script.ScoreScriptUtils
2626
double randomNotReproducible() bound_to org.elasticsearch.script.ScoreScriptUtils$RandomNotReproducible

modules/lang-painless/src/test/resources/rest-api-spec/test/painless/80_script_score.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,77 @@ setup:
55
version: " - 6.99.99"
66
reason: "script score query was introduced in 7.0.0"
77

8+
---
9+
"Math functions":
10+
- do:
11+
indices.create:
12+
index: test
13+
body:
14+
settings:
15+
number_of_shards: 2
16+
mappings:
17+
_doc:
18+
properties:
19+
dval:
20+
type: double
21+
- do:
22+
index:
23+
index: test
24+
type: _doc
25+
id: d1
26+
body: {"dval": 10}
27+
- do:
28+
index:
29+
index: test
30+
type: _doc
31+
id: d2
32+
body: {"dval": 100}
33+
- do:
34+
index:
35+
index: test
36+
type: _doc
37+
id: d3
38+
body: {"dval": 1000}
39+
40+
- do:
41+
indices.refresh: {}
42+
43+
- do:
44+
search:
45+
rest_total_hits_as_int: true
46+
index: test
47+
body:
48+
query:
49+
script_score:
50+
query: {match_all: {} }
51+
script:
52+
source: "saturation(doc['dval'].value, params.k)"
53+
params:
54+
k : 100
55+
- match: { hits.total: 3 }
56+
- match: { hits.hits.0._id: d3 }
57+
- match: { hits.hits.1._id: d2 }
58+
- match: { hits.hits.2._id: d1 }
59+
60+
61+
- do:
62+
search:
63+
rest_total_hits_as_int: true
64+
index: test
65+
body:
66+
query:
67+
script_score:
68+
query: {match_all: {} }
69+
script:
70+
source: "sigmoid(doc['dval'].value, params.k, params.a)"
71+
params:
72+
k: 100
73+
a: 2
74+
- match: { hits.total: 3 }
75+
- match: { hits.hits.0._id: d3 }
76+
- match: { hits.hits.1._id: d2 }
77+
- match: { hits.hits.2._id: d1 }
78+
879
---
980
"Random functions":
1081
- do:

server/src/main/java/org/elasticsearch/script/ScoreScriptUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public final class ScoreScriptUtils {
4141

4242
/****** STATIC FUNCTIONS that can be used by users for score calculations **/
4343

44-
public static double rational(double value, double k) {
44+
public static double saturation(double value, double k) {
4545
return value/ (k + value);
4646
}
4747

0 commit comments

Comments
 (0)