From d62bbca56da771afb72ffcf1142c72ecbf34ea94 Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Fri, 16 Nov 2018 12:33:01 -0800 Subject: [PATCH] [Scripting] Use Number as a return value for BucketAggregationScript (#35653) This change fixes #35351. Users were no longer able to return types of numbers other than doubles for bucket aggregation scripts. This change reverts to the previous behavior of being able to return any type of number and having it converted to a double outside of the script. --- .../script/expression/ExpressionScriptEngine.java | 2 +- .../org/elasticsearch/script/BucketAggregationScript.java | 2 +- .../aggregations/pipeline/BucketScriptPipelineAggregator.java | 4 ++-- 3 files changed, 4 insertions(+), 4 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 e53d0ec3e8c..1ad0fff49b7 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 @@ -109,7 +109,7 @@ public class ExpressionScriptEngine implements ScriptEngine { BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) { @Override public boolean execute() { - return factory.newInstance(getParams()).execute() == 1.0; + return factory.newInstance(getParams()).execute().doubleValue() == 1.0; } }; return context.factoryClazz.cast(wrappedFactory); diff --git a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java index 5fa8d1fbf94..76ff776353e 100644 --- a/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java +++ b/server/src/main/java/org/elasticsearch/script/BucketAggregationScript.java @@ -46,7 +46,7 @@ public abstract class BucketAggregationScript { return params; } - public abstract Double execute(); + public abstract Number execute(); public interface Factory { BucketAggregationScript newInstance(Map params); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptPipelineAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptPipelineAggregator.java index bd9371815fa..d19425e3e03 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptPipelineAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/pipeline/BucketScriptPipelineAggregator.java @@ -108,13 +108,13 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator { if (skipBucket) { newBuckets.add(bucket); } else { - Double returned = factory.newInstance(vars).execute(); + Number returned = factory.newInstance(vars).execute(); if (returned == null) { newBuckets.add(bucket); } else { 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())); + aggs.add(new InternalSimpleValue(name(), returned.doubleValue(), formatter, new ArrayList<>(), metaData())); InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs), bucket); newBuckets.add(newBucket);