Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -1120,3 +1120,44 @@ setup:
- match: { aggregations.courses.sessions.names.buckets.0.doc_count: 4}
- match: { aggregations.courses.sessions.names.buckets.1.key.kw: "spr2021" }
- match: { aggregations.courses.sessions.names.buckets.1.doc_count: 4}

---
"Nested then filter then nested then terms":
- do:
search:
rest_total_hits_as_int: true
index: verynested
body:
"aggregations": {
"courses": {
"nested": { "path": "courses" },
"aggregations": {
"highpass_filter": {
"filter": { "range": {"courses.credits": { "gt": 1 }}},
"aggregations": {
"sessions": {
"nested": { "path": "courses.sessions" },
"aggregations": {
"names": {
"composite": {
"sources": [
"kw": {"terms": { "field": "courses.sessions.semester" }}
]
}
}
}
}
}
}
}
}
}
- match: {hits.total: 2}
- match: {aggregations.courses.doc_count: 4}
- match: {aggregations.courses.highpass_filter.doc_count: 3}
- match: {aggregations.courses.highpass_filter.sessions.doc_count: 6}
- length: { aggregations.courses.highpass_filter.sessions.names.buckets: 2 }
- match: { aggregations.courses.highpass_filter.sessions.names.buckets.0.key.kw: "fall2020" }
- match: { aggregations.courses.highpass_filter.sessions.names.buckets.0.doc_count: 3}
- match: { aggregations.courses.highpass_filter.sessions.names.buckets.1.key.kw: "spr2021" }
- match: { aggregations.courses.highpass_filter.sessions.names.buckets.1.doc_count: 3}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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.aggregations.support.AggregationContext;
import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry;
Expand Down Expand Up @@ -160,11 +161,11 @@ public BucketCardinality bucketCardinality() {
* this aggregator or the instance of the parent's factory that is incompatible with
* the composite aggregation.
*/
private AggregatorFactory checkParentIsNullOrNested(AggregatorFactory factory) {
private AggregatorFactory validateParentAggregations(AggregatorFactory factory) {
if (factory == null) {
return null;
} else if (factory instanceof NestedAggregatorFactory) {
return checkParentIsNullOrNested(factory.getParent());
} else if (factory instanceof NestedAggregatorFactory || factory instanceof FilterAggregatorFactory) {
return validateParentAggregations(factory.getParent());
} else {
return factory;
}
Expand Down Expand Up @@ -195,7 +196,7 @@ private static void validateSources(List<CompositeValuesSourceBuilder<?>> source
@Override
protected AggregatorFactory doBuild(AggregationContext context, AggregatorFactory parent,
AggregatorFactories.Builder subfactoriesBuilder) throws IOException {
AggregatorFactory invalid = checkParentIsNullOrNested(parent);
AggregatorFactory invalid = validateParentAggregations(parent);
if (invalid != null) {
throw new IllegalArgumentException("[composite] aggregation cannot be used with a parent aggregation of" +
" type: [" + invalid.getClass().getSimpleName() + "]");
Expand Down