Skip to content

Commit

Permalink
Add Composite to AggregationBuilders (#38207)
Browse files Browse the repository at this point in the history
  • Loading branch information
polyfractal authored Feb 4, 2019
1 parent 2c1eab2 commit ab11503
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@
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;
Expand Down Expand Up @@ -275,6 +279,34 @@ public void testSearchWithTermsAgg() throws IOException {
assertEquals(0, type2.getAggregations().asList().size());
}

public void testSearchWithCompositeAgg() throws IOException {
SearchRequest searchRequest = new SearchRequest();
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
List<CompositeValuesSourceBuilder<?>> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
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;
Expand Down Expand Up @@ -88,6 +90,7 @@
import org.elasticsearch.search.aggregations.metrics.MedianAbsoluteDeviationAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MedianAbsoluteDeviation;

import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -368,4 +371,11 @@ public static GeoCentroidAggregationBuilder geoCentroid(String name) {
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<CompositeValuesSourceBuilder<?>> sources) {
return new CompositeAggregationBuilder(name, sources);
}
}

0 comments on commit ab11503

Please sign in to comment.