diff options
author | Ryan Ernst <ryan@iernst.net> | 2017-05-24 14:29:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-24 14:29:02 -0700 |
commit | 1daacd97b07b601a8a27a8bf64aaf132d2a1931d (patch) | |
tree | 5321e4d094f9c435f23947134fa71a523e5c65ea /core/src/main/java/org/elasticsearch/search/aggregations/metrics | |
parent | 0ddd21942315092d76b43786b45c5ebb015849a5 (diff) |
Scripting: Add instance and compiled classes to script contexts (#24868)
This commit modifies the compile method of ScriptService to be context
aware. The ScriptContext is now a generic class which contains both the
instance type and compiled type for a script. Instance type may be
stateful (for example, pre loading field information for the index a
script will execute on, like in expressions), while the compiled type is
stateless and used to construct instance type instances. This change is
only a first step to cutover ScriptService to the new paradigm. It only
converts callers to the script service, and has a small shim to wrap
compilation from the script engines to support the current two fixed
instance types, SearchScript and ExecutableScript.
Diffstat (limited to 'core/src/main/java/org/elasticsearch/search/aggregations/metrics')
2 files changed, 6 insertions, 8 deletions
diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/InternalScriptedMetric.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/InternalScriptedMetric.java index 2bedd16ebf..b0a4836a69 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/InternalScriptedMetric.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/InternalScriptedMetric.java @@ -22,7 +22,6 @@ package org.elasticsearch.search.aggregations.metrics.scripted; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.Script; import org.elasticsearch.script.ScriptContext; @@ -96,9 +95,9 @@ public class InternalScriptedMetric extends InternalAggregation implements Scrip if (firstAggregation.reduceScript.getParams() != null) { vars.putAll(firstAggregation.reduceScript.getParams()); } - CompiledScript compiledScript = reduceContext.scriptService().compile( - firstAggregation.reduceScript, ScriptContext.AGGS); - ExecutableScript script = reduceContext.scriptService().executable(compiledScript, vars); + ExecutableScript.Compiled compiled = reduceContext.scriptService().compile( + firstAggregation.reduceScript, ScriptContext.AGGS_EXECUTABLE); + ExecutableScript script = compiled.newInstance(vars); aggregation = Collections.singletonList(script.run()); } else if (reduceContext.isFinalReduce()) { aggregation = Collections.singletonList(aggregationObjects); diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregationBuilder.java b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregationBuilder.java index 6d8c297eeb..6865d6a211 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregationBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregationBuilder.java @@ -186,15 +186,14 @@ public class ScriptedMetricAggregationBuilder extends AbstractAggregationBuilder QueryShardContext queryShardContext = context.getQueryShardContext(); Function<Map<String, Object>, ExecutableScript> executableInitScript; if (initScript != null) { - executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.AGGS); + executableInitScript = queryShardContext.getLazyExecutableScript(initScript, ScriptContext.AGGS_EXECUTABLE); } else { executableInitScript = (p) -> null; } - Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript, - ScriptContext.AGGS); + Function<Map<String, Object>, SearchScript> searchMapScript = queryShardContext.getLazySearchScript(mapScript, ScriptContext.AGGS); Function<Map<String, Object>, ExecutableScript> executableCombineScript; if (combineScript != null) { - executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.AGGS); + executableCombineScript = queryShardContext.getLazyExecutableScript(combineScript, ScriptContext.AGGS_EXECUTABLE); } else { executableCombineScript = (p) -> null; } |