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