From 2ee4b464898d737024b3d72f145c6905b36fbe15 Mon Sep 17 00:00:00 2001 From: Armin Date: Sun, 15 Jul 2018 11:32:13 +0200 Subject: [PATCH 01/17] SCRIPTING: Move Aggregation Scripts to their own context --- .../expression/ExpressionScriptEngine.java | 45 ++++++++++++++++--- .../script/BucketAggregationScript.java | 39 ++++++++++++++++ .../script/ExecutableScript.java | 1 - .../elasticsearch/script/ScriptModule.java | 2 +- .../heuristics/ScriptHeuristic.java | 28 +++++++----- .../BucketScriptPipelineAggregator.java | 8 ++-- .../BucketSelectorPipelineAggregator.java | 9 ++-- .../script/MockScriptEngine.java | 8 ++++ 8 files changed, 112 insertions(+), 28 deletions(-) create mode 100644 server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index 1cde9c258b4f1..7139d454209e8 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -34,10 +34,11 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData; -import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType; import org.elasticsearch.index.mapper.DateFieldMapper; +import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.ClassPermission; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.FilterScript; @@ -54,6 +55,7 @@ import java.security.PrivilegedAction; import java.text.ParseException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -112,6 +114,8 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE } else if (context.instanceClazz.equals(ExecutableScript.class)) { ExecutableScript.Factory factory = (p) -> new ExpressionExecutableScript(expr, p); return context.factoryClazz.cast(factory); + } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { + return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); } else if (context.instanceClazz.equals(FilterScript.class)) { FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p); return context.factoryClazz.cast(factory); @@ -122,6 +126,37 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE throw new IllegalArgumentException("expression engine does not know how to handle script context [" + context.name + "]"); } + private static BucketAggregationScript.Factory newBucketAggregationScriptFactory(Expression expr) { + return () -> { + ReplaceableConstDoubleValues[] functionValuesArray = + new ReplaceableConstDoubleValues[expr.variables.length]; + Map functionValuesMap = new HashMap<>(); + for (int i = 0; i < expr.variables.length; ++i) { + functionValuesArray[i] = new ReplaceableConstDoubleValues(); + functionValuesMap.put(expr.variables[i], functionValuesArray[i]); + } + return new BucketAggregationScript() { + @Override + public Object execute(Map params) { + params.forEach((name, value) -> { + ReplaceableConstDoubleValues placeholder = functionValuesMap.get(name); + if (placeholder == null) { + throw new IllegalArgumentException("Error using " + expr + ". " + + "The variable [" + name + "] does not exist in the executable expressions script."); + } else if (value instanceof Number == false) { + throw new IllegalArgumentException("Error using " + expr + ". " + + "Executable expressions scripts can only process numbers." + + " The variable [" + name + "] is not a number."); + } else { + placeholder.setValue(((Number) value).doubleValue()); + } + }); + return expr.evaluate(functionValuesArray); + } + }; + }; + } + private SearchScript.LeafFactory newSearchScript(Expression expr, SearchLookup lookup, @Nullable Map vars) { MapperService mapper = lookup.doc().mapperService(); // NOTE: if we need to do anything complicated with bindings in the future, we can just extend Bindings, @@ -267,7 +302,7 @@ public void setDocument(int docid) { }; }; } - + private ScoreScript.LeafFactory newScoreScript(Expression expr, SearchLookup lookup, @Nullable Map vars) { SearchScript.LeafFactory searchLeafFactory = newSearchScript(expr, lookup, vars); return new ScoreScript.LeafFactory() { @@ -284,17 +319,17 @@ public ScoreScript newInstance(LeafReaderContext ctx) throws IOException { public double execute() { return script.runAsDouble(); } - + @Override public void setDocument(int docid) { script.setDocument(docid); } - + @Override public void setScorer(Scorer scorer) { script.setScorer(scorer); } - + @Override public double get_score() { return script.getScore(); diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java new file mode 100644 index 0000000000000..55057b70111e0 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java @@ -0,0 +1,39 @@ +/* + * 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; + +/** + * A script used in bucket aggregations. + */ +public abstract class BucketAggregationScript { + + public static final String[] PARAMETERS = { "params" }; + + public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_executable", Factory.class); + + public abstract Object execute(Map params); + + public interface Factory { + BucketAggregationScript newInstance(); + } + +} diff --git a/server/src/main/java/org/elasticsearch/script/ExecutableScript.java b/server/src/main/java/org/elasticsearch/script/ExecutableScript.java index 2f7a01c37980d..1bd4c31ebf349 100644 --- a/server/src/main/java/org/elasticsearch/script/ExecutableScript.java +++ b/server/src/main/java/org/elasticsearch/script/ExecutableScript.java @@ -48,6 +48,5 @@ interface Factory { ScriptContext CONTEXT = new ScriptContext<>("executable", Factory.class); // TODO: remove these once each has its own script interface - ScriptContext AGGS_CONTEXT = new ScriptContext<>("aggs_executable", Factory.class); ScriptContext UPDATE_CONTEXT = new ScriptContext<>("update", Factory.class); } diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index bf4bd9c57cef0..9b09a92255129 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -49,7 +49,7 @@ public class ScriptModule { SearchScript.SCRIPT_SORT_CONTEXT, SearchScript.TERMS_SET_QUERY_CONTEXT, ExecutableScript.CONTEXT, - ExecutableScript.AGGS_CONTEXT, + BucketAggregationScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index a2cbd49693a27..1d4dfbcb34410 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -28,12 +28,14 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; -import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; public class ScriptHeuristic extends SignificanceHeuristic { @@ -48,19 +50,21 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { private final LongAccessor supersetSizeHolder; private final LongAccessor subsetDfHolder; private final LongAccessor supersetDfHolder; - private final ExecutableScript executableScript; + private final BucketAggregationScript executableScript; + private final Map params = new HashMap<>(); - ExecutableScriptHeuristic(Script script, ExecutableScript executableScript){ + ExecutableScriptHeuristic(Script script, BucketAggregationScript executableScript) { super(script); subsetSizeHolder = new LongAccessor(); supersetSizeHolder = new LongAccessor(); subsetDfHolder = new LongAccessor(); supersetDfHolder = new LongAccessor(); this.executableScript = executableScript; - executableScript.setNextVar("_subset_freq", subsetDfHolder); - executableScript.setNextVar("_subset_size", subsetSizeHolder); - executableScript.setNextVar("_superset_freq", supersetDfHolder); - executableScript.setNextVar("_superset_size", supersetSizeHolder); + params.putAll(script.getParams()); + params.put("_subset_freq", subsetDfHolder); + params.put("_subset_size", subsetSizeHolder); + params.put("_superset_freq", supersetDfHolder); + params.put("_superset_size", supersetSizeHolder); } @Override @@ -69,7 +73,7 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long supersetSizeHolder.value = supersetSize; subsetDfHolder.value = subsetFreq; supersetDfHolder.value = supersetFreq; - return ((Number) executableScript.run()).doubleValue(); + return ((Number) executableScript.execute(params)).doubleValue(); } } @@ -91,15 +95,15 @@ public void writeTo(StreamOutput out) throws IOException { @Override public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { - ExecutableScript.Factory factory = context.scriptService().compile(script, ExecutableScript.AGGS_CONTEXT); - return new ExecutableScriptHeuristic(script, factory.newInstance(script.getParams())); + BucketAggregationScript.Factory factory = context.scriptService().compile(script, BucketAggregationScript.CONTEXT); + return new ExecutableScriptHeuristic(script, factory.newInstance()); } @Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); - ExecutableScript.Factory compiledScript = shardContext.getScriptService().compile(script, ExecutableScript.AGGS_CONTEXT); - return new ExecutableScriptHeuristic(script, compiledScript.newInstance(script.getParams())); + BucketAggregationScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregationScript.CONTEXT); + return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java index 42337fbce0f98..c9a2a5375b6b6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java @@ -21,7 +21,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; @@ -89,7 +89,8 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - ExecutableScript.Factory factory = reduceContext.scriptService().compile(script, ExecutableScript.AGGS_CONTEXT); + BucketAggregationScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregationScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); @@ -110,8 +111,7 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext if (skipBucket) { newBuckets.add(bucket); } else { - ExecutableScript executableScript = factory.newInstance(vars); - Object returned = executableScript.run(); + Object returned = executableScript.execute(vars); // no need to check for self references since only numbers are valid if (returned == null) { newBuckets.add(bucket); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index a54ad0ec21f39..6c71a0994f73c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -22,7 +22,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.ExecutableScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -82,7 +82,8 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - ExecutableScript.Factory factory = reduceContext.scriptService().compile(script, ExecutableScript.AGGS_CONTEXT); + BucketAggregationScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregationScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); @@ -95,9 +96,7 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); vars.put(varName, value); } - // TODO: can we use one instance of the script for all buckets? it should be stateless? - ExecutableScript executableScript = factory.newInstance(vars); - Object scriptReturnValue = executableScript.run(); + Object scriptReturnValue = executableScript.execute(vars); final boolean keepBucket; // TODO: WTF!!!!! if ("expression".equals(script.getLang())) { diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 8e40e4bcf1468..50de7a044aaac 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -96,6 +96,14 @@ public void execute(Map ctx) { } }; return context.factoryClazz.cast(factory); + } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { + BucketAggregationScript.Factory factory = () -> new BucketAggregationScript() { + @Override + public Object execute(Map vars) { + return script.apply(vars); + } + }; + return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { // TODO: need a better way to implement all these new contexts From de9b238cac556a3bcd5bd92f1384b0205807a66c Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 18 Jul 2018 12:34:13 +0200 Subject: [PATCH 02/17] CR: Make returns typed --- .../expression/ExpressionScriptEngine.java | 10 ++--- .../BucketAggregateToBooleanScript.java | 38 +++++++++++++++++++ ...ava => BucketAggregateToDoubleScript.java} | 11 +++--- .../elasticsearch/script/ScriptModule.java | 2 +- .../heuristics/ScriptHeuristic.java | 10 ++--- .../BucketScriptPipelineAggregator.java | 32 ++++++---------- .../BucketSelectorPipelineAggregator.java | 7 ++-- .../script/MockScriptEngine.java | 8 ++-- 8 files changed, 73 insertions(+), 45 deletions(-) create mode 100644 server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java rename server/src/main/java/org/elasticsearch/script/{BucketAggregationScript.java => BucketAggregateToDoubleScript.java} (77%) diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index 7139d454209e8..fc01eb5f6d593 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -38,7 +38,7 @@ import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.script.BucketAggregationScript; +import org.elasticsearch.script.BucketAggregateToDoubleScript; import org.elasticsearch.script.ClassPermission; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.FilterScript; @@ -114,7 +114,7 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE } else if (context.instanceClazz.equals(ExecutableScript.class)) { ExecutableScript.Factory factory = (p) -> new ExpressionExecutableScript(expr, p); return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { + } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); } else if (context.instanceClazz.equals(FilterScript.class)) { FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p); @@ -126,7 +126,7 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE throw new IllegalArgumentException("expression engine does not know how to handle script context [" + context.name + "]"); } - private static BucketAggregationScript.Factory newBucketAggregationScriptFactory(Expression expr) { + private static BucketAggregateToDoubleScript.Factory newBucketAggregationScriptFactory(Expression expr) { return () -> { ReplaceableConstDoubleValues[] functionValuesArray = new ReplaceableConstDoubleValues[expr.variables.length]; @@ -135,9 +135,9 @@ private static BucketAggregationScript.Factory newBucketAggregationScriptFactory functionValuesArray[i] = new ReplaceableConstDoubleValues(); functionValuesMap.put(expr.variables[i], functionValuesArray[i]); } - return new BucketAggregationScript() { + return new BucketAggregateToDoubleScript() { @Override - public Object execute(Map params) { + public double execute(Map params) { params.forEach((name, value) -> { ReplaceableConstDoubleValues placeholder = functionValuesMap.get(name); if (placeholder == null) { diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java new file mode 100644 index 0000000000000..6e4ee198cd851 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java @@ -0,0 +1,38 @@ +/* + * 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; + +/** + * A script used in bucket aggregations that returns a {@code boolean} value. + */ +public abstract class BucketAggregateToBooleanScript { + + public static final String[] PARAMETERS = { "params" }; + + public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); + + public abstract boolean execute(Map params); + + public interface Factory { + BucketAggregateToBooleanScript newInstance(); + } +} diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java similarity index 77% rename from server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java rename to server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java index 55057b70111e0..91df4254f8451 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java @@ -22,18 +22,17 @@ import java.util.Map; /** - * A script used in bucket aggregations. + * A script used in bucket aggregations that returns a {@code double} value. */ -public abstract class BucketAggregationScript { +public abstract class BucketAggregateToDoubleScript { public static final String[] PARAMETERS = { "params" }; - public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_executable", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); - public abstract Object execute(Map params); + public abstract double execute(Map params); public interface Factory { - BucketAggregationScript newInstance(); + BucketAggregateToDoubleScript newInstance(); } - } diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index 9b09a92255129..f24c8419bff61 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -49,7 +49,7 @@ public class ScriptModule { SearchScript.SCRIPT_SORT_CONTEXT, SearchScript.TERMS_SET_QUERY_CONTEXT, ExecutableScript.CONTEXT, - BucketAggregationScript.CONTEXT, + BucketAggregateToDoubleScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index 1d4dfbcb34410..b7088cac98425 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; -import org.elasticsearch.script.BucketAggregationScript; +import org.elasticsearch.script.BucketAggregateToDoubleScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.internal.SearchContext; @@ -50,10 +50,10 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { private final LongAccessor supersetSizeHolder; private final LongAccessor subsetDfHolder; private final LongAccessor supersetDfHolder; - private final BucketAggregationScript executableScript; + private final BucketAggregateToDoubleScript executableScript; private final Map params = new HashMap<>(); - ExecutableScriptHeuristic(Script script, BucketAggregationScript executableScript) { + ExecutableScriptHeuristic(Script script, BucketAggregateToDoubleScript executableScript) { super(script); subsetSizeHolder = new LongAccessor(); supersetSizeHolder = new LongAccessor(); @@ -95,14 +95,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { - BucketAggregationScript.Factory factory = context.scriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregateToDoubleScript.Factory factory = context.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); return new ExecutableScriptHeuristic(script, factory.newInstance()); } @Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); - BucketAggregationScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregateToDoubleScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java index c9a2a5375b6b6..5a4a585bd186a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java @@ -21,10 +21,9 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.BucketAggregationScript; +import org.elasticsearch.script.BucketAggregateToDoubleScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.DocValueFormat; -import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; import org.elasticsearch.search.aggregations.InternalAggregations; @@ -89,8 +88,9 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - BucketAggregationScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); - BucketAggregationScript executableScript = factory.newInstance(); + BucketAggregateToDoubleScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); + BucketAggregateToDoubleScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); @@ -111,23 +111,13 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext if (skipBucket) { newBuckets.add(bucket); } else { - Object returned = executableScript.execute(vars); - // no need to check for self references since only numbers are valid - if (returned == null) { - newBuckets.add(bucket); - } else { - if ((returned instanceof Number) == false) { - throw new AggregationExecutionException("series_arithmetic script for reducer [" + name() - + "] must return a Number"); - } - final List aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map( - (p) -> (InternalAggregation) p).collect(Collectors.toList()); - aggs.add(new InternalSimpleValue(name(), ((Number) returned).doubleValue(), formatter, - new ArrayList<>(), metaData())); - InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), - bucket); - newBuckets.add(newBucket); - } + double returned = executableScript.execute(vars); + final List aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map( + (p) -> (InternalAggregation) p).collect(Collectors.toList()); + aggs.add(new InternalSimpleValue(name(), returned, formatter, new ArrayList<>(), metaData())); + InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), + bucket); + newBuckets.add(newBucket); } } return originalAgg.create(newBuckets); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index 6c71a0994f73c..8cd6a2f94b05c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -22,7 +22,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.BucketAggregationScript; +import org.elasticsearch.script.BucketAggregateToDoubleScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -82,8 +82,9 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - BucketAggregationScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); - BucketAggregationScript executableScript = factory.newInstance(); + BucketAggregateToDoubleScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); + BucketAggregateToDoubleScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 50de7a044aaac..41acc7077f5b6 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -96,11 +96,11 @@ public void execute(Map ctx) { } }; return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { - BucketAggregationScript.Factory factory = () -> new BucketAggregationScript() { + } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { + BucketAggregateToDoubleScript.Factory factory = () -> new BucketAggregateToDoubleScript() { @Override - public Object execute(Map vars) { - return script.apply(vars); + public double execute(Map vars) { + return (double) script.apply(vars); } }; return context.factoryClazz.cast(factory); From 2ac53ab9659b5c47276f8671928afcb400dc459a Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 18 Jul 2018 13:33:36 +0200 Subject: [PATCH 03/17] CR: Make returns typed --- .../BucketAggregateToBooleanScript.java | 10 ++-- .../script/BucketAggregateToDoubleScript.java | 10 ++-- .../elasticsearch/script/ScriptModule.java | 1 + .../heuristics/ScriptHeuristic.java | 2 +- .../BucketSelectorPipelineAggregator.java | 56 +++++++++++-------- .../script/MockScriptEngine.java | 8 +++ 6 files changed, 52 insertions(+), 35 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java index 6e4ee198cd851..7d149d88d886d 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code boolean} value. */ -public abstract class BucketAggregateToBooleanScript { +public interface BucketAggregateToBooleanScript { - public static final String[] PARAMETERS = { "params" }; + String[] PARAMETERS = { "params" }; - public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); + ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); - public abstract boolean execute(Map params); + boolean execute(Map params); - public interface Factory { + interface Factory { BucketAggregateToBooleanScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java index 91df4254f8451..9bb60ba307bf7 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code double} value. */ -public abstract class BucketAggregateToDoubleScript { +public interface BucketAggregateToDoubleScript { - public static final String[] PARAMETERS = { "params" }; + String[] PARAMETERS = { "params" }; - public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); + ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); - public abstract double execute(Map params); + double execute(Map params); - public interface Factory { + interface Factory { BucketAggregateToDoubleScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index f24c8419bff61..1b1db664b2d85 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -50,6 +50,7 @@ public class ScriptModule { SearchScript.TERMS_SET_QUERY_CONTEXT, ExecutableScript.CONTEXT, BucketAggregateToDoubleScript.CONTEXT, + BucketAggregateToBooleanScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index b7088cac98425..23df2884a3472 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -73,7 +73,7 @@ public double getScore(long subsetFreq, long subsetSize, long supersetFreq, long supersetSizeHolder.value = supersetSize; subsetDfHolder.value = subsetFreq; supersetDfHolder.value = supersetFreq; - return ((Number) executableScript.execute(params)).doubleValue(); + return executableScript.execute(params); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index 8cd6a2f94b05c..51a443bffed58 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -22,6 +22,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; +import org.elasticsearch.script.BucketAggregateToBooleanScript; import org.elasticsearch.script.BucketAggregateToDoubleScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -82,34 +83,41 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - BucketAggregateToDoubleScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); - BucketAggregateToDoubleScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); - for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { - Map vars = new HashMap<>(); - if (script.getParams() != null) { - vars.putAll(script.getParams()); + if ("expression".equals(script.getLang())) { + BucketAggregateToDoubleScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); + BucketAggregateToDoubleScript executableScript = factory.newInstance(); + for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { + if (executableScript.execute(scriptArgs(originalAgg, bucket)) == 1.0) { + newBuckets.add(bucket); + } } - for (Map.Entry entry : bucketsPathsMap.entrySet()) { - String varName = entry.getKey(); - String bucketsPath = entry.getValue(); - Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); - vars.put(varName, value); - } - Object scriptReturnValue = executableScript.execute(vars); - final boolean keepBucket; - // TODO: WTF!!!!! - if ("expression".equals(script.getLang())) { - double scriptDoubleValue = (double) scriptReturnValue; - keepBucket = scriptDoubleValue == 1.0; - } else { - keepBucket = (boolean) scriptReturnValue; - } - if (keepBucket) { - newBuckets.add(bucket); + } else { + BucketAggregateToBooleanScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregateToBooleanScript.CONTEXT); + BucketAggregateToBooleanScript executableScript = factory.newInstance(); + for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { + if (executableScript.execute(scriptArgs(originalAgg, bucket))) { + newBuckets.add(bucket); + } } } return originalAgg.create(newBuckets); } + + private Map scriptArgs(InternalMultiBucketAggregation originalAgg, InternalMultiBucketAggregation.InternalBucket bucket) { + Map vars = new HashMap<>(); + if (script.getParams() != null) { + vars.putAll(script.getParams()); + } + for (Map.Entry entry : bucketsPathsMap.entrySet()) { + String varName = entry.getKey(); + String bucketsPath = entry.getValue(); + Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); + vars.put(varName, value); + } + return vars; + } } diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 41acc7077f5b6..d91c244f3c759 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -104,6 +104,14 @@ public double execute(Map vars) { } }; return context.factoryClazz.cast(factory); + } else if (context.instanceClazz.equals(BucketAggregateToBooleanScript.class)) { + BucketAggregateToBooleanScript.Factory factory = () -> new BucketAggregateToBooleanScript() { + @Override + public boolean execute(Map vars) { + return (boolean) script.apply(vars); + } + }; + return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { // TODO: need a better way to implement all these new contexts From 0386ec8f7c994b7770b4f441d1f0e25cf41975fc Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 18 Jul 2018 13:59:02 +0200 Subject: [PATCH 04/17] CR: Make returns typed --- .../elasticsearch/script/MockScriptEngine.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index d91c244f3c759..3f22ae0e426d9 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -97,20 +97,12 @@ public void execute(Map ctx) { }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { - BucketAggregateToDoubleScript.Factory factory = () -> new BucketAggregateToDoubleScript() { - @Override - public double execute(Map vars) { - return (double) script.apply(vars); - } - }; + BucketAggregateToDoubleScript.Factory factory = + () -> (BucketAggregateToDoubleScript) vars -> ((Number) script.apply(vars)).doubleValue(); return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToBooleanScript.class)) { - BucketAggregateToBooleanScript.Factory factory = () -> new BucketAggregateToBooleanScript() { - @Override - public boolean execute(Map vars) { - return (boolean) script.apply(vars); - } - }; + BucketAggregateToBooleanScript.Factory factory = + () -> (BucketAggregateToBooleanScript) vars -> (boolean) script.apply(vars); return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { From 63963f65e31283deb6de11f4229555cf0a9c356f Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 18 Jul 2018 14:02:04 +0200 Subject: [PATCH 05/17] CR: Make returns typed --- .../java/org/elasticsearch/script/MockScriptEngine.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 3f22ae0e426d9..a3c9ee7a33d5f 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -97,12 +97,10 @@ public void execute(Map ctx) { }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { - BucketAggregateToDoubleScript.Factory factory = - () -> (BucketAggregateToDoubleScript) vars -> ((Number) script.apply(vars)).doubleValue(); + BucketAggregateToDoubleScript.Factory factory = () -> vars -> ((Number) script.apply(vars)).doubleValue(); return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToBooleanScript.class)) { - BucketAggregateToBooleanScript.Factory factory = - () -> (BucketAggregateToBooleanScript) vars -> (boolean) script.apply(vars); + BucketAggregateToBooleanScript.Factory factory = () -> vars -> (boolean) script.apply(vars); return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { From ba8fd94e96a5b3b924a1a46c093ac8a117244a2d Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 18 Jul 2018 14:35:38 +0200 Subject: [PATCH 06/17] CR: Make returns typed --- .../script/BucketAggregateToBooleanScript.java | 10 +++++----- .../script/BucketAggregateToDoubleScript.java | 10 +++++----- .../org/elasticsearch/script/MockScriptEngine.java | 14 ++++++++++++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java index 7d149d88d886d..6e4ee198cd851 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code boolean} value. */ -public interface BucketAggregateToBooleanScript { +public abstract class BucketAggregateToBooleanScript { - String[] PARAMETERS = { "params" }; + public static final String[] PARAMETERS = { "params" }; - ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); - boolean execute(Map params); + public abstract boolean execute(Map params); - interface Factory { + public interface Factory { BucketAggregateToBooleanScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java index 9bb60ba307bf7..91df4254f8451 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code double} value. */ -public interface BucketAggregateToDoubleScript { +public abstract class BucketAggregateToDoubleScript { - String[] PARAMETERS = { "params" }; + public static final String[] PARAMETERS = { "params" }; - ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); - double execute(Map params); + public abstract double execute(Map params); - interface Factory { + public interface Factory { BucketAggregateToDoubleScript newInstance(); } } diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index a3c9ee7a33d5f..1efdbe91bcab6 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -97,10 +97,20 @@ public void execute(Map ctx) { }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { - BucketAggregateToDoubleScript.Factory factory = () -> vars -> ((Number) script.apply(vars)).doubleValue(); + BucketAggregateToDoubleScript.Factory factory = () -> new BucketAggregateToDoubleScript() { + @Override + public double execute(Map vars) { + return ((Number) script.apply(vars)).doubleValue(); + } + }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregateToBooleanScript.class)) { - BucketAggregateToBooleanScript.Factory factory = () -> vars -> (boolean) script.apply(vars); + BucketAggregateToBooleanScript.Factory factory = () -> new BucketAggregateToBooleanScript() { + @Override + public boolean execute(Map vars) { + return (boolean) script.apply(vars); + } + }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { From 145e11f4f37cf7d6f353b32817065569072d2b79 Mon Sep 17 00:00:00 2001 From: Armin Date: Thu, 19 Jul 2018 09:57:32 +0200 Subject: [PATCH 07/17] Renamings from CR --- .../expression/ExpressionScriptEngine.java | 8 ++++---- ...eScript.java => BucketAggregationScript.java} | 6 +++--- ...java => BucketAggregationSelectorScript.java} | 6 +++--- .../org/elasticsearch/script/ScriptModule.java | 4 ++-- .../significant/heuristics/ScriptHeuristic.java | 10 +++++----- .../BucketScriptPipelineAggregator.java | 8 ++++---- .../BucketSelectorPipelineAggregator.java | 16 ++++++++-------- .../elasticsearch/script/MockScriptEngine.java | 8 ++++---- 8 files changed, 33 insertions(+), 33 deletions(-) rename server/src/main/java/org/elasticsearch/script/{BucketAggregateToDoubleScript.java => BucketAggregationScript.java} (88%) rename server/src/main/java/org/elasticsearch/script/{BucketAggregateToBooleanScript.java => BucketAggregationSelectorScript.java} (87%) diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index fc01eb5f6d593..c6ff437a229d9 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -38,7 +38,7 @@ import org.elasticsearch.index.mapper.GeoPointFieldMapper.GeoPointFieldType; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.script.BucketAggregateToDoubleScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.ClassPermission; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.FilterScript; @@ -114,7 +114,7 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE } else if (context.instanceClazz.equals(ExecutableScript.class)) { ExecutableScript.Factory factory = (p) -> new ExpressionExecutableScript(expr, p); return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { + } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); } else if (context.instanceClazz.equals(FilterScript.class)) { FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p); @@ -126,7 +126,7 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE throw new IllegalArgumentException("expression engine does not know how to handle script context [" + context.name + "]"); } - private static BucketAggregateToDoubleScript.Factory newBucketAggregationScriptFactory(Expression expr) { + private static BucketAggregationScript.Factory newBucketAggregationScriptFactory(Expression expr) { return () -> { ReplaceableConstDoubleValues[] functionValuesArray = new ReplaceableConstDoubleValues[expr.variables.length]; @@ -135,7 +135,7 @@ private static BucketAggregateToDoubleScript.Factory newBucketAggregationScriptF functionValuesArray[i] = new ReplaceableConstDoubleValues(); functionValuesMap.put(expr.variables[i], functionValuesArray[i]); } - return new BucketAggregateToDoubleScript() { + return new BucketAggregationScript() { @Override public double execute(Map params) { params.forEach((name, value) -> { diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java similarity index 88% rename from server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java rename to server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java index 91df4254f8451..b12f23e85e1a2 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToDoubleScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code double} value. */ -public abstract class BucketAggregateToDoubleScript { +public abstract class BucketAggregationScript { public static final String[] PARAMETERS = { "params" }; - public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_double", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("bucket_aggregation", Factory.class); public abstract double execute(Map params); public interface Factory { - BucketAggregateToDoubleScript newInstance(); + BucketAggregationScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java similarity index 87% rename from server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java rename to server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java index 6e4ee198cd851..19ceada10a2d4 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregateToBooleanScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java @@ -24,15 +24,15 @@ /** * A script used in bucket aggregations that returns a {@code boolean} value. */ -public abstract class BucketAggregateToBooleanScript { +public abstract class BucketAggregationSelectorScript { public static final String[] PARAMETERS = { "params" }; - public static final ScriptContext CONTEXT = new ScriptContext<>("aggs_boolean", Factory.class); + public static final ScriptContext CONTEXT = new ScriptContext<>("aggregation_selector", Factory.class); public abstract boolean execute(Map params); public interface Factory { - BucketAggregateToBooleanScript newInstance(); + BucketAggregationSelectorScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index 1b1db664b2d85..6ab7889f11708 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -49,8 +49,8 @@ public class ScriptModule { SearchScript.SCRIPT_SORT_CONTEXT, SearchScript.TERMS_SET_QUERY_CONTEXT, ExecutableScript.CONTEXT, - BucketAggregateToDoubleScript.CONTEXT, - BucketAggregateToBooleanScript.CONTEXT, + BucketAggregationScript.CONTEXT, + BucketAggregationSelectorScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index 23df2884a3472..e63c030cab315 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -28,7 +28,7 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; -import org.elasticsearch.script.BucketAggregateToDoubleScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.internal.SearchContext; @@ -50,10 +50,10 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { private final LongAccessor supersetSizeHolder; private final LongAccessor subsetDfHolder; private final LongAccessor supersetDfHolder; - private final BucketAggregateToDoubleScript executableScript; + private final BucketAggregationScript executableScript; private final Map params = new HashMap<>(); - ExecutableScriptHeuristic(Script script, BucketAggregateToDoubleScript executableScript) { + ExecutableScriptHeuristic(Script script, BucketAggregationScript executableScript) { super(script); subsetSizeHolder = new LongAccessor(); supersetSizeHolder = new LongAccessor(); @@ -95,14 +95,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { - BucketAggregateToDoubleScript.Factory factory = context.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); + BucketAggregationScript.Factory factory = context.scriptService().compile(script, BucketAggregationScript.CONTEXT); return new ExecutableScriptHeuristic(script, factory.newInstance()); } @Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); - BucketAggregateToDoubleScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); + BucketAggregationScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregationScript.CONTEXT); return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java index 5a4a585bd186a..e9436a9ec60a4 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java @@ -21,7 +21,7 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.BucketAggregateToDoubleScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.InternalAggregation; @@ -88,9 +88,9 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - BucketAggregateToDoubleScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); - BucketAggregateToDoubleScript executableScript = factory.newInstance(); + BucketAggregationScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregationScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index 51a443bffed58..d180c99dd4c75 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -22,8 +22,8 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.script.BucketAggregateToBooleanScript; -import org.elasticsearch.script.BucketAggregateToDoubleScript; +import org.elasticsearch.script.BucketAggregationSelectorScript; +import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -85,18 +85,18 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext List newBuckets = new ArrayList<>(); if ("expression".equals(script.getLang())) { - BucketAggregateToDoubleScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregateToDoubleScript.CONTEXT); - BucketAggregateToDoubleScript executableScript = factory.newInstance(); + BucketAggregationScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); + BucketAggregationScript executableScript = factory.newInstance(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { if (executableScript.execute(scriptArgs(originalAgg, bucket)) == 1.0) { newBuckets.add(bucket); } } } else { - BucketAggregateToBooleanScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregateToBooleanScript.CONTEXT); - BucketAggregateToBooleanScript executableScript = factory.newInstance(); + BucketAggregationSelectorScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregationSelectorScript.CONTEXT); + BucketAggregationSelectorScript executableScript = factory.newInstance(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { if (executableScript.execute(scriptArgs(originalAgg, bucket))) { newBuckets.add(bucket); diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 1efdbe91bcab6..e2287fef0aceb 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -96,16 +96,16 @@ public void execute(Map ctx) { } }; return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(BucketAggregateToDoubleScript.class)) { - BucketAggregateToDoubleScript.Factory factory = () -> new BucketAggregateToDoubleScript() { + } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { + BucketAggregationScript.Factory factory = () -> new BucketAggregationScript() { @Override public double execute(Map vars) { return ((Number) script.apply(vars)).doubleValue(); } }; return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(BucketAggregateToBooleanScript.class)) { - BucketAggregateToBooleanScript.Factory factory = () -> new BucketAggregateToBooleanScript() { + } else if (context.instanceClazz.equals(BucketAggregationSelectorScript.class)) { + BucketAggregationSelectorScript.Factory factory = () -> new BucketAggregationSelectorScript() { @Override public boolean execute(Map vars) { return (boolean) script.apply(vars); From bc2746643a94f91c50af2680e5f4d49c65e20857 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 07:37:24 +0200 Subject: [PATCH 08/17] CR: Make BucketSelectorPipelineAggregator agnositc to lang --- .../BucketSelectorPipelineAggregator.java | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index d180c99dd4c75..f31c9ce8c88e3 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -23,7 +23,6 @@ import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.script.BucketAggregationSelectorScript; -import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext; @@ -84,23 +83,12 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext List buckets = originalAgg.getBuckets(); List newBuckets = new ArrayList<>(); - if ("expression".equals(script.getLang())) { - BucketAggregationScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); - BucketAggregationScript executableScript = factory.newInstance(); - for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { - if (executableScript.execute(scriptArgs(originalAgg, bucket)) == 1.0) { - newBuckets.add(bucket); - } - } - } else { - BucketAggregationSelectorScript.Factory factory = - reduceContext.scriptService().compile(script, BucketAggregationSelectorScript.CONTEXT); - BucketAggregationSelectorScript executableScript = factory.newInstance(); - for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { - if (executableScript.execute(scriptArgs(originalAgg, bucket))) { - newBuckets.add(bucket); - } + BucketAggregationSelectorScript.Factory factory = + reduceContext.scriptService().compile(script, BucketAggregationSelectorScript.CONTEXT); + BucketAggregationSelectorScript executableScript = factory.newInstance(); + for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { + if (executableScript.execute(scriptArgs(originalAgg, bucket))) { + newBuckets.add(bucket); } } return originalAgg.create(newBuckets); From 0f13633790cd98fe64a507502bc79f1354c5d603 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 13:11:02 +0200 Subject: [PATCH 09/17] CR: Make BucketSelectorPipelineAggregator agnositic to language --- .../script/expression/ExpressionScriptEngine.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index c6ff437a229d9..9369141fea639 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -39,6 +39,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.script.BucketAggregationScript; +import org.elasticsearch.script.BucketAggregationSelectorScript; import org.elasticsearch.script.ClassPermission; import org.elasticsearch.script.ExecutableScript; import org.elasticsearch.script.FilterScript; @@ -116,6 +117,18 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); + } else if (context.instanceClazz.equals(BucketAggregationSelectorScript.class)) { + BucketAggregationScript.Factory factory = newBucketAggregationScriptFactory(expr); + BucketAggregationSelectorScript.Factory wrappedFactory = () -> { + BucketAggregationScript script = factory.newInstance(); + return new BucketAggregationSelectorScript() { + @Override + public boolean execute(Map params) { + return script.execute(params) == 1.0; + } + }; + }; + return context.factoryClazz.cast(wrappedFactory); } else if (context.instanceClazz.equals(FilterScript.class)) { FilterScript.Factory factory = (p, lookup) -> newFilterScript(expr, lookup, p); return context.factoryClazz.cast(factory); From a1bd3895723efbbebfc07d1b175c9548a5b63f20 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 13:17:03 +0200 Subject: [PATCH 10/17] CR: Add separate context for scriptheuristic --- .../script/ScriptHeuristicScript.java | 38 +++++++++++++++++++ .../heuristics/ScriptHeuristic.java | 10 ++--- 2 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java diff --git a/server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java b/server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java new file mode 100644 index 0000000000000..5d262b3be87f6 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java @@ -0,0 +1,38 @@ +/* + * 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; + +/** + * A script used in script heuristics. + */ +public abstract class ScriptHeuristicScript { + + public static final String[] PARAMETERS = { "params" }; + + public static final ScriptContext CONTEXT = new ScriptContext<>("script_heuristic", Factory.class); + + public abstract double execute(Map params); + + public interface Factory { + ScriptHeuristicScript newInstance(); + } +} diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index e63c030cab315..6673a97b87669 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -28,8 +28,8 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; -import org.elasticsearch.script.BucketAggregationScript; import org.elasticsearch.script.Script; +import org.elasticsearch.script.ScriptHeuristicScript; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.internal.SearchContext; @@ -50,10 +50,10 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { private final LongAccessor supersetSizeHolder; private final LongAccessor subsetDfHolder; private final LongAccessor supersetDfHolder; - private final BucketAggregationScript executableScript; + private final ScriptHeuristicScript executableScript; private final Map params = new HashMap<>(); - ExecutableScriptHeuristic(Script script, BucketAggregationScript executableScript) { + ExecutableScriptHeuristic(Script script, ScriptHeuristicScript executableScript) { super(script); subsetSizeHolder = new LongAccessor(); supersetSizeHolder = new LongAccessor(); @@ -95,14 +95,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { - BucketAggregationScript.Factory factory = context.scriptService().compile(script, BucketAggregationScript.CONTEXT); + ScriptHeuristicScript.Factory factory = context.scriptService().compile(script, ScriptHeuristicScript.CONTEXT); return new ExecutableScriptHeuristic(script, factory.newInstance()); } @Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); - BucketAggregationScript.Factory compiledScript = shardContext.getScriptService().compile(script, BucketAggregationScript.CONTEXT); + ScriptHeuristicScript.Factory compiledScript = shardContext.getScriptService().compile(script, ScriptHeuristicScript.CONTEXT); return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); } From af67738b31343f647c216f3c28d1b8b1cbcb3ec8 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 17:03:32 +0200 Subject: [PATCH 11/17] CR: Add separate context for scriptheuristic --- server/src/main/java/org/elasticsearch/script/ScriptModule.java | 1 + 1 file changed, 1 insertion(+) diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index ba3c132f0e0d4..f1c495b66fda8 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -49,6 +49,7 @@ public class ScriptModule { ExecutableScript.CONTEXT, BucketAggregationScript.CONTEXT, BucketAggregationSelectorScript.CONTEXT, + ScriptHeuristicScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, From b0a44873f1f8b706bc1d424c57c324e415c536ce Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 17:18:29 +0200 Subject: [PATCH 12/17] CR: Add separate context for scriptheuristic --- .../java/org/elasticsearch/script/MockScriptEngine.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index e2287fef0aceb..d9307fd1ebbf1 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -112,6 +112,14 @@ public boolean execute(Map vars) { } }; return context.factoryClazz.cast(factory); + } else if (context.instanceClazz.equals(ScriptHeuristicScript.class)) { + ScriptHeuristicScript.Factory factory = () -> new ScriptHeuristicScript() { + @Override + public double execute(Map vars) { + return (double) script.apply(vars); + } + }; + return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(TemplateScript.class)) { TemplateScript.Factory factory = vars -> { // TODO: need a better way to implement all these new contexts From 2662c875f5eb786c4f29d4c116e44913e9843440 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 21 Jul 2018 17:53:01 +0200 Subject: [PATCH 13/17] CR: Add separate context for scriptheuristic --- .../main/java/org/elasticsearch/script/MockScriptEngine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index d9307fd1ebbf1..6d1372c56f35b 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -116,7 +116,7 @@ public boolean execute(Map vars) { ScriptHeuristicScript.Factory factory = () -> new ScriptHeuristicScript() { @Override public double execute(Map vars) { - return (double) script.apply(vars); + return ((Number) script.apply(vars)).doubleValue(); } }; return context.factoryClazz.cast(factory); From d506e97f4ad52ed316527ff23eef87283b99b94c Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 25 Jul 2018 08:49:11 +0200 Subject: [PATCH 14/17] CR: Move params to script field --- .../expression/ExpressionScriptEngine.java | 8 ++--- .../BucketAggregationSelectorScript.java | 22 +++++++++++-- .../BucketSelectorPipelineAggregator.java | 32 ++++++++----------- .../script/MockScriptEngine.java | 6 ++-- 4 files changed, 40 insertions(+), 28 deletions(-) diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index 9369141fea639..3cfd617d51ab0 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -119,12 +119,12 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); } else if (context.instanceClazz.equals(BucketAggregationSelectorScript.class)) { BucketAggregationScript.Factory factory = newBucketAggregationScriptFactory(expr); - BucketAggregationSelectorScript.Factory wrappedFactory = () -> { + BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> { BucketAggregationScript script = factory.newInstance(); - return new BucketAggregationSelectorScript() { + return new BucketAggregationSelectorScript(parameters) { @Override - public boolean execute(Map params) { - return script.execute(params) == 1.0; + public boolean execute() { + return script.execute(getParams()) == 1.0; } }; }; diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java index 19ceada10a2d4..4ae0786d44e3c 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java @@ -26,13 +26,29 @@ */ public abstract class BucketAggregationSelectorScript { - public static final String[] PARAMETERS = { "params" }; + public static final String[] PARAMETERS = {}; public static final ScriptContext CONTEXT = new ScriptContext<>("aggregation_selector", Factory.class); - public abstract boolean execute(Map params); + /** + * The generic runtime parameters for the script. + */ + private final Map params; + + protected BucketAggregationSelectorScript(Map params) { + this.params = params; + } + + /** + * Return the parameters for this script. + */ + public Map getParams() { + return params; + } + + public abstract boolean execute(); public interface Factory { - BucketAggregationSelectorScript newInstance(); + BucketAggregationSelectorScript newInstance(Map params); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java index f31c9ce8c88e3..06beab04aa605 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketselector/BucketSelectorPipelineAggregator.java @@ -82,30 +82,26 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext (InternalMultiBucketAggregation) aggregation; List buckets = originalAgg.getBuckets(); - List newBuckets = new ArrayList<>(); BucketAggregationSelectorScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationSelectorScript.CONTEXT); - BucketAggregationSelectorScript executableScript = factory.newInstance(); + List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { - if (executableScript.execute(scriptArgs(originalAgg, bucket))) { + Map vars = new HashMap<>(); + if (script.getParams() != null) { + vars.putAll(script.getParams()); + } + for (Map.Entry entry : bucketsPathsMap.entrySet()) { + String varName = entry.getKey(); + String bucketsPath = entry.getValue(); + Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); + vars.put(varName, value); + } + // TODO: can we use one instance of the script for all buckets? it should be stateless? + BucketAggregationSelectorScript executableScript = factory.newInstance(vars); + if (executableScript.execute()) { newBuckets.add(bucket); } } return originalAgg.create(newBuckets); } - - private Map scriptArgs(InternalMultiBucketAggregation originalAgg, InternalMultiBucketAggregation.InternalBucket bucket) { - Map vars = new HashMap<>(); - if (script.getParams() != null) { - vars.putAll(script.getParams()); - } - for (Map.Entry entry : bucketsPathsMap.entrySet()) { - String varName = entry.getKey(); - String bucketsPath = entry.getValue(); - Double value = resolveBucketValue(originalAgg, bucket, bucketsPath, gapPolicy); - vars.put(varName, value); - } - return vars; - } } diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 6d1372c56f35b..9b839bca7c946 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -105,10 +105,10 @@ public double execute(Map vars) { }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregationSelectorScript.class)) { - BucketAggregationSelectorScript.Factory factory = () -> new BucketAggregationSelectorScript() { + BucketAggregationSelectorScript.Factory factory = parameters -> new BucketAggregationSelectorScript(parameters) { @Override - public boolean execute(Map vars) { - return (boolean) script.apply(vars); + public boolean execute() { + return (boolean) script.apply(getParams()); } }; return context.factoryClazz.cast(factory); From 5a55338ec89f148c2ca413c5b9e36447d8db00b3 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 25 Jul 2018 10:04:52 +0200 Subject: [PATCH 15/17] CR: Move params to script field --- .../elasticsearch/script/BucketAggregationSelectorScript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java index 4ae0786d44e3c..a8e2fad7cdcda 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationSelectorScript.java @@ -35,7 +35,7 @@ public abstract class BucketAggregationSelectorScript { */ private final Map params; - protected BucketAggregationSelectorScript(Map params) { + public BucketAggregationSelectorScript(Map params) { this.params = params; } From b095f3d4e90e17c6402f0e241922bf089bc33833 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 25 Jul 2018 14:30:17 +0200 Subject: [PATCH 16/17] CR: Move params to script field --- .../expression/ExpressionScriptEngine.java | 21 ++++++++---------- .../script/BucketAggregationScript.java | 22 ++++++++++++++++--- .../BucketScriptPipelineAggregator.java | 3 +-- .../script/MockScriptEngine.java | 6 ++--- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java index 3cfd617d51ab0..a3a62225b09fb 100644 --- a/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java +++ b/modules/lang-expression/src/main/java/org/elasticsearch/script/expression/ExpressionScriptEngine.java @@ -119,14 +119,11 @@ protected Class loadClass(String name, boolean resolve) throws ClassNotFoundE return context.factoryClazz.cast(newBucketAggregationScriptFactory(expr)); } else if (context.instanceClazz.equals(BucketAggregationSelectorScript.class)) { BucketAggregationScript.Factory factory = newBucketAggregationScriptFactory(expr); - BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> { - BucketAggregationScript script = factory.newInstance(); - return new BucketAggregationSelectorScript(parameters) { - @Override - public boolean execute() { - return script.execute(getParams()) == 1.0; - } - }; + BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) { + @Override + public boolean execute() { + return factory.newInstance(getParams()).execute() == 1.0; + } }; return context.factoryClazz.cast(wrappedFactory); } else if (context.instanceClazz.equals(FilterScript.class)) { @@ -140,7 +137,7 @@ public boolean execute() { } private static BucketAggregationScript.Factory newBucketAggregationScriptFactory(Expression expr) { - return () -> { + return parameters -> { ReplaceableConstDoubleValues[] functionValuesArray = new ReplaceableConstDoubleValues[expr.variables.length]; Map functionValuesMap = new HashMap<>(); @@ -148,10 +145,10 @@ private static BucketAggregationScript.Factory newBucketAggregationScriptFactory functionValuesArray[i] = new ReplaceableConstDoubleValues(); functionValuesMap.put(expr.variables[i], functionValuesArray[i]); } - return new BucketAggregationScript() { + return new BucketAggregationScript(parameters) { @Override - public double execute(Map params) { - params.forEach((name, value) -> { + public double execute() { + getParams().forEach((name, value) -> { ReplaceableConstDoubleValues placeholder = functionValuesMap.get(name); if (placeholder == null) { throw new IllegalArgumentException("Error using " + expr + ". " + diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java index b12f23e85e1a2..5d4a934969424 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java @@ -26,13 +26,29 @@ */ public abstract class BucketAggregationScript { - public static final String[] PARAMETERS = { "params" }; + public static final String[] PARAMETERS = {}; public static final ScriptContext CONTEXT = new ScriptContext<>("bucket_aggregation", Factory.class); - public abstract double execute(Map params); + /** + * The generic runtime parameters for the script. + */ + private final Map params; + + public BucketAggregationScript(Map params) { + this.params = params; + } + + /** + * Return the parameters for this script. + */ + public Map getParams() { + return params; + } + + public abstract double execute(); public interface Factory { - BucketAggregationScript newInstance(); + BucketAggregationScript newInstance(Map params); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java index e9436a9ec60a4..c8117f9029bb6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/bucketscript/BucketScriptPipelineAggregator.java @@ -90,7 +90,6 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext BucketAggregationScript.Factory factory = reduceContext.scriptService().compile(script, BucketAggregationScript.CONTEXT); - BucketAggregationScript executableScript = factory.newInstance(); List newBuckets = new ArrayList<>(); for (InternalMultiBucketAggregation.InternalBucket bucket : buckets) { Map vars = new HashMap<>(); @@ -111,7 +110,7 @@ public InternalAggregation reduce(InternalAggregation aggregation, ReduceContext if (skipBucket) { newBuckets.add(bucket); } else { - double returned = executableScript.execute(vars); + double returned = factory.newInstance(vars).execute(); final List aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map( (p) -> (InternalAggregation) p).collect(Collectors.toList()); aggs.add(new InternalSimpleValue(name(), returned, formatter, new ArrayList<>(), metaData())); diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 9b839bca7c946..4835084f4cd6e 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -97,10 +97,10 @@ public void execute(Map ctx) { }; return context.factoryClazz.cast(factory); } else if (context.instanceClazz.equals(BucketAggregationScript.class)) { - BucketAggregationScript.Factory factory = () -> new BucketAggregationScript() { + BucketAggregationScript.Factory factory = parameters -> new BucketAggregationScript(parameters) { @Override - public double execute(Map vars) { - return ((Number) script.apply(vars)).doubleValue(); + public double execute() { + return ((Number) script.apply(getParams())).doubleValue(); } }; return context.factoryClazz.cast(factory); From 6509e85955a4ec7195c2f8021e78998f95fd1695 Mon Sep 17 00:00:00 2001 From: Armin Date: Sat, 4 Aug 2018 08:05:17 +0200 Subject: [PATCH 17/17] CR: Rename heuristic score script --- .../java/org/elasticsearch/script/ScriptModule.java | 2 +- ....java => SignificantTermsHeuristicScoreScript.java} | 6 +++--- .../bucket/significant/heuristics/ScriptHeuristic.java | 10 +++++----- .../org/elasticsearch/script/MockScriptEngine.java | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) rename server/src/main/java/org/elasticsearch/script/{ScriptHeuristicScript.java => SignificantTermsHeuristicScoreScript.java} (86%) diff --git a/server/src/main/java/org/elasticsearch/script/ScriptModule.java b/server/src/main/java/org/elasticsearch/script/ScriptModule.java index 596bd88ce2763..3eeb26317f9cc 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptModule.java +++ b/server/src/main/java/org/elasticsearch/script/ScriptModule.java @@ -48,7 +48,7 @@ public class ScriptModule { ExecutableScript.CONTEXT, BucketAggregationScript.CONTEXT, BucketAggregationSelectorScript.CONTEXT, - ScriptHeuristicScript.CONTEXT, + SignificantTermsHeuristicScoreScript.CONTEXT, ExecutableScript.UPDATE_CONTEXT, IngestScript.CONTEXT, FilterScript.CONTEXT, diff --git a/server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java b/server/src/main/java/org/elasticsearch/script/SignificantTermsHeuristicScoreScript.java similarity index 86% rename from server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java rename to server/src/main/java/org/elasticsearch/script/SignificantTermsHeuristicScoreScript.java index 5d262b3be87f6..0296bc36ce107 100644 --- a/server/src/main/java/org/elasticsearch/script/ScriptHeuristicScript.java +++ b/server/src/main/java/org/elasticsearch/script/SignificantTermsHeuristicScoreScript.java @@ -22,9 +22,9 @@ import java.util.Map; /** - * A script used in script heuristics. + * A script used in significant terms heuristic scoring. */ -public abstract class ScriptHeuristicScript { +public abstract class SignificantTermsHeuristicScoreScript { public static final String[] PARAMETERS = { "params" }; @@ -33,6 +33,6 @@ public abstract class ScriptHeuristicScript { public abstract double execute(Map params); public interface Factory { - ScriptHeuristicScript newInstance(); + SignificantTermsHeuristicScoreScript newInstance(); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java index 6673a97b87669..05415cf7d19a8 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/heuristics/ScriptHeuristic.java @@ -29,7 +29,7 @@ import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.QueryShardException; import org.elasticsearch.script.Script; -import org.elasticsearch.script.ScriptHeuristicScript; +import org.elasticsearch.script.SignificantTermsHeuristicScoreScript; import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.internal.SearchContext; @@ -50,10 +50,10 @@ static class ExecutableScriptHeuristic extends ScriptHeuristic { private final LongAccessor supersetSizeHolder; private final LongAccessor subsetDfHolder; private final LongAccessor supersetDfHolder; - private final ScriptHeuristicScript executableScript; + private final SignificantTermsHeuristicScoreScript executableScript; private final Map params = new HashMap<>(); - ExecutableScriptHeuristic(Script script, ScriptHeuristicScript executableScript) { + ExecutableScriptHeuristic(Script script, SignificantTermsHeuristicScoreScript executableScript) { super(script); subsetSizeHolder = new LongAccessor(); supersetSizeHolder = new LongAccessor(); @@ -95,14 +95,14 @@ public void writeTo(StreamOutput out) throws IOException { @Override public SignificanceHeuristic rewrite(InternalAggregation.ReduceContext context) { - ScriptHeuristicScript.Factory factory = context.scriptService().compile(script, ScriptHeuristicScript.CONTEXT); + SignificantTermsHeuristicScoreScript.Factory factory = context.scriptService().compile(script, SignificantTermsHeuristicScoreScript.CONTEXT); return new ExecutableScriptHeuristic(script, factory.newInstance()); } @Override public SignificanceHeuristic rewrite(SearchContext context) { QueryShardContext shardContext = context.getQueryShardContext(); - ScriptHeuristicScript.Factory compiledScript = shardContext.getScriptService().compile(script, ScriptHeuristicScript.CONTEXT); + SignificantTermsHeuristicScoreScript.Factory compiledScript = shardContext.getScriptService().compile(script, SignificantTermsHeuristicScoreScript.CONTEXT); return new ExecutableScriptHeuristic(script, compiledScript.newInstance()); } diff --git a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java index 4835084f4cd6e..14dcac926f7b0 100644 --- a/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java +++ b/test/framework/src/main/java/org/elasticsearch/script/MockScriptEngine.java @@ -112,8 +112,8 @@ public boolean execute() { } }; return context.factoryClazz.cast(factory); - } else if (context.instanceClazz.equals(ScriptHeuristicScript.class)) { - ScriptHeuristicScript.Factory factory = () -> new ScriptHeuristicScript() { + } else if (context.instanceClazz.equals(SignificantTermsHeuristicScoreScript.class)) { + SignificantTermsHeuristicScoreScript.Factory factory = () -> new SignificantTermsHeuristicScoreScript() { @Override public double execute(Map vars) { return ((Number) script.apply(vars)).doubleValue();