[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.
This commit is contained in:
Jack Conradson 2018-11-16 12:33:01 -08:00 committed by GitHub
parent baf33b3162
commit d62bbca56d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 4 deletions

View File

@ -109,7 +109,7 @@ public class ExpressionScriptEngine implements ScriptEngine {
BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) { BucketAggregationSelectorScript.Factory wrappedFactory = parameters -> new BucketAggregationSelectorScript(parameters) {
@Override @Override
public boolean execute() { public boolean execute() {
return factory.newInstance(getParams()).execute() == 1.0; return factory.newInstance(getParams()).execute().doubleValue() == 1.0;
} }
}; };
return context.factoryClazz.cast(wrappedFactory); return context.factoryClazz.cast(wrappedFactory);

View File

@ -46,7 +46,7 @@ public abstract class BucketAggregationScript {
return params; return params;
} }
public abstract Double execute(); public abstract Number execute();
public interface Factory { public interface Factory {
BucketAggregationScript newInstance(Map<String, Object> params); BucketAggregationScript newInstance(Map<String, Object> params);

View File

@ -108,13 +108,13 @@ public class BucketScriptPipelineAggregator extends PipelineAggregator {
if (skipBucket) { if (skipBucket) {
newBuckets.add(bucket); newBuckets.add(bucket);
} else { } else {
Double returned = factory.newInstance(vars).execute(); Number returned = factory.newInstance(vars).execute();
if (returned == null) { if (returned == null) {
newBuckets.add(bucket); newBuckets.add(bucket);
} else { } else {
final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map( final List<InternalAggregation> aggs = StreamSupport.stream(bucket.getAggregations().spliterator(), false).map(
(p) -> (InternalAggregation) p).collect(Collectors.toList()); (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), InternalMultiBucketAggregation.InternalBucket newBucket = originalAgg.createBucket(new InternalAggregations(aggs),
bucket); bucket);
newBuckets.add(newBucket); newBuckets.add(newBucket);