From 59d4a9d339f45389b3b4a2fafb3bd8cbbd69510d Mon Sep 17 00:00:00 2001 From: Christos Soulios Date: Thu, 27 Feb 2020 14:59:24 +0200 Subject: [PATCH] VS refactoring: Wire up stats aggregation --- .../elasticsearch/search/SearchModule.java | 3 +- .../metrics/StatsAggregationBuilder.java | 5 +++ .../aggregations/metrics/StatsAggregator.java | 1 - .../metrics/StatsAggregatorFactory.java | 35 +++++++++++++++---- 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/SearchModule.java b/server/src/main/java/org/elasticsearch/search/SearchModule.java index e6db21648cc72..f85f743d1f847 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/server/src/main/java/org/elasticsearch/search/SearchModule.java @@ -352,7 +352,8 @@ private void registerAggregations(List plugins) { .addResultReader(InternalMax::new) .setAggregatorRegistrar(MaxAggregationBuilder::registerAggregators)); registerAggregation(new AggregationSpec(StatsAggregationBuilder.NAME, StatsAggregationBuilder::new, StatsAggregationBuilder::parse) - .addResultReader(InternalStats::new)); + .addResultReader(InternalStats::new) + .setAggregatorRegistrar(StatsAggregationBuilder::registerAggregators)); registerAggregation(new AggregationSpec(ExtendedStatsAggregationBuilder.NAME, ExtendedStatsAggregationBuilder::new, ExtendedStatsAggregationBuilder::parse).addResultReader(InternalExtendedStats::new)); registerAggregation(new AggregationSpec(ValueCountAggregationBuilder.NAME, ValueCountAggregationBuilder::new, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java index f3b842ed9195d..ed86e9c533e87 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregationBuilder.java @@ -33,6 +33,7 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.aggregations.support.ValuesSourceParserHelper; +import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.aggregations.support.ValuesSourceType; import java.io.IOException; @@ -60,6 +61,10 @@ protected StatsAggregationBuilder(StatsAggregationBuilder clone, super(clone, factoriesBuilder, metaData); } + public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { + StatsAggregatorFactory.registerAggregators(valuesSourceRegistry); + } + @Override protected AggregationBuilder shallowCopy(Builder factoriesBuilder, Map metaData) { return new StatsAggregationBuilder(this, factoriesBuilder, metaData); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregator.java index 7ae5b016f75c3..bdb53e92df9eb 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregator.java @@ -49,7 +49,6 @@ class StatsAggregator extends NumericMetricsAggregator.MultiValue { DoubleArray mins; DoubleArray maxes; - StatsAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat format, SearchContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorFactory.java index 9204ebf81b9ab..d1da48f0f5e9c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/StatsAggregatorFactory.java @@ -20,15 +20,19 @@ package org.elasticsearch.search.aggregations.metrics; import org.elasticsearch.index.query.QueryShardContext; +import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.AggregationExecutionException; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.AggregatorFactory; import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.search.aggregations.support.AggregatorSupplier; +import org.elasticsearch.search.aggregations.support.CoreValuesSourceType; import org.elasticsearch.search.aggregations.support.ValuesSource; import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric; import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; +import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -46,12 +50,27 @@ class StatsAggregatorFactory extends ValuesSourceAggregatorFactory { super(name, config, queryShardContext, parent, subFactoriesBuilder, metaData); } + static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { + valuesSourceRegistry.register(StatsAggregationBuilder.NAME, + List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + new MetricAggregatorSupplier() { + @Override + public Aggregator build(String name, + ValuesSource valuesSource, + DocValueFormat formatter, + SearchContext context, + Aggregator parent, + List pipelineAggregators, Map metaData) throws IOException { + return new StatsAggregator(name, (Numeric) valuesSource, formatter, context, parent, pipelineAggregators, metaData); + } + }); + } + @Override protected Aggregator createUnmapped(SearchContext searchContext, Aggregator parent, List pipelineAggregators, - Map metaData) - throws IOException { + Map metaData) throws IOException { return new StatsAggregator(name, null, config.format(), searchContext, parent, pipelineAggregators, metaData); } @@ -62,10 +81,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { - if (valuesSource instanceof Numeric == false) { - throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " + - this.name()); + AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(), + StatsAggregationBuilder.NAME); + + if (aggregatorSupplier instanceof MetricAggregatorSupplier == false) { + throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + + aggregatorSupplier.getClass().toString() + "]"); } - return new StatsAggregator(name, (Numeric) valuesSource, config.format(), searchContext, parent, pipelineAggregators, metaData); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), searchContext, parent, + pipelineAggregators, metaData); } }