Search in sources :

Example 26 with Filter

use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.

the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars() throws Exception {
    StringBuilder filter2NameBuilder = new StringBuilder("filt.er2");
    filter2NameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
    String filter2Name = filter2NameBuilder.toString();
    StringBuilder statsNameBuilder = new StringBuilder("st.ats");
    statsNameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
    String statsName = statsNameBuilder.toString();
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").executionHint(randomExecutionHint()).field("tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>" + filter2Name + ">" + statsName + ".max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter(filter2Name, QueryBuilders.matchAllQuery()).subAggregation(stats(statsName).field("i"))))).execute().actionGet();
    assertSearchResponse(response);
    Terms tags = response.getAggregations().get("tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));
    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
    // the max for "more" is 2
    // the max for "less" is 4
    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "more" : "less"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 3L : 2L));
    Filter filter2 = filter1.getAggregations().get(filter2Name);
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
    Stats stats = filter2.getAggregations().get(statsName);
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0));
    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "less" : "more"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
    filter1 = tag.getAggregations().get("filter1");
    assertThat(filter1, notNullValue());
    assertThat(filter1.getDocCount(), equalTo(asc ? 2L : 3L));
    filter2 = filter1.getAggregations().get(filter2Name);
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
    stats = filter2.getAggregations().get(statsName);
    assertThat(stats, notNullValue());
    assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.extendedStats(org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 27 with Filter

use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.

the class StringTermsIT method testStringTermsNestedIntoPerBucketAggregator.

public void testStringTermsNestedIntoPerBucketAggregator() throws Exception {
    // no execution hint so that the logic that decides whether or not to use ordinals is executed
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(filter("filter", termQuery(MULTI_VALUED_FIELD_NAME, "val3")).subAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
    assertThat(response.getFailedShards(), equalTo(0));
    Filter filter = response.getAggregations().get("filter");
    Terms terms = filter.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(3));
    for (int i = 2; i <= 4; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(i == 3 ? 2L : 1L));
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 28 with Filter

use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.

the class FilterIT 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(filter("filter", 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());
    Filter filter = bucket.getAggregations().get("filter");
    assertThat(filter, Matchers.notNullValue());
    assertThat(filter.getName(), equalTo("filter"));
    assertThat(filter.getDocCount(), is(0L));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 29 with Filter

use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.

the class FilterIT 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(filter("tag1", emptyFilter)).execute().actionGet();
    assertSearchResponse(response);
    Filter filter = response.getAggregations().get("tag1");
    assertThat(filter, notNullValue());
    assertThat(filter.getDocCount(), equalTo((long) numDocs));
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) 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)

Example 30 with Filter

use of org.elasticsearch.search.aggregations.bucket.filter.Filter in project elasticsearch by elastic.

the class GeoHashGridIT method testFiltered.

public void testFiltered() throws Exception {
    GeoBoundingBoxQueryBuilder bbox = new GeoBoundingBoxQueryBuilder("location");
    bbox.setCorners(smallestGeoHash).queryName("bbox");
    for (int precision = 1; precision <= PRECISION; precision++) {
        SearchResponse response = client().prepareSearch("idx").addAggregation(AggregationBuilders.filter("filtered", bbox).subAggregation(geohashGrid("geohashgrid").field("location").precision(precision))).execute().actionGet();
        assertSearchResponse(response);
        Filter filter = response.getAggregations().get("filtered");
        GeoHashGrid geoGrid = filter.getAggregations().get("geohashgrid");
        for (GeoHashGrid.Bucket cell : geoGrid.getBuckets()) {
            String geohash = cell.getKeyAsString();
            long bucketCount = cell.getDocCount();
            int expectedBucketCount = expectedDocCountsForGeoHash.get(geohash);
            assertNotSame(bucketCount, 0);
            assertTrue("Buckets must be filtered", geohash.startsWith(smallestGeoHash));
            assertEquals("Geohash " + geohash + " has wrong doc count ", expectedBucketCount, bucketCount);
        }
    }
}
Also used : GeoBoundingBoxQueryBuilder(org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Bucket(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid.Bucket) GeoHashGrid(org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid) Matchers.containsString(org.hamcrest.Matchers.containsString) GeoPoint(org.elasticsearch.common.geo.GeoPoint) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)43 SearchResponse (org.elasticsearch.action.search.SearchResponse)42 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)34 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)27 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)11 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)7 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)5 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)5 Max (org.elasticsearch.search.aggregations.metrics.max.Max)5 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)5 ArrayList (java.util.ArrayList)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 QueryStringQueryBuilder (org.elasticsearch.index.query.QueryStringQueryBuilder)3 Global (org.elasticsearch.search.aggregations.bucket.global.Global)3 Nested (org.elasticsearch.search.aggregations.bucket.nested.Nested)3 Percentiles (org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles)3 Stats (org.elasticsearch.search.aggregations.metrics.stats.Stats)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 DateTime (org.joda.time.DateTime)3