Search in sources :

Example 6 with Filters

use of org.opensearch.search.aggregations.bucket.filter.Filters in project OpenSearch by opensearch-project.

the class FiltersIT method testWithSubAggregation.

public void testWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).subAggregation(avg("avg_value").field("value"))).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(2));
    assertThat(((InternalAggregation) filters).getProperty("_bucket_count"), equalTo(2));
    Object[] propertiesKeys = (Object[]) ((InternalAggregation) filters).getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) ((InternalAggregation) filters).getProperty("_count");
    Object[] propertiesCounts = (Object[]) ((InternalAggregation) filters).getProperty("avg_value.value");
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
    long sum = 0;
    for (int i = 0; i < numTag1Docs; ++i) {
        sum += i + 1;
    }
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    Avg avgValue = bucket.getAggregations().get("avg_value");
    assertThat(avgValue, notNullValue());
    assertThat(avgValue.getName(), equalTo("avg_value"));
    assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs));
    assertThat((String) propertiesKeys[0], equalTo("tag1"));
    assertThat((long) propertiesDocCounts[0], equalTo((long) numTag1Docs));
    assertThat((double) propertiesCounts[0], equalTo((double) sum / numTag1Docs));
    bucket = filters.getBucketByKey("tag2");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
    sum = 0;
    for (int i = numTag1Docs; i < (numTag1Docs + numTag2Docs); ++i) {
        sum += i;
    }
    assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
    avgValue = bucket.getAggregations().get("avg_value");
    assertThat(avgValue, notNullValue());
    assertThat(avgValue.getName(), equalTo("avg_value"));
    assertThat(avgValue.getValue(), equalTo((double) sum / numTag2Docs));
    assertThat(propertiesKeys[1], equalTo("tag2"));
    assertThat(propertiesDocCounts[1], equalTo((long) numTag2Docs));
    assertThat(propertiesCounts[1], equalTo((double) sum / numTag2Docs));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) Avg(org.opensearch.search.aggregations.metrics.Avg) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with Filters

use of org.opensearch.search.aggregations.bucket.filter.Filters in project OpenSearch by opensearch-project.

the class FiltersIT method testSimpleNonKeyed.

public void testSimpleNonKeyed() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", termQuery("tag", "tag1"), termQuery("tag", "tag2"))).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(2));
    Collection<? extends Filters.Bucket> buckets = filters.getBuckets();
    Iterator<? extends Filters.Bucket> itr = buckets.iterator();
    Filters.Bucket bucket = itr.next();
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
    bucket = itr.next();
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
}
Also used : Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 8 with Filters

use of org.opensearch.search.aggregations.bucket.filter.Filters in project OpenSearch by opensearch-project.

the class FiltersIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(filters("filters", new KeyedFilter("all", matchAllQuery())))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Filters filters = bucket.getAggregations().get("filters");
    assertThat(filters, notNullValue());
    Filters.Bucket all = filters.getBucketByKey("all");
    assertThat(all, Matchers.notNullValue());
    assertThat(all.getKeyAsString(), equalTo("all"));
    assertThat(all.getDocCount(), is(0L));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 9 with Filters

use of org.opensearch.search.aggregations.bucket.filter.Filters 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)

Example 10 with Filters

use of org.opensearch.search.aggregations.bucket.filter.Filters in project OpenSearch by opensearch-project.

the class FiltersIT method testAsSubAggregation.

public void testAsSubAggregation() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field("value").interval(2L).subAggregation(filters("filters", matchAllQuery()))).get();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getBuckets().size(), greaterThanOrEqualTo(1));
    for (Histogram.Bucket bucket : histo.getBuckets()) {
        Filters filters = bucket.getAggregations().get("filters");
        assertThat(filters, notNullValue());
        assertThat(filters.getBuckets().size(), equalTo(1));
        Filters.Bucket filterBucket = filters.getBuckets().get(0);
        assertEquals(bucket.getDocCount(), filterBucket.getDocCount());
    }
}
Also used : Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

SearchResponse (org.opensearch.action.search.SearchResponse)13 Filters (org.opensearch.search.aggregations.bucket.filter.Filters)13 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)11 KeyedFilter (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)9 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 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)2 Client (org.opensearch.client.Client)2 Settings (org.opensearch.common.settings.Settings)2 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)2 QueryBuilder (org.opensearch.index.query.QueryBuilder)2 AggregationBuilder (org.opensearch.search.aggregations.AggregationBuilder)2 AggregationBuilders (org.opensearch.search.aggregations.AggregationBuilders)2 CompositeAggregation (org.opensearch.search.aggregations.bucket.composite.CompositeAggregation)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 Avg (org.opensearch.search.aggregations.metrics.Avg)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1