diff --git a/server/src/main/java/org/elasticsearch/script/ScoreScript.java b/server/src/main/java/org/elasticsearch/script/ScoreScript.java index 5c533298cbe13..c88c68fd407a2 100644 --- a/server/src/main/java/org/elasticsearch/script/ScoreScript.java +++ b/server/src/main/java/org/elasticsearch/script/ScoreScript.java @@ -26,6 +26,8 @@ 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 @@ */ public abstract class ScoreScript { + private static final Map DEPRECATIONS; + static { + Map 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,9 +63,18 @@ public abstract class ScoreScript { private DoubleSupplier scoreSupplier = () -> 0.0; public ScoreScript(Map params, SearchLookup lookup, LeafReaderContext leafContext) { - this.params = params; // null check needed b/c of expression engine subclass - this.leafLookup = lookup == null ? null : lookup.getLeafSearchLookup(leafContext); + 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();