use of org.opensearch.search.aggregations.bucket.composite.CompositeAggregation 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.CompositeAggregation in project ml-commons by opensearch-project.
the class AnomalyLocalizerImpl method onNewEntryResponse.
/**
* Chooses entities from the new bucket that contribute the most to the overall change.
*/
private void onNewEntryResponse(SearchResponse response, AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Result result, AnomalyLocalizationOutput.Bucket outputBucket, PriorityQueue<AnomalyLocalizationOutput.Entity> queue, AnomalyLocalizationOutput output, ActionListener<AnomalyLocalizationOutput> listener) {
Optional<CompositeAggregation> respAgg = Optional.ofNullable(response.getAggregations()).map(aggs -> (CompositeAggregation) aggs.get(agg.getName()));
for (CompositeAggregation.Bucket bucket : respAgg.map(a -> a.getBuckets()).orElse(Collections.emptyList())) {
List<String> key = toStringKey(bucket.getKey(), input);
AnomalyLocalizationOutput.Entity entity = new AnomalyLocalizationOutput.Entity();
entity.setKey(key);
entity.setNewValue(getDoubleValue((SingleValue) bucket.getAggregations().get(agg.getName())));
entity.setBaseValue(outputBucket.getBase().get().getCounter().get().estimate(key));
entity.setContributionValue(entity.getNewValue() - entity.getBaseValue());
if (queue.size() < input.getNumOutputs()) {
queue.add(entity);
} else if (queue.comparator().compare(queue.peek(), entity) < 0) {
queue.poll();
queue.add(entity);
}
}
Optional<Map<String, Object>> afterKey = respAgg.map(r -> r.afterKey());
if (afterKey.isPresent()) {
processNewEntry(input, agg, result, outputBucket, afterKey, queue, output, listener);
} else {
List<List<String>> keys = queue.stream().map(AnomalyLocalizationOutput.Entity::getKey).collect(Collectors.toList());
SearchRequest request = newSearchRequestForEntityKeys(input, agg, outputBucket, keys);
client.search(request, wrap(r -> onEntityKeysResponse(r, input, agg, result, outputBucket, queue, output, listener), listener::onFailure));
}
}
use of org.opensearch.search.aggregations.bucket.composite.CompositeAggregation in project ml-commons by opensearch-project.
the class AnomalyLocalizerImplTests method setup.
@Before
@SuppressWarnings("unchecked")
public void setup() {
MockitoAnnotations.openMocks(this);
settings = Settings.builder().build();
anomalyLocalizer = new AnomalyLocalizerImpl(client, settings);
input = new AnomalyLocalizationInput(indexName, Arrays.asList(attributeFieldNameOne), Arrays.asList(agg), timeFieldName, startTime, endTime, minTimeInterval, numOutput, Optional.empty(), Optional.empty());
when(valueOne.value()).thenReturn(0.);
when(valueOne.getName()).thenReturn(agg.getName());
SearchResponse respOne = mock(SearchResponse.class);
when(respOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
MultiSearchResponse.Item itemOne = new MultiSearchResponse.Item(respOne, null);
when(valueTwo.value()).thenReturn(10.);
when(valueTwo.getName()).thenReturn(agg.getName());
SearchResponse respTwo = mock(SearchResponse.class);
when(respTwo.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueTwo)));
MultiSearchResponse.Item itemTwo = new MultiSearchResponse.Item(respTwo, null);
MultiSearchResponse multiSearchResponse = new MultiSearchResponse(new MultiSearchResponse.Item[] { itemOne, itemTwo }, 0);
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<MultiSearchResponse> listener = (ActionListener<MultiSearchResponse>) args[1];
listener.onResponse(multiSearchResponse);
return null;
}).when(client).multiSearch(any(), any());
CompositeAggregation.Bucket bucketOne = mock(CompositeAggregation.Bucket.class);
Map<String, Object> bucketOneKey = new HashMap<>();
String bucketOneKeyValue = "bucketOneKeyValue";
bucketOneKey.put(attributeFieldNameOne, bucketOneKeyValue);
when(bucketOne.getKey()).thenReturn(bucketOneKey);
when(bucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
CompositeAggregation compositeOne = mock(CompositeAggregation.class);
when(compositeOne.getName()).thenReturn(agg.getName());
doReturn(Arrays.asList(bucketOne)).when(compositeOne).getBuckets();
when(compositeOne.afterKey()).thenReturn(bucketOneKey);
SearchResponse respBucketOne = mock(SearchResponse.class);
when(respBucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(compositeOne))).thenReturn(new Aggregations(Collections.emptyList()));
CompositeAggregation.Bucket bucketOneNew = mock(CompositeAggregation.Bucket.class);
when(bucketOneNew.getKey()).thenReturn(bucketOneKey);
when(bucketOneNew.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueTwo)));
Map<String, Object> bucketTwoKey = new HashMap<>();
String bucketTwoKeyValue = "bucketTwoKeyValue";
bucketTwoKey.put(attributeFieldNameOne, bucketTwoKeyValue);
when(valueThree.value()).thenReturn(0.);
when(valueThree.getName()).thenReturn(agg.getName());
CompositeAggregation.Bucket bucketTwoNew = mock(CompositeAggregation.Bucket.class);
when(bucketTwoNew.getKey()).thenReturn(bucketTwoKey);
when(bucketTwoNew.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueThree)));
CompositeAggregation compositeTwo = mock(CompositeAggregation.class);
when(compositeTwo.getName()).thenReturn(agg.getName());
doReturn(Arrays.asList(bucketTwoNew, bucketOneNew, bucketTwoNew)).when(compositeTwo).getBuckets();
when(compositeTwo.afterKey()).thenReturn(bucketOneKey);
SearchResponse respBucketTwo = mock(SearchResponse.class);
when(respBucketTwo.getAggregations()).thenReturn(new Aggregations(Arrays.asList(compositeTwo))).thenReturn(new Aggregations(Collections.emptyList()));
Filters.Bucket filterBucketOne = mock(Filters.Bucket.class);
when(filterBucketOne.getKeyAsString()).thenReturn(String.valueOf(0));
when(filterBucketOne.getAggregations()).thenReturn(new Aggregations(Arrays.asList(valueOne)));
Filters filters = mock(Filters.class);
when(filters.getName()).thenReturn(agg.getName());
doReturn(Arrays.asList(filterBucketOne)).when(filters).getBuckets();
SearchResponse filtersResp = mock(SearchResponse.class);
when(filtersResp.getAggregations()).thenReturn(new Aggregations(Arrays.asList(filters)));
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(respBucketOne);
return null;
}).doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(respBucketOne);
return null;
}).doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(respBucketTwo);
return null;
}).doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(respBucketTwo);
return null;
}).doAnswer(invocation -> {
Object[] args = invocation.getArguments();
ActionListener<SearchResponse> listener = (ActionListener<SearchResponse>) args[1];
listener.onResponse(filtersResp);
return null;
}).when(client).search(any(), any());
expectedOutput = new AnomalyLocalizationOutput();
AnomalyLocalizationOutput.Result result = new AnomalyLocalizationOutput.Result();
expectedBucketOne = new AnomalyLocalizationOutput.Bucket();
expectedBucketOne.setStartTime(0);
expectedBucketOne.setEndTime(1);
expectedBucketOne.setOverallAggValue(0);
expectedBucketTwo = new AnomalyLocalizationOutput.Bucket();
expectedBucketTwo.setStartTime(1);
expectedBucketTwo.setEndTime(2);
expectedBucketTwo.setOverallAggValue(10);
entity = new AnomalyLocalizationOutput.Entity();
entity.setKey(Arrays.asList(bucketOneKeyValue));
entity.setNewValue(valueTwo.value());
entity.setBaseValue(valueOne.value());
entity.setContributionValue(valueTwo.value());
expectedBucketTwo.setEntities(Arrays.asList(entity));
result.getBuckets().add(expectedBucketOne);
result.getBuckets().add(expectedBucketTwo);
expectedOutput.getResults().put(agg.getName(), result);
}
Aggregations