Scripting: Add back lookup vars in score script (#34833)

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.
This commit is contained in:
Ryan Ernst 2018-11-07 15:09:09 -08:00 committed by Jack Conradson
parent d7a4cef483
commit a4d979cfc8
1 changed files with 29 additions and 2 deletions

View File

@ -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,9 +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
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();