Search in sources :

Example 1 with KeyedFilter

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

the class FiltersIT method testEmptyAggregationWithOtherBucket.

public void testEmptyAggregationWithOtherBucket() 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("foo", matchAllQuery())).otherBucket(true).otherBucketKey("bar"))).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 other = filters.getBucketByKey("bar");
    assertThat(other, Matchers.notNullValue());
    assertThat(other.getKeyAsString(), equalTo("bar"));
    assertThat(other.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 2 with KeyedFilter

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

the class FiltersIT method testOtherWithSubAggregation.

public void testOtherWithSubAggregation() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).otherBucket(true).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(3));
    assertThat(((InternalAggregation) filters).getProperty("_bucket_count"), equalTo(3));
    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(propertiesKeys[0], equalTo("tag1"));
    assertThat(propertiesDocCounts[0], equalTo((long) numTag1Docs));
    assertThat(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));
    bucket = filters.getBucketByKey("_other_");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
    sum = 0;
    for (int i = numTag1Docs + numTag2Docs; i < numDocs; ++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 / numOtherDocs));
    assertThat(propertiesKeys[2], equalTo("_other_"));
    assertThat(propertiesDocCounts[2], equalTo((long) numOtherDocs));
    assertThat(propertiesCounts[2], equalTo((double) sum / numOtherDocs));
}
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 3 with KeyedFilter

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

the class FiltersIT method testOtherNamedBucket.

public void testOtherNamedBucket() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).otherBucket(true).otherBucketKey("foobar")).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(3));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
    bucket = filters.getBucketByKey("tag2");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
    bucket = filters.getBucketByKey("foobar");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 4 with KeyedFilter

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

the class FiltersIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("all", emptyFilter), new KeyedFilter("tag1", termQuery("tag", "tag1"))))).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    Filters.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with KeyedFilter

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

Aggregations

KeyedFilter (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)17 SearchResponse (org.opensearch.action.search.SearchResponse)9 Filters (org.opensearch.search.aggregations.bucket.filter.Filters)9 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)8 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)4 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 FiltersAggregationBuilder (org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)4 ArrayList (java.util.ArrayList)3 AbstractQueryBuilder.parseInnerQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 Avg (org.opensearch.search.aggregations.metrics.Avg)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 PriorityQueue (java.util.PriorityQueue)1