From 1f500583b1c5728f7a77ae1dc17ab8a8ef2ae8b9 Mon Sep 17 00:00:00 2001 From: Mark Tozzi Date: Mon, 1 Jun 2020 14:21:07 -0400 Subject: [PATCH] Clean up Aggregator Supplier Boiler Plate (#57442) (#57452) --- .../range/AbstractRangeAggregatorFactory.java | 22 ++--------- .../aggregations/metrics/AvgAggregator.java | 9 +++-- .../metrics/AvgAggregatorFactory.java | 22 +++-------- .../metrics/GeoBoundsAggregator.java | 4 +- .../metrics/GeoBoundsAggregatorFactory.java | 4 +- .../metrics/GeoCentroidAggregator.java | 4 +- .../metrics/GeoCentroidAggregatorFactory.java | 3 +- .../aggregations/metrics/MaxAggregator.java | 4 +- .../metrics/MaxAggregatorFactory.java | 23 +++--------- .../MedianAbsoluteDeviationAggregator.java | 8 ++-- ...ianAbsoluteDeviationAggregatorFactory.java | 27 ++------------ .../metrics/MetricAggregatorSupplier.java | 4 +- .../aggregations/metrics/MinAggregator.java | 4 +- .../metrics/MinAggregatorFactory.java | 22 +++-------- .../metrics/MinMaxAggregatorSupplier.java | 37 ------------------- .../aggregations/metrics/StatsAggregator.java | 9 +++-- .../metrics/StatsAggregatorFactory.java | 21 ++--------- .../aggregations/metrics/SumAggregator.java | 9 +++-- .../metrics/SumAggregatorFactory.java | 21 ++--------- .../metrics/AnalyticsAggregatorFactory.java | 20 ++++++++-- .../stringstats/StringStatsAggregator.java | 4 +- .../StringStatsAggregatorFactory.java | 17 +-------- 22 files changed, 80 insertions(+), 218 deletions(-) delete mode 100644 server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinMaxAggregatorSupplier.java diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/AbstractRangeAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/AbstractRangeAggregatorFactory.java index fa719d8c09d..2e75903d615 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/AbstractRangeAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/range/AbstractRangeAggregatorFactory.java @@ -20,7 +20,6 @@ package org.elasticsearch.search.aggregations.bucket.range; 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; @@ -37,7 +36,6 @@ import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; -import java.util.Arrays; import java.util.Map; public class AbstractRangeAggregatorFactory extends ValuesSourceAggregatorFactory { @@ -50,24 +48,10 @@ public class AbstractRangeAggregatorFactory extends ValuesSourc public static void registerAggregators(ValuesSourceRegistry.Builder builder, String aggregationName) { builder.register(aggregationName, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new RangeAggregatorSupplier() { - @Override - public Aggregator build(String name, - AggregatorFactories factories, - Numeric valuesSource, - DocValueFormat format, - InternalRange.Factory rangeFactory, - Range[] ranges, - boolean keyed, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new RangeAggregator(name, factories, valuesSource, format, rangeFactory, ranges, keyed, context, parent, - metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (RangeAggregatorSupplier) RangeAggregator::new); } + public AbstractRangeAggregatorFactory(String name, String aggregationTypeName, ValuesSourceConfig config, diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregator.java index 99598bb13f0..a13b075cc4c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregator.java @@ -31,6 +31,7 @@ import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -45,11 +46,11 @@ class AvgAggregator extends NumericMetricsAggregator.SingleValue { DoubleArray compensations; DocValueFormat format; - AvgAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, SearchContext context, - Aggregator parent, Map metadata) throws IOException { + AvgAggregator(String name, ValuesSourceConfig valuesSourceConfig, ValuesSource valuesSource, SearchContext context, + Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; - this.format = formatter; + this.valuesSource = (ValuesSource.Numeric) valuesSource; + this.format = valuesSourceConfig.format(); if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); counts = bigArrays.newLongArray(1, true); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorFactory.java index 83b86bdfaa0..c7d86ae1c35 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/AvgAggregatorFactory.java @@ -20,7 +20,6 @@ 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; @@ -28,14 +27,12 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; 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; -import java.util.Arrays; import java.util.Map; class AvgAggregatorFactory extends ValuesSourceAggregatorFactory { @@ -48,25 +45,16 @@ class AvgAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(AvgAggregationBuilder.NAME, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new MetricAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSource valuesSource, - DocValueFormat formatter, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new AvgAggregator(name, (Numeric) valuesSource, formatter, context, parent, metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (MetricAggregatorSupplier) AvgAggregator::new + ); } @Override protected Aggregator createUnmapped(SearchContext searchContext, Aggregator parent, Map metadata) throws IOException { - return new AvgAggregator(name, null, config.format(), searchContext, parent, metadata); + return new AvgAggregator(name, config, null, searchContext, parent, metadata); } @Override @@ -82,6 +70,6 @@ class AvgAggregatorFactory extends ValuesSourceAggregatorFactory { throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + aggregatorSupplier.getClass().toString() + "]"); } - return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), searchContext, parent, metadata); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregator.java index 9a76873aaea..7ec2ad6fd9e 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregator.java @@ -50,9 +50,9 @@ final class GeoBoundsAggregator extends MetricsAggregator { DoubleArray negRights; GeoBoundsAggregator(String name, SearchContext aggregationContext, Aggregator parent, - ValuesSource.GeoPoint valuesSource, boolean wrapLongitude, Map metadata) throws IOException { + ValuesSource valuesSource, boolean wrapLongitude, Map metadata) throws IOException { super(name, aggregationContext, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.GeoPoint) valuesSource; this.wrapLongitude = wrapLongitude; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorFactory.java index 97489b4110b..91d61ab6962 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoBoundsAggregatorFactory.java @@ -76,8 +76,6 @@ class GeoBoundsAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(GeoBoundsAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT, - (GeoBoundsAggregatorSupplier) (name, aggregationContext, parent, valuesSource, wrapLongitude, metadata) - -> new GeoBoundsAggregator(name, aggregationContext, parent, (ValuesSource.GeoPoint) valuesSource, - wrapLongitude, metadata)); + (GeoBoundsAggregatorSupplier) GeoBoundsAggregator::new); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregator.java index 6cc6500278f..87f9fed3425 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregator.java @@ -45,9 +45,9 @@ final class GeoCentroidAggregator extends MetricsAggregator { private LongArray counts; GeoCentroidAggregator(String name, SearchContext context, Aggregator parent, - ValuesSource.GeoPoint valuesSource, Map metadata) throws IOException { + ValuesSource valuesSource, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.GeoPoint) valuesSource; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); lonSum = bigArrays.newDoubleArray(1, true); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorFactory.java index 6bf496e4a9a..156fa4140bf 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/GeoCentroidAggregatorFactory.java @@ -71,7 +71,6 @@ class GeoCentroidAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(GeoCentroidAggregationBuilder.NAME, CoreValuesSourceType.GEOPOINT, - (GeoCentroidAggregatorSupplier) (name, context, parent, valuesSource, metadata) -> - new GeoCentroidAggregator(name, context, parent, (ValuesSource.GeoPoint) valuesSource, metadata)); + (GeoCentroidAggregatorSupplier) GeoCentroidAggregator::new); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregator.java index 012d5fee20a..7b2547fc187 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregator.java @@ -58,11 +58,11 @@ class MaxAggregator extends NumericMetricsAggregator.SingleValue { MaxAggregator(String name, ValuesSourceConfig config, - ValuesSource.Numeric valuesSource, + ValuesSource valuesSource, SearchContext context, Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.Numeric) valuesSource; if (valuesSource != null) { maxes = context.bigArrays().newDoubleArray(1, false); maxes.fill(0, maxes.size(), Double.NEGATIVE_INFINITY); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorFactory.java index 45c2753b392..e1a3477ee0a 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MaxAggregatorFactory.java @@ -27,32 +27,20 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; 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; -import java.util.Arrays; import java.util.Map; class MaxAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(MaxAggregationBuilder.NAME, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new MinMaxAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSourceConfig config, - ValuesSource valuesSource, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new MaxAggregator(name, config, (Numeric) valuesSource, context, parent, metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (MetricAggregatorSupplier) MaxAggregator::new); } MaxAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext, @@ -77,11 +65,10 @@ class MaxAggregatorFactory extends ValuesSourceAggregatorFactory { AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(), MaxAggregationBuilder.NAME); - if (aggregatorSupplier instanceof MinMaxAggregatorSupplier == false) { - throw new AggregationExecutionException("Registry miss-match - expected MinMaxAggregatorSupplier, found [" + + if (aggregatorSupplier instanceof MetricAggregatorSupplier == false) { + throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + aggregatorSupplier.getClass().toString() + "]"); } - return ((MinMaxAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, - metadata); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregator.java index 03055eebade..d3a6ba2355f 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MedianAbsoluteDeviationAggregator.java @@ -50,16 +50,16 @@ public class MedianAbsoluteDeviationAggregator extends NumericMetricsAggregator. private ObjectArray valueSketches; MedianAbsoluteDeviationAggregator(String name, - SearchContext context, + @Nullable ValuesSource valuesSource, + DocValueFormat format, + SearchContext context, Aggregator parent, Map metadata, - @Nullable ValuesSource.Numeric valuesSource, - DocValueFormat format, double compression) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.Numeric) valuesSource; this.format = Objects.requireNonNull(format); this.compression = compression; this.valueSketches = context.bigArrays().newObjectArray(1); 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 962239dfcc4..6acdf7eb853 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,7 +20,6 @@ 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; @@ -54,27 +53,7 @@ public class MedianAbsoluteDeviationAggregatorFactory extends ValuesSourceAggreg static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(MedianAbsoluteDeviationAggregationBuilder.NAME, - CoreValuesSourceType.NUMERIC, - new MedianAbsoluteDeviationAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSource valuesSource, - DocValueFormat format, - SearchContext context, - Aggregator parent, - Map metadata, - double compression) throws IOException { - return new MedianAbsoluteDeviationAggregator( - name, - context, - parent, - metadata, - (ValuesSource.Numeric) valuesSource, - format, - compression - ); - } - }); + CoreValuesSourceType.NUMERIC, (MedianAbsoluteDeviationAggregatorSupplier) MedianAbsoluteDeviationAggregator::new); } @Override @@ -84,11 +63,11 @@ public class MedianAbsoluteDeviationAggregatorFactory extends ValuesSourceAggreg return new MedianAbsoluteDeviationAggregator( name, + null, + config.format(), searchContext, parent, metadata, - null, - config.format(), compression ); } 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 9885f1f0dba..533419f5478 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 @@ -18,10 +18,10 @@ */ package org.elasticsearch.search.aggregations.metrics; -import org.elasticsearch.search.DocValueFormat; import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.support.AggregatorSupplier; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -29,8 +29,8 @@ import java.util.Map; public interface MetricAggregatorSupplier extends AggregatorSupplier { Aggregator build(String name, + ValuesSourceConfig valuesSourceConfig, ValuesSource valuesSource, - DocValueFormat format, SearchContext context, Aggregator parent, Map metadata) throws IOException; diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java index 7cb1f015342..fa8362fd4f9 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregator.java @@ -61,12 +61,12 @@ class MinAggregator extends NumericMetricsAggregator.SingleValue { MinAggregator(String name, ValuesSourceConfig config, - ValuesSource.Numeric valuesSource, + ValuesSource valuesSource, SearchContext context, Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.Numeric) valuesSource; if (valuesSource != null) { mins = context.bigArrays().newDoubleArray(1, false); mins.fill(0, mins.size(), Double.POSITIVE_INFINITY); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorFactory.java index 33fe688deb3..bb1a96edb40 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinAggregatorFactory.java @@ -27,32 +27,20 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; 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; -import java.util.Arrays; import java.util.Map; class MinAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(MinAggregationBuilder.NAME, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new MinMaxAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSourceConfig config, - ValuesSource valuesSource, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new MinAggregator(name, config, (Numeric) valuesSource, context, parent, metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (MetricAggregatorSupplier) MinAggregator::new); } MinAggregatorFactory(String name, ValuesSourceConfig config, QueryShardContext queryShardContext, @@ -77,10 +65,10 @@ class MinAggregatorFactory extends ValuesSourceAggregatorFactory { AggregatorSupplier aggregatorSupplier = queryShardContext.getValuesSourceRegistry().getAggregator(config.valueSourceType(), MinAggregationBuilder.NAME); - if (aggregatorSupplier instanceof MinMaxAggregatorSupplier == false) { - throw new AggregationExecutionException("Registry miss-match - expected MinMaxAggregatorSupplier, found [" + + if (aggregatorSupplier instanceof MetricAggregatorSupplier == false) { + throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + aggregatorSupplier.getClass().toString() + "]"); } - return ((MinMaxAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinMaxAggregatorSupplier.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinMaxAggregatorSupplier.java deleted file mode 100644 index 69e697d694f..00000000000 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/MinMaxAggregatorSupplier.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.aggregations.Aggregator; -import org.elasticsearch.search.aggregations.support.AggregatorSupplier; -import org.elasticsearch.search.aggregations.support.ValuesSource; -import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; -import org.elasticsearch.search.internal.SearchContext; - -import java.io.IOException; -import java.util.Map; - -public interface MinMaxAggregatorSupplier extends AggregatorSupplier { - Aggregator build(String name, - ValuesSourceConfig config, - ValuesSource valuesSource, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException; -} 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 a6f49131a3e..10579737510 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 @@ -31,6 +31,7 @@ import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -47,10 +48,10 @@ class StatsAggregator extends NumericMetricsAggregator.MultiValue { DoubleArray mins; DoubleArray maxes; - StatsAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat format, - SearchContext context, Aggregator parent, Map metadata) throws IOException { + StatsAggregator(String name, ValuesSourceConfig valuesSourceConfig, ValuesSource valuesSource, + SearchContext context, Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.Numeric) valuesSource; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); counts = bigArrays.newLongArray(1, true); @@ -61,7 +62,7 @@ class StatsAggregator extends NumericMetricsAggregator.MultiValue { maxes = bigArrays.newDoubleArray(1, false); maxes.fill(0, maxes.size(), Double.NEGATIVE_INFINITY); } - this.format = format; + this.format = valuesSourceConfig.format(); } @Override 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 fe598c3b7e9..c9a921f3a64 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,7 +20,6 @@ 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; @@ -28,14 +27,12 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; 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; -import java.util.Arrays; import java.util.Map; class StatsAggregatorFactory extends ValuesSourceAggregatorFactory { @@ -51,25 +48,15 @@ class StatsAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(StatsAggregationBuilder.NAME, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new MetricAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSource valuesSource, - DocValueFormat formatter, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new StatsAggregator(name, (Numeric) valuesSource, formatter, context, parent, metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (MetricAggregatorSupplier) StatsAggregator::new); } @Override protected Aggregator createUnmapped(SearchContext searchContext, Aggregator parent, Map metadata) throws IOException { - return new StatsAggregator(name, null, config.format(), searchContext, parent, metadata); + return new StatsAggregator(name, config, null, searchContext, parent, metadata); } @Override @@ -85,6 +72,6 @@ class StatsAggregatorFactory extends ValuesSourceAggregatorFactory { throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + aggregatorSupplier.getClass().toString() + "]"); } - return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), searchContext, parent, metadata); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); } } diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregator.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregator.java index 26bb3d82b1d..41294f3eff6 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregator.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregator.java @@ -30,6 +30,7 @@ import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.LeafBucketCollector; import org.elasticsearch.search.aggregations.LeafBucketCollectorBase; import org.elasticsearch.search.aggregations.support.ValuesSource; +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; import org.elasticsearch.search.internal.SearchContext; import java.io.IOException; @@ -43,11 +44,11 @@ public class SumAggregator extends NumericMetricsAggregator.SingleValue { private DoubleArray sums; private DoubleArray compensations; - SumAggregator(String name, ValuesSource.Numeric valuesSource, DocValueFormat formatter, SearchContext context, - Aggregator parent, Map metadata) throws IOException { + SumAggregator(String name, ValuesSourceConfig valuesSourceConfig, ValuesSource valuesSource, SearchContext context, + Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); - this.valuesSource = valuesSource; - this.format = formatter; + this.valuesSource = (ValuesSource.Numeric) valuesSource; + this.format = valuesSourceConfig.format(); if (valuesSource != null) { sums = context.bigArrays().newDoubleArray(1, true); compensations = context.bigArrays().newDoubleArray(1, true); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java index 73742aa5dca..6bdc08b211c 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/metrics/SumAggregatorFactory.java @@ -20,7 +20,6 @@ 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; @@ -28,14 +27,12 @@ import org.elasticsearch.search.aggregations.AggregatorFactory; 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; -import java.util.Arrays; import java.util.Map; class SumAggregatorFactory extends ValuesSourceAggregatorFactory { @@ -51,18 +48,8 @@ class SumAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(SumAggregationBuilder.NAME, - Arrays.asList(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), - new MetricAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSource valuesSource, - DocValueFormat formatter, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new SumAggregator(name, (Numeric) valuesSource, formatter, context, parent, metadata); - } - }); + org.elasticsearch.common.collect.List.of(CoreValuesSourceType.NUMERIC, CoreValuesSourceType.DATE, CoreValuesSourceType.BOOLEAN), + (MetricAggregatorSupplier) SumAggregator::new); } @Override @@ -70,7 +57,7 @@ class SumAggregatorFactory extends ValuesSourceAggregatorFactory { Aggregator parent, Map metadata) throws IOException { - return new SumAggregator(name, null, config.format(), searchContext, parent, metadata); + return new SumAggregator(name, config, null, searchContext, parent, metadata); } @Override @@ -86,6 +73,6 @@ class SumAggregatorFactory extends ValuesSourceAggregatorFactory { throw new AggregationExecutionException("Registry miss-match - expected MetricAggregatorSupplier, found [" + aggregatorSupplier.getClass().toString() + "]"); } - return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, valuesSource, config.format(), searchContext, parent, metadata); + return ((MetricAggregatorSupplier) aggregatorSupplier).build(name, config, valuesSource, searchContext, parent, metadata); } } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/metrics/AnalyticsAggregatorFactory.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/metrics/AnalyticsAggregatorFactory.java index c73d8223a1e..978fa5ec803 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/metrics/AnalyticsAggregatorFactory.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/aggregations/metrics/AnalyticsAggregatorFactory.java @@ -68,8 +68,14 @@ public class AnalyticsAggregatorFactory { public static void registerHistoBackedSumAggregator(ValuesSourceRegistry.Builder builder) { builder.register(SumAggregationBuilder.NAME, AnalyticsValuesSourceType.HISTOGRAM, - (MetricAggregatorSupplier) (name, valuesSource, format, context, parent, metadata) -> - new HistoBackedSumAggregator(name, (HistogramValuesSource.Histogram) valuesSource, format, context, parent, metadata) + (MetricAggregatorSupplier) (name, valuesSourceConfig, valuesSource, context, parent, metadata) -> new HistoBackedSumAggregator( + name, + (HistogramValuesSource.Histogram) valuesSource, + valuesSourceConfig.format(), + context, + parent, + metadata + ) ); } @@ -84,8 +90,14 @@ public class AnalyticsAggregatorFactory { public static void registerHistoBackedAverageAggregator(ValuesSourceRegistry.Builder builder) { builder.register(AvgAggregationBuilder.NAME, AnalyticsValuesSourceType.HISTOGRAM, - (MetricAggregatorSupplier) (name, valuesSource, format, context, parent, metadata) -> - new HistoBackedAvgAggregator(name, (HistogramValuesSource.Histogram) valuesSource, format, context, parent, metadata) + (MetricAggregatorSupplier) (name, valuesSourceConfig, valuesSource, context, parent, metadata) -> new HistoBackedAvgAggregator( + name, + (HistogramValuesSource.Histogram) valuesSource, + valuesSourceConfig.format(), + context, + parent, + metadata + ) ); } } diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregator.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregator.java index c7275c7f358..02c0a2b934d 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregator.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregator.java @@ -48,11 +48,11 @@ public class StringStatsAggregator extends MetricsAggregator { /** Map that stores the number of occurrences for each character. */ Map charOccurrences; - StringStatsAggregator(String name, boolean showDistribution, ValuesSource.Bytes valuesSource, DocValueFormat format, + StringStatsAggregator(String name, ValuesSource valuesSource, boolean showDistribution, DocValueFormat format, SearchContext context, Aggregator parent, Map metadata) throws IOException { super(name, context, parent, metadata); this.showDistribution = showDistribution; - this.valuesSource = valuesSource; + this.valuesSource = (ValuesSource.Bytes) valuesSource; if (valuesSource != null) { final BigArrays bigArrays = context.bigArrays(); count = bigArrays.newLongArray(1, true); diff --git a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorFactory.java b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorFactory.java index 5626f73d858..1ae7e8168c1 100644 --- a/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorFactory.java +++ b/x-pack/plugin/analytics/src/main/java/org/elasticsearch/xpack/analytics/stringstats/StringStatsAggregatorFactory.java @@ -7,7 +7,6 @@ package org.elasticsearch.xpack.analytics.stringstats; 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; @@ -38,26 +37,14 @@ class StringStatsAggregatorFactory extends ValuesSourceAggregatorFactory { static void registerAggregators(ValuesSourceRegistry.Builder builder) { builder.register(StringStatsAggregationBuilder.NAME, - CoreValuesSourceType.BYTES, new StringStatsAggregatorSupplier() { - @Override - public Aggregator build(String name, - ValuesSource valuesSource, - boolean showDistribution, - DocValueFormat format, - SearchContext context, - Aggregator parent, - Map metadata) throws IOException { - return new StringStatsAggregator(name, showDistribution, (ValuesSource.Bytes) valuesSource, - format, context, parent, metadata); - } - }); + CoreValuesSourceType.BYTES, (StringStatsAggregatorSupplier) StringStatsAggregator::new); } @Override protected Aggregator createUnmapped(SearchContext searchContext, Aggregator parent, Map metadata) throws IOException { - return new StringStatsAggregator(name, showDistribution,null, config.format(), searchContext, parent, metadata); + return new StringStatsAggregator(name, null, showDistribution, config.format(), searchContext, parent, metadata); } @Override