From c12bd3fffb3edb5340cbae939558553e160beea1 Mon Sep 17 00:00:00 2001 From: Julio Valcarcel Date: Wed, 13 Feb 2019 13:21:11 -0500 Subject: [PATCH] Allows for a composite aggregation to be a child of a filter aggregation A composite aggregation should be allowed to be a grandchild of a nested aggregation only if it's parent is a filter aggregation. Relates to #37178 --- .../composite/CompositeAggregationBuilder.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregationBuilder.java b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregationBuilder.java index 69910d21ed8ad..32f933ddf1aaf 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregationBuilder.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/bucket/composite/CompositeAggregationBuilder.java @@ -29,6 +29,7 @@ import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.AggregatorFactory; +import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregatorFactory; import org.elasticsearch.search.aggregations.bucket.nested.NestedAggregatorFactory; import org.elasticsearch.search.internal.SearchContext; @@ -155,13 +156,22 @@ public int size() { /** * Returns null if the provided factory and his parents are compatible with * this aggregator or the instance of the parent's factory that is incompatible with - * the composite aggregation. + * the composite aggregation. A composite aggregation can be used as a child of a nested + * aggregation and as a child of a filter only if the parent of the filter is a + * nested aggregation. + * + * @return null if compatible */ private AggregatorFactory checkParentIsNullOrNested(AggregatorFactory factory) { if (factory == null) { return null; - } else if (factory instanceof NestedAggregatorFactory) { - return checkParentIsNullOrNested(factory.getParent()); + } else if (factory instanceof NestedAggregatorFactory && + factory.getParent() == null) { + return null; + } else if (factory instanceof FilterAggregatorFactory && + factory.getParent() instanceof NestedAggregatorFactory && + factory.getParent().getParent() == null) { + return null; } else { return factory; }