From ae76239b0aefe65991e50572c6d0b0039f2c1c0d Mon Sep 17 00:00:00 2001 From: Colin Goodheart-Smithe Date: Thu, 12 Feb 2015 08:41:21 +0000 Subject: [PATCH] AggregatorFactories now stores reducers as well as aggregators These reducers will be passed through from the AggregatorParser --- .../aggregations/AggregatorFactories.java | 20 ++++++++++++++++--- .../aggregations/AggregatorFactory.java | 8 +------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java b/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java index 10ea7f74c2c..795d9b5724c 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java +++ b/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactories.java @@ -19,6 +19,7 @@ package org.elasticsearch.search.aggregations; import org.elasticsearch.ElasticsearchIllegalArgumentException; +import org.elasticsearch.search.aggregations.reducers.Reducer; import org.elasticsearch.search.aggregations.support.AggregationContext; import java.io.IOException; @@ -35,13 +36,19 @@ public class AggregatorFactories { public static final AggregatorFactories EMPTY = new Empty(); private AggregatorFactory[] factories; + private List reducers; public static Builder builder() { return new Builder(); } - private AggregatorFactories(AggregatorFactory[] factories) { + private AggregatorFactories(AggregatorFactory[] factories, List reducers) { this.factories = factories; + this.reducers = reducers; + } + + public List reducers() { + return reducers; } private static Aggregator createAndRegisterContextAware(AggregationContext context, AggregatorFactory factory, Aggregator parent, boolean collectsFromSingleBucket) throws IOException { @@ -100,9 +107,10 @@ public class AggregatorFactories { private static final AggregatorFactory[] EMPTY_FACTORIES = new AggregatorFactory[0]; private static final Aggregator[] EMPTY_AGGREGATORS = new Aggregator[0]; + private static final List EMPTY_REDUCERS = new ArrayList<>(); private Empty() { - super(EMPTY_FACTORIES); + super(EMPTY_FACTORIES, EMPTY_REDUCERS); } @Override @@ -121,6 +129,7 @@ public class AggregatorFactories { private final Set names = new HashSet<>(); private final List factories = new ArrayList<>(); + private List reducers = new ArrayList<>(); public Builder add(AggregatorFactory factory) { if (!names.add(factory.name)) { @@ -130,11 +139,16 @@ public class AggregatorFactories { return this; } + public Builder setReducers(List reducers) { + this.reducers = reducers; + return this; + } + public AggregatorFactories build() { if (factories.isEmpty()) { return EMPTY; } - return new AggregatorFactories(factories.toArray(new AggregatorFactory[factories.size()])); + return new AggregatorFactories(factories.toArray(new AggregatorFactory[factories.size()]), this.reducers); } } } diff --git a/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactory.java b/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactory.java index f49a328fd16..3db9e5ddd69 100644 --- a/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactory.java +++ b/src/main/java/org/elasticsearch/search/aggregations/AggregatorFactory.java @@ -28,7 +28,6 @@ import org.elasticsearch.search.aggregations.support.AggregationContext; import org.elasticsearch.search.internal.SearchContext.Lifetime; import java.io.IOException; -import java.util.Collections; import java.util.List; import java.util.Map; @@ -41,7 +40,6 @@ public abstract class AggregatorFactory { protected String type; protected AggregatorFactory parent; protected AggregatorFactories factories = AggregatorFactories.EMPTY; - protected List reducers = Collections.emptyList(); protected Map metaData; /** @@ -97,7 +95,7 @@ public abstract class AggregatorFactory { * @return The created aggregator */ public final Aggregator create(AggregationContext context, Aggregator parent, boolean collectsFromSingleBucket) throws IOException { - return createInternal(context, parent, collectsFromSingleBucket, this.reducers, this.metaData); + return createInternal(context, parent, collectsFromSingleBucket, this.factories.reducers(), this.metaData); } public void doValidate() { @@ -108,10 +106,6 @@ public abstract class AggregatorFactory { } - public void setReducers(List reducers) { - this.reducers = reducers; - } - /** * Utility method. Given an {@link AggregatorFactory} that creates {@link Aggregator}s that only know how