Skip to content

Commit c53b7d3

Browse files
authored
Scripting: Add back lookup vars in score script (#34833) (#35386)
The lookup vars under params (namely _fields and _source) were inadvertently removed when scoring scripts were converted to using script contexts. This commit adds them back, along with deprecation warnings for those that should not be used.
1 parent 5926a86 commit c53b7d3

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,32 @@
2626

2727
import java.io.IOException;
2828
import java.io.UncheckedIOException;
29+
import java.util.Collections;
30+
import java.util.HashMap;
2931
import java.util.Map;
3032
import java.util.function.DoubleSupplier;
3133

3234
/**
3335
* A script used for adjusting the score on a per document basis.
3436
*/
3537
public abstract class ScoreScript {
36-
38+
39+
private static final Map<String, String> DEPRECATIONS;
40+
static {
41+
Map<String, String> deprecations = new HashMap<>();
42+
deprecations.put(
43+
"doc",
44+
"Accessing variable [doc] via [params.doc] from within a score script " +
45+
"is deprecated in favor of directly accessing [doc]."
46+
);
47+
deprecations.put(
48+
"_doc",
49+
"Accessing variable [doc] via [params._doc] from within a score script " +
50+
"is deprecated in favor of directly accessing [doc]."
51+
);
52+
DEPRECATIONS = Collections.unmodifiableMap(deprecations);
53+
}
54+
3755
public static final String[] PARAMETERS = new String[]{};
3856

3957
/** The generic runtime parameters for the script. */
@@ -45,8 +63,18 @@ public abstract class ScoreScript {
4563
private DoubleSupplier scoreSupplier = () -> 0.0;
4664

4765
public ScoreScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) {
48-
this.params = params;
49-
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
66+
// null check needed b/c of expression engine subclass
67+
if (lookup == null) {
68+
assert params == null;
69+
assert leafContext == null;
70+
this.params = null;
71+
this.leafLookup = null;
72+
} else {
73+
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
74+
params = new HashMap<>(params);
75+
params.putAll(leafLookup.asMap());
76+
this.params = new ParameterMap(params, DEPRECATIONS);
77+
}
5078
}
5179

5280
public abstract double execute();

0 commit comments

Comments
 (0)