Search in sources :

Example 1 with CompositeAggregation

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

Example 2 with CompositeAggregation

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));
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SneakyThrows(lombok.SneakyThrows) PriorityQueue(java.util.PriorityQueue) NotifyOnceListener(org.opensearch.action.NotifyOnceListener) Executable(org.opensearch.ml.engine.Executable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Input(org.opensearch.ml.common.parameter.Input) ArrayList(java.util.ArrayList) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) Output(org.opensearch.ml.common.parameter.Output) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) LatchedActionListener(org.opensearch.action.LatchedActionListener) KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Map(java.util.Map) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) MAX_BUCKET_SETTING(org.opensearch.search.aggregations.MultiBucketConsumerService.MAX_BUCKET_SETTING) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) Settings(org.opensearch.common.settings.Settings) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) Collectors(java.util.stream.Collectors) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) Optional(java.util.Optional) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) Collections(java.util.Collections) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with CompositeAggregation

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);
}
Also used : Arrays(java.util.Arrays) Mock(org.mockito.Mock) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) MockitoAnnotations(org.mockito.MockitoAnnotations) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) ArgumentCaptor(org.mockito.ArgumentCaptor) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) Mockito.doReturn(org.mockito.Mockito.doReturn) Before(org.junit.Before) Client(org.opensearch.client.Client) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) Test(org.junit.Test) Settings(org.opensearch.common.settings.Settings) Mockito.when(org.mockito.Mockito.when) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Mockito.verify(org.mockito.Mockito.verify) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) QueryBuilder(org.opensearch.index.query.QueryBuilder) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) Optional(java.util.Optional) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) HashMap(java.util.HashMap) Aggregations(org.opensearch.search.aggregations.Aggregations) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) ActionListener(org.opensearch.action.ActionListener) Before(org.junit.Before)

Aggregations

MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)3 SearchResponse (org.opensearch.action.search.SearchResponse)3 CompositeAggregation (org.opensearch.search.aggregations.bucket.composite.CompositeAggregation)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Map (java.util.Map)2 Optional (java.util.Optional)2 ActionListener (org.opensearch.action.ActionListener)2 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 Client (org.opensearch.client.Client)2 Settings (org.opensearch.common.settings.Settings)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 AggregationBuilders (org.opensearch.search.aggregations.AggregationBuilders)2 TermsValuesSourceBuilder (org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder)2 Filters (org.opensearch.search.aggregations.bucket.filter.Filters)2 SingleValue (org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue)2 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)1