Add Composite to AggregationBuilders (#38207)

This commit is contained in:
Zachary Tong 2019-02-04 13:47:04 -05:00 committed by GitHub
parent 2c1eab2b8a
commit ab1150378b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 0 deletions

View File

@ -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<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();

View File

@ -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<CompositeValuesSourceBuilder<?>> sources) {
return new CompositeAggregationBuilder(name, sources);
}
}