Search in sources :

Example 6 with Filter

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

the class LongTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").field("num_tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>filter2>max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter("filter2", QueryBuilders.matchAllQuery()).subAggregation(max("max").field(SINGLE_VALUED_FIELD_NAME))))).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 "1" is 2
    // the max for "0" is 4
    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "1" : "0"));
    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("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
    Max max = filter2.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getValue(), equalTo(asc ? 2.0 : 4.0));
    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "0" : "1"));
    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("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
    max = filter2.getAggregations().get("max");
    assertThat(max, notNullValue());
    assertThat(max.getValue(), 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) Max(org.elasticsearch.search.aggregations.metrics.max.Max) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with Filter

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

the class ShardReduceIT method testFilter.

public void testFilter() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setQuery(QueryBuilders.matchAllQuery()).addAggregation(filter("filter", QueryBuilders.matchAllQuery()).subAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.DAY).minDocCount(0))).execute().actionGet();
    assertSearchResponse(response);
    Filter filter = response.getAggregations().get("filter");
    Histogram histo = filter.getAggregations().get("histo");
    assertThat(histo.getBuckets().size(), equalTo(4));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 8 with Filter

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

the class LongTermsIT method testSingleValuedFieldOrderedBySingleBucketSubAggregationAsc.

public void testSingleValuedFieldOrderedBySingleBucketSubAggregationAsc() throws Exception {
    boolean asc = randomBoolean();
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("num_tags").field("num_tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter", asc)).subAggregation(filter("filter", QueryBuilders.matchAllQuery()))).get();
    assertSearchResponse(response);
    Terms tags = response.getAggregations().get("num_tags");
    assertThat(tags, notNullValue());
    assertThat(tags.getName(), equalTo("num_tags"));
    assertThat(tags.getBuckets().size(), equalTo(2));
    Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
    Terms.Bucket tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "0" : "1"));
    assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
    Filter filter = tag.getAggregations().get("filter");
    assertThat(filter, notNullValue());
    assertThat(filter.getDocCount(), equalTo(asc ? 2L : 3L));
    tag = iters.next();
    assertThat(tag, notNullValue());
    assertThat(key(tag), equalTo(asc ? "1" : "0"));
    assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
    filter = tag.getAggregations().get("filter");
    assertThat(filter, notNullValue());
    assertThat(filter.getDocCount(), equalTo(asc ? 3L : 2L));
}
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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 9 with Filter

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

the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialCharsNoDotNotation.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialCharsNoDotNotation() 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 10 with Filter

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

the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels.

public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevels() throws Exception {
    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>filter2>stats.max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter("filter2", QueryBuilders.matchAllQuery()).subAggregation(stats("stats").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("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
    Stats stats = filter2.getAggregations().get("stats");
    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("filter2");
    assertThat(filter2, notNullValue());
    assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
    stats = filter2.getAggregations().get("stats");
    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) 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