diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java index bf16ce93c14..54826e963cb 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java @@ -59,7 +59,11 @@ import org.elasticsearch.script.mustache.MultiSearchTemplateResponse.Item; import org.elasticsearch.script.mustache.SearchTemplateRequest; import org.elasticsearch.script.mustache.SearchTemplateResponse; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.BucketOrder; +import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregation; +import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; +import org.elasticsearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder; import org.elasticsearch.search.aggregations.bucket.range.Range; import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.terms.Terms; @@ -275,6 +279,34 @@ public class SearchIT extends ESRestHighLevelClientTestCase { assertEquals(0, type2.getAggregations().asList().size()); } + public void testSearchWithCompositeAgg() throws IOException { + SearchRequest searchRequest = new SearchRequest(); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + List> sources + = Collections.singletonList(new TermsValuesSourceBuilder("terms").field("type.keyword").missingBucket(true).order("asc")); + searchSourceBuilder.aggregation(AggregationBuilders.composite("composite", sources)); + searchSourceBuilder.size(0); + searchRequest.source(searchSourceBuilder); + searchRequest.indices("index"); + SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync); + assertSearchHeader(searchResponse); + assertNull(searchResponse.getSuggest()); + assertEquals(Collections.emptyMap(), searchResponse.getProfileResults()); + assertEquals(0, searchResponse.getHits().getHits().length); + assertEquals(Float.NaN, searchResponse.getHits().getMaxScore(), 0f); + CompositeAggregation compositeAgg = searchResponse.getAggregations().get("composite"); + assertEquals("composite", compositeAgg.getName()); + assertEquals(2, compositeAgg.getBuckets().size()); + CompositeAggregation.Bucket bucket1 = compositeAgg.getBuckets().get(0); + assertEquals(3, bucket1.getDocCount()); + assertEquals("{terms=type1}", bucket1.getKeyAsString()); + assertEquals(0, bucket1.getAggregations().asList().size()); + CompositeAggregation.Bucket bucket2 = compositeAgg.getBuckets().get(1); + assertEquals(2, bucket2.getDocCount()); + assertEquals("{terms=type2}", bucket2.getKeyAsString()); + assertEquals(0, bucket2.getAggregations().asList().size()); + } + public void testSearchWithRangeAgg() throws IOException { { SearchRequest searchRequest = new SearchRequest(); diff --git a/server/src/main/java/org/elasticsearch/search/aggregations/AggregationBuilders.java b/server/src/main/java/org/elasticsearch/search/aggregations/AggregationBuilders.java index d78e42ba896..72ac99d94b9 100644 --- a/server/src/main/java/org/elasticsearch/search/aggregations/AggregationBuilders.java +++ b/server/src/main/java/org/elasticsearch/search/aggregations/AggregationBuilders.java @@ -23,6 +23,8 @@ import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrix; import org.elasticsearch.search.aggregations.bucket.adjacency.AdjacencyMatrixAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; +import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; import org.elasticsearch.search.aggregations.bucket.filter.Filter; import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder; import org.elasticsearch.search.aggregations.bucket.filter.Filters; @@ -88,6 +90,7 @@ import org.elasticsearch.search.aggregations.metrics.WeightedAvgAggregationBuild import org.elasticsearch.search.aggregations.metrics.MedianAbsoluteDeviationAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.MedianAbsoluteDeviation; +import java.util.List; import java.util.Map; /** @@ -368,4 +371,11 @@ public class AggregationBuilders { public static ScriptedMetricAggregationBuilder scriptedMetric(String name) { return new ScriptedMetricAggregationBuilder(name); } + + /** + * Create a new {@link CompositeAggregationBuilder} aggregation with the given name. + */ + public static CompositeAggregationBuilder composite(String name, List> sources) { + return new CompositeAggregationBuilder(name, sources); + } }