Scripting: Add back lookup vars in score script
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.
@@ -26,6 +26,8 @@ import org.elasticsearch.search.lookup.SearchLookup;
import java.io.IOException;
import java.io.UncheckedIOException;
+import java.util.Collections;
+import java.util.HashMap;
import java.util.Map;
import java.util.function.DoubleSupplier;
@@ -34,6 +36,22 @@ import java.util.function.DoubleSupplier;
*/
public abstract class ScoreScript {
+ private static final Map<String, String> DEPRECATIONS;
+ static {
+ Map<String, String> deprecations = new HashMap<>();
+ deprecations.put(
+ "doc",
+ "Accessing variable [doc] via [params.doc] from within a score script " +
+ "is deprecated in favor of directly accessing [doc]."
+ );
+ deprecations.put(
+ "_doc",
+ "Accessing variable [doc] via [params._doc] from within a score script " +
+ "is deprecated in favor of directly accessing [doc]."
+ );
+ DEPRECATIONS = Collections.unmodifiableMap(deprecations);
+ }
+
public static final String[] PARAMETERS = new String[]{};
/** The generic runtime parameters for the script. */
@@ -45,8 +63,18 @@ public abstract class ScoreScript {
private DoubleSupplier scoreSupplier = () -> 0.0;
public ScoreScript(Map<String, Object> params, SearchLookup lookup, LeafReaderContext leafContext) {
- this.params = params;
+ // null check needed b/c of expression engine subclass
+ if (lookup == null) {
+ assert params == null;
+ assert leafContext == null;
+ this.params = null;
+ this.leafLookup = null;
+ } else {
this.leafLookup = lookup.getLeafSearchLookup(leafContext);
+ params = new HashMap<>(params);
+ params.putAll(leafLookup.asMap());
+ this.params = new ParameterMap(params, DEPRECATIONS);
+ }
}
public abstract double execute();
