Search in sources :

Example 6 with Filters

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

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")).execute().actionGet();
    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.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 7 with Filters

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

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"))).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 other = filters.getBucketByKey("bar");
    assertThat(other, Matchers.notNullValue());
    assertThat(other.getKeyAsString(), equalTo("bar"));
    assertThat(other.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 8 with Filters

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

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"))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(3));
    assertThat(filters.getProperty("_bucket_count"), equalTo(3));
    Object[] propertiesKeys = (Object[]) filters.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) filters.getProperty("_count");
    Object[] propertiesCounts = (Object[]) 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.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with Filters

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

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"))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(2));
    assertThat(filters.getProperty("_bucket_count"), equalTo(2));
    Object[] propertiesKeys = (Object[]) filters.getProperty("_key");
    Object[] propertiesDocCounts = (Object[]) filters.getProperty("_count");
    Object[] propertiesCounts = (Object[]) 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.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 10 with Filters

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

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.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)

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