Search in sources :

Example 36 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class DoubleTermsIT method testMultiValuedFieldWithValueScript.

public void testMultiValuedFieldWithValueScript() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(MULTI_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).script(new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value + 1", Collections.emptyMap()))).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(6));
    for (int i = 0; i < 6; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("" + (i + 1d));
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("" + (i + 1d)));
        assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i + 1));
        if (i == 0 || i == 5) {
            assertThat(bucket.getDocCount(), equalTo(1L));
        } else {
            assertThat(bucket.getDocCount(), equalTo(2L));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Script(org.elasticsearch.script.Script) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 37 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class DoubleTermsIT 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()))).execute().actionGet();
    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 38 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class ReverseNestedIT method testSimpleReverseNestedToNested1.

public void testSimpleReverseNestedToNested1() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type2").addAggregation(nested("nested1", "nested1.nested2").subAggregation(terms("field2").field("nested1.nested2.field2").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())).size(10000).subAggregation(reverseNested("nested1_to_field1").path("nested1").subAggregation(terms("field1").field("nested1.field1").order(Terms.Order.term(true)).collectMode(randomFrom(SubAggCollectionMode.values())))))).get();
    assertSearchResponse(response);
    Nested nested = response.getAggregations().get("nested1");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested1"));
    assertThat(nested.getDocCount(), equalTo(27L));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    Terms usernames = nested.getAggregations().get("field2");
    assertThat(usernames, notNullValue());
    assertThat(usernames.getBuckets().size(), equalTo(5));
    List<Terms.Bucket> usernameBuckets = new ArrayList<>(usernames.getBuckets());
    Terms.Bucket bucket = usernameBuckets.get(0);
    assertThat(bucket.getKeyAsString(), equalTo("0"));
    assertThat(bucket.getDocCount(), equalTo(12L));
    ReverseNested reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(5L));
    Terms tags = reverseNested.getAggregations().get("field1");
    List<Terms.Bucket> tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(3L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(2L));
    bucket = usernameBuckets.get(1);
    assertThat(bucket.getKeyAsString(), equalTo("1"));
    assertThat(bucket.getDocCount(), equalTo(6L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(2);
    assertThat(bucket.getKeyAsString(), equalTo("2"));
    assertThat(bucket.getDocCount(), equalTo(5L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(4L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(4));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("a"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("b"));
    assertThat(tagsBuckets.get(1).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(2).getKeyAsString(), equalTo("c"));
    assertThat(tagsBuckets.get(2).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(3).getKeyAsString(), equalTo("e"));
    assertThat(tagsBuckets.get(3).getDocCount(), equalTo(1L));
    bucket = usernameBuckets.get(3);
    assertThat(bucket.getKeyAsString(), equalTo("3"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
    bucket = usernameBuckets.get(4);
    assertThat(bucket.getKeyAsString(), equalTo("4"));
    assertThat(bucket.getDocCount(), equalTo(2L));
    reverseNested = bucket.getAggregations().get("nested1_to_field1");
    assertThat(reverseNested.getDocCount(), equalTo(2L));
    tags = reverseNested.getAggregations().get("field1");
    tagsBuckets = new ArrayList<>(tags.getBuckets());
    assertThat(tagsBuckets.size(), equalTo(2));
    assertThat(tagsBuckets.get(0).getKeyAsString(), equalTo("d"));
    assertThat(tagsBuckets.get(0).getDocCount(), equalTo(1L));
    assertThat(tagsBuckets.get(1).getKeyAsString(), equalTo("f"));
}
Also used : AggregationBuilders.reverseNested(org.elasticsearch.search.aggregations.AggregationBuilders.reverseNested) Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) ReverseNested(org.elasticsearch.search.aggregations.bucket.nested.ReverseNested) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 39 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class SamplerIT method testUnmappedChildAggNoDiversity.

public void testUnmappedChildAggNoDiversity() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    assertThat(sample.getDocCount(), equalTo(0L));
    Terms authors = sample.getAggregations().get("authors");
    assertThat(authors.getBuckets().size(), equalTo(0));
}
Also used : Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SamplerAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 40 with Terms

use of org.elasticsearch.search.aggregations.bucket.terms.Terms in project elasticsearch by elastic.

the class SamplerIT method testSimpleSampler.

public void testSimpleSampler() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    Terms authors = sample.getAggregations().get("authors");
    Collection<Bucket> testBuckets = authors.getBuckets();
    long maxBooksPerAuthor = 0;
    for (Terms.Bucket testBucket : testBuckets) {
        maxBooksPerAuthor = Math.max(testBucket.getDocCount(), maxBooksPerAuthor);
    }
    assertThat(maxBooksPerAuthor, equalTo(3L));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SamplerAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)300 SearchResponse (org.elasticsearch.action.search.SearchResponse)268 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)100 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)38 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)26 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)24 Script (org.elasticsearch.script.Script)22 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)16 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)15 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)15 Aggregation (org.elasticsearch.search.aggregations.Aggregation)14 SearchHits (org.elasticsearch.search.SearchHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13