Scripting: Added a new script construct
Added an initial script construct to unify the parameters typically passed to methods in the ScriptService. This changes the way several public methods are called in the ScriptService along with all the callers since they must wrap the parameters passed in into a script object. In the future, parsing parameters can also be moved into this construct along with ToXContent. closes #10649
This commit is contained in:
parent
2627324ac2
commit
a37d3c02ec
|
@ -46,6 +46,7 @@ import org.elasticsearch.index.shard.IndexShard;
|
|||
import org.elasticsearch.index.shard.ShardId;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.search.fetch.source.FetchSourceContext;
|
||||
import org.elasticsearch.search.lookup.SourceLookup;
|
||||
|
@ -94,7 +95,7 @@ public class UpdateHelper extends AbstractComponent {
|
|||
ctx.put("op", "create");
|
||||
ctx.put("_source", upsertDoc);
|
||||
try {
|
||||
ExecutableScript script = scriptService.executable(request.scriptLang, request.script, request.scriptType, ScriptContext.Standard.UPDATE, request.scriptParams);
|
||||
ExecutableScript script = scriptService.executable(new Script(request.scriptLang, request.script, request.scriptType, request.scriptParams), ScriptContext.Standard.UPDATE);
|
||||
script.setNextVar("ctx", ctx);
|
||||
script.run();
|
||||
// we need to unwrap the ctx...
|
||||
|
@ -193,7 +194,7 @@ public class UpdateHelper extends AbstractComponent {
|
|||
ctx.put("_source", sourceAndContent.v2());
|
||||
|
||||
try {
|
||||
ExecutableScript script = scriptService.executable(request.scriptLang, request.script, request.scriptType, ScriptContext.Standard.UPDATE, request.scriptParams);
|
||||
ExecutableScript script = scriptService.executable(new Script(request.scriptLang, request.script, request.scriptType, request.scriptParams), ScriptContext.Standard.UPDATE);
|
||||
script.setNextVar("ctx", ctx);
|
||||
script.run();
|
||||
// we need to unwrap the ctx...
|
||||
|
|
|
@ -67,6 +67,7 @@ import org.elasticsearch.index.mapper.internal.VersionFieldMapper;
|
|||
import org.elasticsearch.index.mapper.object.ObjectMapper;
|
||||
import org.elasticsearch.index.mapper.object.RootObjectMapper;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
|
@ -734,7 +735,7 @@ public class DocumentMapper implements ToXContent {
|
|||
public Map<String, Object> transformSourceAsMap(Map<String, Object> sourceAsMap) {
|
||||
try {
|
||||
// We use the ctx variable and the _source name to be consistent with the update api.
|
||||
ExecutableScript executable = scriptService.executable(language, script, scriptType, ScriptContext.Standard.MAPPING, parameters);
|
||||
ExecutableScript executable = scriptService.executable(new Script(language, script, scriptType, parameters), ScriptContext.Standard.MAPPING);
|
||||
Map<String, Object> ctx = new HashMap<>(1);
|
||||
ctx.put("_source", sourceAsMap);
|
||||
executable.setNextVar("ctx", ctx);
|
||||
|
|
|
@ -32,6 +32,7 @@ import org.elasticsearch.common.inject.Inject;
|
|||
import org.elasticsearch.common.lucene.HashedBytesRef;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptParameterParser;
|
||||
import org.elasticsearch.script.ScriptParameterParser.ScriptParameterValue;
|
||||
|
@ -135,7 +136,7 @@ public class ScriptFilterParser implements FilterParser {
|
|||
public ScriptFilter(String scriptLang, String script, ScriptService.ScriptType scriptType, Map<String, Object> params, ScriptService scriptService, SearchLookup searchLookup) {
|
||||
this.script = script;
|
||||
this.params = params;
|
||||
this.searchScript = scriptService.search(searchLookup, scriptLang, script, scriptType, ScriptContext.Standard.SEARCH, newHashMap(params));
|
||||
this.searchScript = scriptService.search(searchLookup, new Script(scriptLang, script, scriptType, newHashMap(params)), ScriptContext.Standard.SEARCH);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -27,6 +27,7 @@ import org.elasticsearch.common.xcontent.XContentFactory;
|
|||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
|
||||
|
||||
|
@ -77,7 +78,7 @@ public class TemplateQueryParser implements QueryParser {
|
|||
public Query parse(QueryParseContext parseContext) throws IOException {
|
||||
XContentParser parser = parseContext.parser();
|
||||
TemplateContext templateContext = parse(parser, PARAMS, parametersToTypes);
|
||||
ExecutableScript executable = this.scriptService.executable(MustacheScriptEngineService.NAME, templateContext.template(), templateContext.scriptType(), ScriptContext.Standard.SEARCH, templateContext.params());
|
||||
ExecutableScript executable = this.scriptService.executable(new Script(MustacheScriptEngineService.NAME, templateContext.template(), templateContext.scriptType(), templateContext.params()), ScriptContext.Standard.SEARCH);
|
||||
|
||||
BytesReference querySource = (BytesReference) executable.run();
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ public class ScriptScoreFunctionParser implements ScoreFunctionParser {
|
|||
|
||||
SearchScript searchScript;
|
||||
try {
|
||||
searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptParameterParser.lang(), script, scriptType, ScriptContext.Standard.SEARCH, vars);
|
||||
searchScript = parseContext.scriptService().search(parseContext.lookup(), new Script(scriptParameterParser.lang(), script, scriptType, vars), ScriptContext.Standard.SEARCH);
|
||||
return new ScriptScoreFunction(script, vars, searchScript);
|
||||
} catch (Exception e) {
|
||||
throw new QueryParsingException(parseContext.index(), NAMES[0] + " the script could not be loaded", e);
|
||||
|
|
|
@ -20,23 +20,35 @@
|
|||
package org.elasticsearch.script;
|
||||
|
||||
/**
|
||||
*
|
||||
* CompiledScript holds all the parameters necessary to execute a previously compiled script.
|
||||
*/
|
||||
public class CompiledScript {
|
||||
|
||||
private final String type;
|
||||
|
||||
private final String lang;
|
||||
private final Object compiled;
|
||||
|
||||
public CompiledScript(String type, Object compiled) {
|
||||
this.type = type;
|
||||
/**
|
||||
* Constructor for CompiledScript.
|
||||
* @param lang The language of the script to be executed.
|
||||
* @param compiled The compiled script Object that is executable.
|
||||
*/
|
||||
public CompiledScript(String lang, Object compiled) {
|
||||
this.lang = lang;
|
||||
this.compiled = compiled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the language.
|
||||
* @return The language of the script to be executed.
|
||||
*/
|
||||
public String lang() {
|
||||
return type;
|
||||
return lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to get the compiled script object.
|
||||
* @return The compiled script Object that is executable.
|
||||
*/
|
||||
public Object compiled() {
|
||||
return compiled;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch licenses this file to you under
|
||||
* the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.script;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.elasticsearch.ElasticsearchIllegalArgumentException;
|
||||
|
||||
import static org.elasticsearch.script.ScriptService.ScriptType;
|
||||
|
||||
/**
|
||||
* Script holds all the parameters necessary to compile or find in cache and then execute a script.
|
||||
*/
|
||||
public class Script {
|
||||
|
||||
private final String lang;
|
||||
private final String script;
|
||||
private final ScriptType type;
|
||||
private final Map<String, Object> params;
|
||||
|
||||
/**
|
||||
* Constructor for Script.
|
||||
* @param lang The language of the script to be compiled/executed.
|
||||
* @param script The cache key of the script to be compiled/executed. For dynamic scripts this is the actual
|
||||
* script source code. For indexed scripts this is the id used in the request. For on disk scripts
|
||||
* this is the file name.
|
||||
* @param type The type of script -- dynamic, indexed, or file.
|
||||
* @param params The map of parameters the script will be executed with.
|
||||
*/
|
||||
public Script(String lang, String script, ScriptType type, Map<String, Object> params) {
|
||||
if (script == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("The parameter script (String) must not be null in Script.");
|
||||
}
|
||||
if (type == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("The parameter type (ScriptType) must not be null in Script.");
|
||||
}
|
||||
|
||||
this.lang = lang;
|
||||
this.script = script;
|
||||
this.type = type;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for getting language.
|
||||
* @return The language of the script to be compiled/executed.
|
||||
*/
|
||||
public String getLang() {
|
||||
return lang;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for getting the script.
|
||||
* @return The cache key of the script to be compiled/executed. For dynamic scripts this is the actual
|
||||
* script source code. For indexed scripts this is the id used in the request. For on disk scripts
|
||||
* this is the file name.
|
||||
*/
|
||||
public String getScript() {
|
||||
return script;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for getting the type.
|
||||
* @return The type of script -- dynamic, indexed, or file.
|
||||
*/
|
||||
public ScriptType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method for getting the parameters.
|
||||
* @return The map of parameters the script will be executed with.
|
||||
*/
|
||||
public Map<String, Object> getParams() {
|
||||
return params;
|
||||
}
|
||||
}
|
|
@ -35,13 +35,11 @@ import org.elasticsearch.action.delete.DeleteResponse;
|
|||
import org.elasticsearch.action.get.GetRequest;
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.indexedscripts.delete.DeleteIndexedScriptRequest;
|
||||
import org.elasticsearch.action.indexedscripts.get.GetIndexedScriptRequest;
|
||||
import org.elasticsearch.action.indexedscripts.put.PutIndexedScriptRequest;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.ParseField;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.bytes.BytesReference;
|
||||
|
@ -74,7 +72,6 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -220,57 +217,67 @@ public class ScriptService extends AbstractComponent implements Closeable {
|
|||
/**
|
||||
* Checks if a script can be executed and compiles it if needed, or returns the previously compiled and cached script.
|
||||
*/
|
||||
public CompiledScript compile(String lang, String script, ScriptType scriptType, ScriptContext scriptContext) {
|
||||
assert script != null;
|
||||
assert scriptType != null;
|
||||
assert scriptContext != null;
|
||||
public CompiledScript compile(Script script, ScriptContext scriptContext) {
|
||||
if (script == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("The parameter script (Script) must not be null.");
|
||||
}
|
||||
if (scriptContext == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("The parameter scriptContext (ScriptContext) must not be null.");
|
||||
}
|
||||
|
||||
String lang = script.getLang();
|
||||
|
||||
if (lang == null) {
|
||||
lang = defaultLang;
|
||||
}
|
||||
|
||||
ScriptEngineService scriptEngineService = getScriptEngineServiceForLang(lang);
|
||||
if (canExecuteScript(lang, scriptEngineService, scriptType, scriptContext) == false) {
|
||||
throw new ScriptException("scripts of type [" + scriptType + "], operation [" + scriptContext.getKey() + "] and lang [" + lang + "] are disabled");
|
||||
if (canExecuteScript(lang, scriptEngineService, script.getType(), scriptContext) == false) {
|
||||
throw new ScriptException("scripts of type [" + script.getType() + "], operation [" + scriptContext.getKey() + "] and lang [" + lang + "] are disabled");
|
||||
}
|
||||
return compileInternal(lang, script, scriptType);
|
||||
return compileInternal(script);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles a script straight-away, or returns the previously compiled and cached script, without checking if it can be executed based on settings.
|
||||
*/
|
||||
public CompiledScript compileInternal(String lang, final String scriptOrId, final ScriptType scriptType) {
|
||||
assert scriptOrId != null;
|
||||
assert scriptType != null;
|
||||
public CompiledScript compileInternal(Script script) {
|
||||
if (script == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("The parameter script (Script) must not be null.");
|
||||
}
|
||||
|
||||
String lang = script.getLang();
|
||||
|
||||
if (lang == null) {
|
||||
lang = defaultLang;
|
||||
}
|
||||
if (logger.isTraceEnabled()) {
|
||||
logger.trace("Compiling lang: [{}] type: [{}] script: {}", lang, scriptType, scriptOrId);
|
||||
logger.trace("Compiling lang: [{}] type: [{}] script: {}", lang, script.getType(), script.getScript());
|
||||
}
|
||||
|
||||
ScriptEngineService scriptEngineService = getScriptEngineServiceForLang(lang);
|
||||
CacheKey cacheKey = newCacheKey(scriptEngineService, scriptOrId);
|
||||
CacheKey cacheKey = newCacheKey(scriptEngineService, script.getScript());
|
||||
|
||||
if (scriptType == ScriptType.FILE) {
|
||||
if (script.getType() == ScriptType.FILE) {
|
||||
CompiledScript compiled = staticCache.get(cacheKey); //On disk scripts will be loaded into the staticCache by the listener
|
||||
if (compiled == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("Unable to find on disk script " + scriptOrId);
|
||||
throw new ElasticsearchIllegalArgumentException("Unable to find on disk script " + script.getScript());
|
||||
}
|
||||
return compiled;
|
||||
}
|
||||
|
||||
String script = scriptOrId;
|
||||
if (scriptType == ScriptType.INDEXED) {
|
||||
final IndexedScript indexedScript = new IndexedScript(lang, scriptOrId);
|
||||
script = getScriptFromIndex(indexedScript.lang, indexedScript.id);
|
||||
cacheKey = newCacheKey(scriptEngineService, script);
|
||||
String code = script.getScript();
|
||||
|
||||
if (script.getType() == ScriptType.INDEXED) {
|
||||
final IndexedScript indexedScript = new IndexedScript(lang, script.getScript());
|
||||
code = getScriptFromIndex(indexedScript.lang, indexedScript.id);
|
||||
cacheKey = newCacheKey(scriptEngineService, code);
|
||||
}
|
||||
|
||||
CompiledScript compiled = cache.getIfPresent(cacheKey);
|
||||
if (compiled == null) {
|
||||
//Either an un-cached inline script or an indexed script
|
||||
compiled = new CompiledScript(lang, scriptEngineService.compile(script));
|
||||
compiled = new CompiledScript(lang, scriptEngineService.compile(code));
|
||||
//Since the cache key is the script content itself we don't need to
|
||||
//invalidate/check the cache if an indexed script changes.
|
||||
cache.put(cacheKey, compiled);
|
||||
|
@ -320,7 +327,7 @@ public class ScriptService extends AbstractComponent implements Closeable {
|
|||
//we don't know yet what the script will be used for, but if all of the operations for this lang with
|
||||
//indexed scripts are disabled, it makes no sense to even compile it and cache it.
|
||||
if (isAnyScriptContextEnabled(scriptLang, getScriptEngineServiceForLang(scriptLang), ScriptType.INDEXED)) {
|
||||
CompiledScript compiledScript = compileInternal(scriptLang, context.template(), ScriptType.INLINE);
|
||||
CompiledScript compiledScript = compileInternal(new Script(scriptLang, context.template(), ScriptType.INLINE, null));
|
||||
if (compiledScript == null) {
|
||||
throw new ElasticsearchIllegalArgumentException("Unable to parse [" + context.template() +
|
||||
"] lang [" + scriptLang + "] (ScriptService.compile returned null)");
|
||||
|
@ -390,8 +397,8 @@ public class ScriptService extends AbstractComponent implements Closeable {
|
|||
/**
|
||||
* Compiles (or retrieves from cache) and executes the provided script
|
||||
*/
|
||||
public ExecutableScript executable(String lang, String script, ScriptType scriptType, ScriptContext scriptContext, Map<String, Object> vars) {
|
||||
return executable(compile(lang, script, scriptType, scriptContext), vars);
|
||||
public ExecutableScript executable(Script script, ScriptContext scriptContext) {
|
||||
return executable(compile(script, scriptContext), script.getParams());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -404,9 +411,9 @@ public class ScriptService extends AbstractComponent implements Closeable {
|
|||
/**
|
||||
* Compiles (or retrieves from cache) and executes the provided search script
|
||||
*/
|
||||
public SearchScript search(SearchLookup lookup, String lang, String script, ScriptType scriptType, ScriptContext scriptContext, @Nullable Map<String, Object> vars) {
|
||||
CompiledScript compiledScript = compile(lang, script, scriptType, scriptContext);
|
||||
return getScriptEngineServiceForLang(compiledScript.lang()).search(compiledScript.compiled(), lookup, vars);
|
||||
public SearchScript search(SearchLookup lookup, Script script, ScriptContext scriptContext) {
|
||||
CompiledScript compiledScript = compile(script, scriptContext);
|
||||
return getScriptEngineServiceForLang(compiledScript.lang()).search(compiledScript.compiled(), lookup, script.getParams());
|
||||
}
|
||||
|
||||
private boolean isAnyScriptContextEnabled(String lang, ScriptEngineService scriptEngineService, ScriptType scriptType) {
|
||||
|
|
|
@ -71,9 +71,9 @@ import org.elasticsearch.indices.IndicesWarmer.TerminationHandle;
|
|||
import org.elasticsearch.indices.IndicesWarmer.WarmerContext;
|
||||
import org.elasticsearch.indices.cache.query.IndicesQueryCache;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.ScriptContextRegistry;
|
||||
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
|
||||
import org.elasticsearch.search.dfs.CachedDfSource;
|
||||
import org.elasticsearch.search.dfs.DfsPhase;
|
||||
|
@ -653,7 +653,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
|
|||
|
||||
final ExecutableScript executable;
|
||||
if (hasLength(request.templateName())) {
|
||||
executable = this.scriptService.executable(MustacheScriptEngineService.NAME, request.templateName(), request.templateType(), ScriptContext.Standard.SEARCH, request.templateParams());
|
||||
executable = this.scriptService.executable(new Script(MustacheScriptEngineService.NAME, request.templateName(), request.templateType(), request.templateParams()), ScriptContext.Standard.SEARCH);
|
||||
} else {
|
||||
if (!hasLength(request.templateSource())) {
|
||||
return;
|
||||
|
@ -693,7 +693,7 @@ public class SearchService extends AbstractLifecycleComponent<SearchService> {
|
|||
if (!hasLength(templateContext.template())) {
|
||||
throw new ElasticsearchParseException("Template must have [template] field configured");
|
||||
}
|
||||
executable = this.scriptService.executable(MustacheScriptEngineService.NAME, templateContext.template(), templateContext.scriptType(), ScriptContext.Standard.SEARCH, templateContext.params());
|
||||
executable = this.scriptService.executable(new Script(MustacheScriptEngineService.NAME, templateContext.template(), templateContext.scriptType(), templateContext.params()), ScriptContext.Standard.SEARCH);
|
||||
}
|
||||
|
||||
BytesReference processedQuery = (BytesReference) executable.run();
|
||||
|
|
|
@ -83,7 +83,7 @@ public class ScriptHeuristic extends SignificanceHeuristic {
|
|||
}
|
||||
|
||||
public void initialize(InternalAggregation.ReduceContext context) {
|
||||
script = context.scriptService().executable(scriptLang, scriptString, scriptType, ScriptContext.Standard.AGGS, params);
|
||||
script = context.scriptService().executable(new Script(scriptLang, scriptString, scriptType, params), ScriptContext.Standard.AGGS);
|
||||
script.setNextVar("_subset_freq", subsetDfHolder);
|
||||
script.setNextVar("_subset_size", subsetSizeHolder);
|
||||
script.setNextVar("_superset_freq", supersetDfHolder);
|
||||
|
@ -168,7 +168,7 @@ public class ScriptHeuristic extends SignificanceHeuristic {
|
|||
}
|
||||
ExecutableScript searchScript;
|
||||
try {
|
||||
searchScript = scriptService.executable(scriptLang, script, scriptType, ScriptContext.Standard.AGGS, params);
|
||||
searchScript = scriptService.executable(new Script(scriptLang, script, scriptType, params), ScriptContext.Standard.AGGS);
|
||||
} catch (Exception e) {
|
||||
throw new ElasticsearchParseException("The script [" + script + "] could not be loaded", e);
|
||||
}
|
||||
|
|
|
@ -24,6 +24,7 @@ import org.elasticsearch.common.io.stream.StreamInput;
|
|||
import org.elasticsearch.common.io.stream.StreamOutput;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.script.ExecutableScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.AggregationStreams;
|
||||
|
@ -98,8 +99,8 @@ public class InternalScriptedMetric extends InternalMetricsAggregation implement
|
|||
params = new HashMap<>();
|
||||
}
|
||||
params.put("_aggs", aggregationObjects);
|
||||
ExecutableScript script = reduceContext.scriptService().executable(firstAggregation.scriptLang, firstAggregation.reduceScript,
|
||||
firstAggregation.scriptType, ScriptContext.Standard.AGGS, params);
|
||||
ExecutableScript script = reduceContext.scriptService().executable(new Script(firstAggregation.scriptLang, firstAggregation.reduceScript,
|
||||
firstAggregation.scriptType, params), ScriptContext.Standard.AGGS);
|
||||
aggregation = script.run();
|
||||
} else {
|
||||
aggregation = aggregationObjects;
|
||||
|
|
|
@ -74,11 +74,11 @@ public class ScriptedMetricAggregator extends MetricsAggregator {
|
|||
}
|
||||
ScriptService scriptService = context.searchContext().scriptService();
|
||||
if (initScript != null) {
|
||||
scriptService.executable(scriptLang, initScript, initScriptType, ScriptContext.Standard.AGGS, this.params).run();
|
||||
scriptService.executable(new Script(scriptLang, initScript, initScriptType, this.params), ScriptContext.Standard.AGGS).run();
|
||||
}
|
||||
this.mapScript = scriptService.search(context.searchContext().lookup(), scriptLang, mapScript, mapScriptType, ScriptContext.Standard.AGGS, this.params);
|
||||
this.mapScript = scriptService.search(context.searchContext().lookup(), new Script(scriptLang, mapScript, mapScriptType, this.params), ScriptContext.Standard.AGGS);
|
||||
if (combineScript != null) {
|
||||
this.combineScript = scriptService.executable(scriptLang, combineScript, combineScriptType, ScriptContext.Standard.AGGS, this.params);
|
||||
this.combineScript = scriptService.executable(new Script(scriptLang, combineScript, combineScriptType, this.params), ScriptContext.Standard.AGGS);
|
||||
} else {
|
||||
this.combineScript = null;
|
||||
}
|
||||
|
|
|
@ -186,7 +186,7 @@ public class ValuesSourceParser<VS extends ValuesSource> {
|
|||
}
|
||||
|
||||
private SearchScript createScript() {
|
||||
return input.script == null ? null : context.scriptService().search(context.lookup(), input.lang, input.script, input.scriptType, ScriptContext.Standard.AGGS, input.params);
|
||||
return input.script == null ? null : context.scriptService().search(context.lookup(), new Script(input.lang, input.script, input.scriptType, input.params), ScriptContext.Standard.AGGS);
|
||||
}
|
||||
|
||||
private static ValueFormat resolveFormat(@Nullable String format, @Nullable ValueType valueType) {
|
||||
|
|
|
@ -76,7 +76,7 @@ public class ScriptFieldsParseElement implements SearchParseElement {
|
|||
script = scriptValue.script();
|
||||
scriptType = scriptValue.scriptType();
|
||||
}
|
||||
SearchScript searchScript = context.scriptService().search(context.lookup(), scriptParameterParser.lang(), script, scriptType, ScriptContext.Standard.SEARCH, params);
|
||||
SearchScript searchScript = context.scriptService().search(context.lookup(), new Script(scriptParameterParser.lang(), script, scriptType, params), ScriptContext.Standard.SEARCH);
|
||||
context.scriptFields().add(new ScriptFieldsContext.ScriptField(fieldName, searchScript, ignoreException));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ import org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparator
|
|||
import org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource;
|
||||
import org.elasticsearch.index.query.support.NestedInnerQueryParseSupport;
|
||||
import org.elasticsearch.script.LeafSearchScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptService;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
|
@ -122,7 +123,7 @@ public class ScriptSortParser implements SortParser {
|
|||
if (type == null) {
|
||||
throw new SearchParseException(context, "_script sorting requires setting the type of the script");
|
||||
}
|
||||
final SearchScript searchScript = context.scriptService().search(context.lookup(), scriptLang, script, scriptType, ScriptContext.Standard.SEARCH, params);
|
||||
final SearchScript searchScript = context.scriptService().search(context.lookup(), new Script(scriptLang, script, scriptType, params), ScriptContext.Standard.SEARCH);
|
||||
|
||||
if (STRING_SORT_TYPE.equals(type) && (sortMode == MultiValueMode.SUM || sortMode == MultiValueMode.AVG)) {
|
||||
throw new SearchParseException(context, "type [string] doesn't support mode [" + sortMode + "]");
|
||||
|
|
|
@ -30,6 +30,7 @@ import org.elasticsearch.index.analysis.ShingleTokenFilterFactory;
|
|||
import org.elasticsearch.index.mapper.FieldMapper;
|
||||
import org.elasticsearch.index.mapper.MapperService;
|
||||
import org.elasticsearch.script.CompiledScript;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptContext;
|
||||
import org.elasticsearch.script.ScriptService.ScriptType;
|
||||
import org.elasticsearch.script.mustache.MustacheScriptEngineService;
|
||||
|
@ -153,7 +154,7 @@ public final class PhraseSuggestParser implements SuggestContextParser {
|
|||
if (suggestion.getCollateQueryScript() != null) {
|
||||
throw new ElasticsearchIllegalArgumentException("suggester[phrase][collate] query already set, doesn't support additional [" + fieldName + "]");
|
||||
}
|
||||
CompiledScript compiledScript = suggester.scriptService().compile(MustacheScriptEngineService.NAME, templateNameOrTemplateContent, ScriptType.INLINE, ScriptContext.Standard.SEARCH);
|
||||
CompiledScript compiledScript = suggester.scriptService().compile(new Script(MustacheScriptEngineService.NAME, templateNameOrTemplateContent, ScriptType.INLINE, null), ScriptContext.Standard.SEARCH);
|
||||
if ("query".equals(fieldName)) {
|
||||
suggestion.setCollateQueryScript(compiledScript);
|
||||
} else {
|
||||
|
|
|
@ -56,7 +56,7 @@ public class CustomScriptContextTests extends ElasticsearchIntegrationTest {
|
|||
for (String lang : LANG_SET) {
|
||||
for (ScriptService.ScriptType scriptType : ScriptService.ScriptType.values()) {
|
||||
try {
|
||||
scriptService.compile(lang, "test", scriptType, new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
|
||||
scriptService.compile(new Script(lang, "test", scriptType, null), new ScriptContext.Plugin(PLUGIN_NAME, "custom_globally_disabled_op"));
|
||||
fail("script compilation should have been rejected");
|
||||
} catch(ScriptException e) {
|
||||
assertThat(e.getMessage(), containsString("scripts of type [" + scriptType + "], operation [" + PLUGIN_NAME + "_custom_globally_disabled_op] and lang [" + lang + "] are disabled"));
|
||||
|
@ -65,20 +65,20 @@ public class CustomScriptContextTests extends ElasticsearchIntegrationTest {
|
|||
}
|
||||
|
||||
try {
|
||||
scriptService.compile("expression", "1", ScriptService.ScriptType.INLINE, new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"));
|
||||
scriptService.compile(new Script("expression", "1", ScriptService.ScriptType.INLINE, null), new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"));
|
||||
fail("script compilation should have been rejected");
|
||||
} catch(ScriptException e) {
|
||||
assertThat(e.getMessage(), containsString("scripts of type [inline], operation [" + PLUGIN_NAME + "_custom_exp_disabled_op] and lang [expression] are disabled"));
|
||||
}
|
||||
|
||||
CompiledScript compiledScript = scriptService.compile("expression", "1", ScriptService.ScriptType.INLINE, randomFrom(ScriptContext.Standard.values()));
|
||||
CompiledScript compiledScript = scriptService.compile(new Script("expression", "1", ScriptService.ScriptType.INLINE, null), randomFrom(ScriptContext.Standard.values()));
|
||||
assertThat(compiledScript, notNullValue());
|
||||
|
||||
compiledScript = scriptService.compile("mustache", "1", ScriptService.ScriptType.INLINE, new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"));
|
||||
compiledScript = scriptService.compile(new Script("mustache", "1", ScriptService.ScriptType.INLINE, null), new ScriptContext.Plugin(PLUGIN_NAME, "custom_exp_disabled_op"));
|
||||
assertThat(compiledScript, notNullValue());
|
||||
|
||||
for (String lang : LANG_SET) {
|
||||
compiledScript = scriptService.compile(lang, "1", ScriptService.ScriptType.INLINE, new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"));
|
||||
compiledScript = scriptService.compile(new Script(lang, "1", ScriptService.ScriptType.INLINE, null), new ScriptContext.Plugin(PLUGIN_NAME, "custom_op"));
|
||||
assertThat(compiledScript, notNullValue());
|
||||
}
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ public class CustomScriptContextTests extends ElasticsearchIntegrationTest {
|
|||
public void testCompileNonRegisteredPluginContext() {
|
||||
ScriptService scriptService = internalCluster().getInstance(ScriptService.class);
|
||||
try {
|
||||
scriptService.compile(randomFrom(LANG_SET.toArray(new String[LANG_SET.size()])), "test", randomFrom(ScriptService.ScriptType.values()), new ScriptContext.Plugin("test", "unknown"));
|
||||
scriptService.compile(new Script(randomFrom(LANG_SET.toArray(new String[LANG_SET.size()])), "test", randomFrom(ScriptService.ScriptType.values()), null), new ScriptContext.Plugin("test", "unknown"));
|
||||
fail("script compilation should have been rejected");
|
||||
} catch(ElasticsearchIllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), containsString("script context [test_unknown] not supported"));
|
||||
|
@ -98,7 +98,7 @@ public class CustomScriptContextTests extends ElasticsearchIntegrationTest {
|
|||
public void testCompileNonRegisteredScriptContext() {
|
||||
ScriptService scriptService = internalCluster().getInstance(ScriptService.class);
|
||||
try {
|
||||
scriptService.compile(randomFrom(LANG_SET.toArray(new String[LANG_SET.size()])), "test", randomFrom(ScriptService.ScriptType.values()), new ScriptContext() {
|
||||
scriptService.compile(new Script(randomFrom(LANG_SET.toArray(new String[LANG_SET.size()])), "test", randomFrom(ScriptService.ScriptType.values()), null), new ScriptContext() {
|
||||
@Override
|
||||
public String getKey() {
|
||||
return "test";
|
||||
|
|
|
@ -59,7 +59,7 @@ public class NativeScriptTests extends ElasticsearchTestCase {
|
|||
|
||||
ScriptService scriptService = injector.getInstance(ScriptService.class);
|
||||
|
||||
ExecutableScript executable = scriptService.executable(NativeScriptEngineService.NAME, "my", ScriptType.INLINE, ScriptContext.Standard.SEARCH, null);
|
||||
ExecutableScript executable = scriptService.executable(new Script(NativeScriptEngineService.NAME, "my", ScriptType.INLINE, null), ScriptContext.Standard.SEARCH);
|
||||
assertThat(executable.run().toString(), equalTo("test"));
|
||||
terminate(injector.getInstance(ThreadPool.class));
|
||||
}
|
||||
|
@ -84,7 +84,7 @@ public class NativeScriptTests extends ElasticsearchTestCase {
|
|||
ScriptService scriptService = new ScriptService(settings, environment, scriptEngineServices, resourceWatcherService, new NodeSettingsService(settings), scriptContextRegistry);
|
||||
|
||||
for (ScriptContext scriptContext : scriptContextRegistry.scriptContexts()) {
|
||||
assertThat(scriptService.compile(NativeScriptEngineService.NAME, "my", ScriptType.INLINE, scriptContext), notNullValue());
|
||||
assertThat(scriptService.compile(new Script(NativeScriptEngineService.NAME, "my", ScriptType.INLINE, null), scriptContext), notNullValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -133,7 +133,7 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
resourceWatcherService.notifyNow();
|
||||
|
||||
logger.info("--> verify that file with extension was correctly processed");
|
||||
CompiledScript compiledScript = scriptService.compile("test", "test_script", ScriptType.FILE, ScriptContext.Standard.SEARCH);
|
||||
CompiledScript compiledScript = scriptService.compile(new Script("test", "test_script", ScriptType.FILE, null), ScriptContext.Standard.SEARCH);
|
||||
assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file"));
|
||||
|
||||
logger.info("--> delete both files");
|
||||
|
@ -143,7 +143,7 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
|
||||
logger.info("--> verify that file with extension was correctly removed");
|
||||
try {
|
||||
scriptService.compile("test", "test_script", ScriptType.FILE, ScriptContext.Standard.SEARCH);
|
||||
scriptService.compile(new Script("test", "test_script", ScriptType.FILE, null), ScriptContext.Standard.SEARCH);
|
||||
fail("the script test_script should no longer exist");
|
||||
} catch (ElasticsearchIllegalArgumentException ex) {
|
||||
assertThat(ex.getMessage(), containsString("Unable to find on disk script test_script"));
|
||||
|
@ -154,17 +154,17 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
public void testScriptsSameNameDifferentLanguage() throws IOException {
|
||||
buildScriptService(ImmutableSettings.EMPTY);
|
||||
createFileScripts("groovy", "expression");
|
||||
CompiledScript groovyScript = scriptService.compile(GroovyScriptEngineService.NAME, "file_script", ScriptType.FILE, randomFrom(scriptContexts));
|
||||
CompiledScript groovyScript = scriptService.compile(new Script(GroovyScriptEngineService.NAME, "file_script", ScriptType.FILE, null), randomFrom(scriptContexts));
|
||||
assertThat(groovyScript.lang(), equalTo(GroovyScriptEngineService.NAME));
|
||||
CompiledScript expressionScript = scriptService.compile(ExpressionScriptEngineService.NAME, "file_script", ScriptType.FILE, randomFrom(scriptContexts));
|
||||
CompiledScript expressionScript = scriptService.compile(new Script(ExpressionScriptEngineService.NAME, "file_script", ScriptType.FILE, null), randomFrom(scriptContexts));
|
||||
assertThat(expressionScript.lang(), equalTo(ExpressionScriptEngineService.NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInlineScriptCompiledOnceMultipleLangAcronyms() throws IOException {
|
||||
buildScriptService(ImmutableSettings.EMPTY);
|
||||
CompiledScript compiledScript1 = scriptService.compile("test", "script", ScriptType.INLINE, randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript2 = scriptService.compile("test2", "script", ScriptType.INLINE, randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript1 = scriptService.compile(new Script("test", "script", ScriptType.INLINE, null), randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript2 = scriptService.compile(new Script("test2", "script", ScriptType.INLINE, null), randomFrom(scriptContexts));
|
||||
assertThat(compiledScript1, sameInstance(compiledScript2));
|
||||
}
|
||||
|
||||
|
@ -172,8 +172,8 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
public void testFileScriptCompiledOnceMultipleLangAcronyms() throws IOException {
|
||||
buildScriptService(ImmutableSettings.EMPTY);
|
||||
createFileScripts("test");
|
||||
CompiledScript compiledScript1 = scriptService.compile("test", "file_script", ScriptType.FILE, randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript2 = scriptService.compile("test2", "file_script", ScriptType.FILE, randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript1 = scriptService.compile(new Script("test", "file_script", ScriptType.FILE, null), randomFrom(scriptContexts));
|
||||
CompiledScript compiledScript2 = scriptService.compile(new Script("test2", "file_script", ScriptType.FILE, null), randomFrom(scriptContexts));
|
||||
assertThat(compiledScript1, sameInstance(compiledScript2));
|
||||
}
|
||||
|
||||
|
@ -350,7 +350,7 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
for (ScriptEngineService scriptEngineService : scriptEngineServices) {
|
||||
for (String type : scriptEngineService.types()) {
|
||||
try {
|
||||
scriptService.compile(type, "test", randomFrom(ScriptType.values()), new ScriptContext.Plugin(pluginName, unknownContext));
|
||||
scriptService.compile(new Script(type, "test", randomFrom(ScriptType.values()), null), new ScriptContext.Plugin(pluginName, unknownContext));
|
||||
fail("script compilation should have been rejected");
|
||||
} catch(ElasticsearchIllegalArgumentException e) {
|
||||
assertThat(e.getMessage(), containsString("script context [" + pluginName + "_" + unknownContext + "] not supported"));
|
||||
|
@ -369,7 +369,7 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
|
||||
private void assertCompileRejected(String lang, String script, ScriptType scriptType, ScriptContext scriptContext) {
|
||||
try {
|
||||
scriptService.compile(lang, script, scriptType, scriptContext);
|
||||
scriptService.compile(new Script(lang, script, scriptType, null), scriptContext);
|
||||
fail("compile should have been rejected for lang [" + lang + "], script_type [" + scriptType + "], scripted_op [" + scriptContext + "]");
|
||||
} catch(ScriptException e) {
|
||||
//all good
|
||||
|
@ -377,7 +377,7 @@ public class ScriptServiceTests extends ElasticsearchTestCase {
|
|||
}
|
||||
|
||||
private void assertCompileAccepted(String lang, String script, ScriptType scriptType, ScriptContext scriptContext) {
|
||||
assertThat(scriptService.compile(lang, script, scriptType, scriptContext), notNullValue());
|
||||
assertThat(scriptService.compile(new Script(lang, script, scriptType, null), scriptContext), notNullValue());
|
||||
}
|
||||
|
||||
public static class TestEngineService implements ScriptEngineService {
|
||||
|
|
Loading…
Reference in New Issue