Search in sources :

Example 1 with Filters

use of org.elasticsearch.search.aggregations.bucket.filters.Filters in project elasticsearch by elastic.

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())))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), 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.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with Filters

use of org.elasticsearch.search.aggregations.bucket.filters.Filters in project elasticsearch by elastic.

the class FiltersIT method testSimpleNonKeyed.

public void testSimpleNonKeyed() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", termQuery("tag", "tag1"), termQuery("tag", "tag2"))).execute().actionGet();
    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.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 3 with Filters

use of org.elasticsearch.search.aggregations.bucket.filters.Filters in project elasticsearch by elastic.

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)).execute().actionGet();
    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.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with Filters

use of org.elasticsearch.search.aggregations.bucket.filters.Filters in project elasticsearch by elastic.

the class FiltersIT method testSimple.

public void testSimple() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2"))))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(2));
    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));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with Filters

use of org.elasticsearch.search.aggregations.bucket.filters.Filters in project elasticsearch by elastic.

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"))))).execute().actionGet();
    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.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)11 Filters (org.elasticsearch.search.aggregations.bucket.filters.Filters)11 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)11 KeyedFilter (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)8 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)3 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1