use of org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder 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());
}
use of org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder in project ml-commons by opensearch-project.
the class AnomalyLocalizerImpl method newSearchRequestForEntry.
private SearchRequest newSearchRequestForEntry(AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Bucket bucket, Optional<Map<String, Object>> afterKey) {
RangeQueryBuilder timeRangeFilter = new RangeQueryBuilder(input.getTimeFieldName()).from(bucket.getStartTime(), true).to(bucket.getEndTime(), true);
BoolQueryBuilder filter = QueryBuilders.boolQuery().filter(timeRangeFilter);
input.getFilterQuery().ifPresent(q -> filter.filter(q));
CompositeAggregationBuilder compositeAgg = new CompositeAggregationBuilder(agg.getName(), input.getAttributeFieldNames().stream().map(name -> new TermsValuesSourceBuilder(name).field(name)).collect(Collectors.toList())).size(MAX_BUCKET_SETTING.get(this.settings));
compositeAgg.subAggregation(agg);
if (afterKey.isPresent()) {
compositeAgg.aggregateAfter(afterKey.get());
}
SearchSourceBuilder search = new SearchSourceBuilder().size(0).query(filter).aggregation(compositeAgg);
SearchRequest searchRequest = new SearchRequest(new String[] { input.getIndexName() }, search);
return searchRequest;
}
Aggregations