org.elasticsearch.script.FieldScript Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch Show documentation
Show all versions of elasticsearch Show documentation
Elasticsearch subproject :server
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.script;
import org.apache.lucene.index.LeafReaderContext;
import org.elasticsearch.common.logging.DeprecationCategory;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.search.lookup.SearchLookup;
import org.elasticsearch.search.lookup.SourceLookup;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
/**
* A script to produce dynamic values for return fields.
*/
public abstract class FieldScript extends DocBasedScript {
public static final String[] PARAMETERS = {};
private static final DeprecationLogger deprecationLogger = DeprecationLogger.getLogger(DynamicMap.class);
private static final Map> PARAMS_FUNCTIONS = Map.of("doc", value -> {
deprecationLogger.warn(
DeprecationCategory.SCRIPTING,
"field-script_doc",
"Accessing variable [doc] via [params.doc] from within an field-script " + "is deprecated in favor of directly accessing [doc]."
);
return value;
}, "_doc", value -> {
deprecationLogger.warn(
DeprecationCategory.SCRIPTING,
"field-script__doc",
"Accessing variable [doc] via [params._doc] from within an field-script "
+ "is deprecated in favor of directly accessing [doc]."
);
return value;
}, "_source", value -> ((SourceLookup) value).source());
/** The generic runtime parameters for the script. */
private final Map params;
public FieldScript(Map params, SearchLookup lookup, LeafReaderContext leafContext) {
super(new DocValuesDocReader(lookup, leafContext));
params = new HashMap<>(params);
params.putAll(docAsMap());
this.params = new DynamicMap(params, PARAMS_FUNCTIONS);
}
// for expression engine
protected FieldScript() {
super(null);
params = null;
}
public abstract Object execute();
/** Return the parameters for this script. */
public Map getParams() {
return params;
}
/** A factory to construct {@link FieldScript} instances. */
public interface LeafFactory {
FieldScript newInstance(LeafReaderContext ctx) throws IOException;
}
public interface Factory extends ScriptFactory {
LeafFactory newFactory(Map params, SearchLookup lookup);
}
/** The context used to compile {@link FieldScript} factories. */
public static final ScriptContext CONTEXT = new ScriptContext<>("field", Factory.class);
}