Search in sources :

Example 1 with Filters

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

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

the class FiltersIT method testOtherNonKeyed.

public void testOtherNonKeyed() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", termQuery("tag", "tag1"), termQuery("tag", "tag2")).otherBucket(true)).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(3));
    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));
    bucket = itr.next();
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
}
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 3 with Filters

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

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

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

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