Search in sources :

Example 1 with CompositeValuesSourceBuilder

use of org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder in project OpenSearch by opensearch-project.

the class SearchIT method testSearchWithCompositeAgg.

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());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) CompositeValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)1 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchResponse (org.opensearch.action.search.SearchResponse)1 CompositeAggregation (org.opensearch.search.aggregations.bucket.composite.CompositeAggregation)1 CompositeValuesSourceBuilder (org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder)1 TermsValuesSourceBuilder (org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder)1 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)1