diff --git a/server/src/main/java/org/elasticsearch/search/SearchModule.java b/server/src/main/java/org/elasticsearch/search/SearchModule.java index 1a5ce78250eea..090aa2ad71d7c 100644 --- a/server/src/main/java/org/elasticsearch/search/SearchModule.java +++ b/server/src/main/java/org/elasticsearch/search/SearchModule.java @@ -370,8 +370,9 @@ private void registerAggregations(List plugins) { .addResultReader(InternalTDigestPercentileRanks.NAME, InternalTDigestPercentileRanks::new) .addResultReader(InternalHDRPercentileRanks.NAME, InternalHDRPercentileRanks::new)); registerAggregation(new AggregationSpec(MedianAbsoluteDeviationAggregationBuilder.NAME, - MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder::parse) - .addResultReader(InternalMedianAbsoluteDeviation::new)); + MedianAbsoluteDeviationAggregationBuilder::new, MedianAbsoluteDeviationAggregationBuilder::parse) + .addResultReader(InternalMedianAbsoluteDeviation::new) + .setAggregatorRegistrar(MedianAbsoluteDeviationAggregationBuilder::registerAggregators)); registerAggregation(new AggregationSpec(CardinalityAggregationBuilder.NAME, CardinalityAggregationBuilder::new, CardinalityAggregationBuilder::parse).addResultReader(InternalCardinality::new) .setAggregatorRegistrar(CardinalityAggregationBuilder::registerAggregators)); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java index 6efb8097ad805..11419e0f8d263 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregationBuilder.java @@ -35,6 +35,7 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory; 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; @@ -59,6 +60,10 @@ public static MedianAbsoluteDeviationAggregationBuilder parse(String aggregation return PARSER.parse(parser, new MedianAbsoluteDeviationAggregationBuilder(aggregationName), null); } + public static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { + MedianAbsoluteDeviationAggregatorFactory.registerAggregators(valuesSourceRegistry); + } + private double compression = 1000d; public MedianAbsoluteDeviationAggregationBuilder(String name) { diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorFactory.java index 4370441cffd23..9e69c9038f0f9 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorFactory.java @@ -20,14 +20,18 @@ 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.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; @@ -39,22 +43,49 @@ public class MedianAbsoluteDeviationAggregatorFactory extends ValuesSourceAggreg private final double compression; MedianAbsoluteDeviationAggregatorFactory(String name, - ValuesSourceConfig config, - QueryShardContext queryShardContext, - AggregatorFactory parent, - AggregatorFactories.Builder subFactoriesBuilder, - Map metaData, - double compression) throws IOException { + ValuesSourceConfig config, + QueryShardContext queryShardContext, + AggregatorFactory parent, + AggregatorFactories.Builder subFactoriesBuilder, + Map metaData, + double compression) throws IOException { super(name, config, queryShardContext, parent, subFactoriesBuilder, metaData); this.compression = compression; } + static void registerAggregators(ValuesSourceRegistry valuesSourceRegistry) { + valuesSourceRegistry.register(MedianAbsoluteDeviationAggregationBuilder.NAME, + CoreValuesSourceType.NUMERIC, + new MedianAbsoluteDeviationAggregatorSupplier() { + @Override + public Aggregator build(String name, + ValuesSource valuesSource, + DocValueFormat format, + SearchContext context, + Aggregator parent, + List pipelineAggregators, + Map metaData, + double compression) throws IOException { + return new MedianAbsoluteDeviationAggregator( + name, + context, + parent, + pipelineAggregators, + metaData, + (ValuesSource.Numeric) valuesSource, + format, + compression + ); + } + }); + } + @Override protected Aggregator createUnmapped(SearchContext searchContext, - Aggregator parent, - List pipelineAggregators, - Map metaData) throws IOException { + Aggregator parent, + List pipelineAggregators, + Map metaData) throws IOException { return new MedianAbsoluteDeviationAggregator( name, @@ -75,20 +106,14 @@ protected Aggregator doCreateInternal(ValuesSource valuesSource, boolean collectsFromSingleBucket, List pipelineAggregators, Map metaData) throws IOException { + AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(), + MedianAbsoluteDeviationAggregationBuilder.NAME); - if (valuesSource instanceof ValuesSource.Numeric == false) { - throw new AggregationExecutionException("ValuesSource type " + valuesSource.toString() + "is not supported for aggregation " + - this.name()); + if (aggregatorSupplier instanceof MedianAbsoluteDeviationAggregatorSupplier == false) { + throw new AggregationExecutionException("Registry miss-match - expected MedianAbsoluteDeviationAggregatorSupplier, found [" + + aggregatorSupplier.getClass().toString() + "]"); } - return new MedianAbsoluteDeviationAggregator( - name, - searchContext, - parent, - pipelineAggregators, - metaData, - (ValuesSource.Numeric) valuesSource, - config.format(), - compression - ); + return ((MedianAbsoluteDeviationAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), + searchContext, parent, pipelineAggregators, metaData, compression); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorSupplier.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorSupplier.java new file mode 100644 index 0000000000000..4451f731487a8 --- /dev/null +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregatorSupplier.java @@ -0,0 +1,41 @@ +/* + * 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.search.aggregations.metrics; + +import org.elasticsearch.search.DocValueFormat; +import org.elasticsearch.search.aggregations.Aggregator; +import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; +import org.elasticsearch.search.aggregations.support.AggregatorSupplier; +import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.internal.SearchContext; + +import java.io.IOException; +import java.util.List; +import java.util.Map; + +public interface MedianAbsoluteDeviationAggregatorSupplier extends AggregatorSupplier { + Aggregator build(String name, + ValuesSource valuesSource, + DocValueFormat format, + SearchContext context, + Aggregator parent, + List pipelineAggregators, + Map metaData, + double compression) throws IOException; +} diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MetricAggregatorSupplier.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MetricAggregatorSupplier.java index 7c32d690fa4ad..4aa2136f03fbc 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MetricAggregatorSupplier.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MetricAggregatorSupplier.java @@ -30,11 +30,10 @@ import java.util.Map; public interface MetricAggregatorSupplier extends AggregatorSupplier { - Aggregator build(String name, ValuesSource valuesSource, - DocValueFormat formatter, - SearchContext searchContext, + DocValueFormat format, + SearchContext context, Aggregator parent, List pipelineAggregators, Map metaData) throws IOException;