Search in sources :

Example 21 with Terms

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

the class DiversifiedSamplerIT method testNestedDiversity.

public void testNestedDiversity() throws Exception {
    // Test multiple samples gathered under buckets made by a parent agg
    int MAX_DOCS_PER_AUTHOR = 1;
    TermsAggregationBuilder rootTerms = terms("genres").field("genre");
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100);
    sampleAgg.field("author").maxDocsPerValue(MAX_DOCS_PER_AUTHOR).executionHint(randomExecutionHint());
    sampleAgg.subAggregation(terms("authors").field("author"));
    rootTerms.subAggregation(sampleAgg);
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).addAggregation(rootTerms).execute().actionGet();
    assertSearchResponse(response);
    Terms genres = response.getAggregations().get("genres");
    Collection<Bucket> genreBuckets = genres.getBuckets();
    for (Terms.Bucket genreBucket : genreBuckets) {
        Sampler sample = genreBucket.getAggregations().get("sample");
        Terms authors = sample.getAggregations().get("authors");
        Collection<Bucket> testBuckets = authors.getBuckets();
        for (Terms.Bucket testBucket : testBuckets) {
            assertThat(testBucket.getDocCount(), lessThanOrEqualTo((long) NUM_SHARDS * MAX_DOCS_PER_AUTHOR));
        }
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) TermsAggregationBuilder(org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder) 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) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 22 with Terms

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

the class DiversifiedSamplerIT method testPartiallyUnmappedDiversifyField.

public void testPartiallyUnmappedDiversifyField() throws Exception {
    // One of the indexes is missing the "author" field used for
    // diversifying results
    DiversifiedAggregationBuilder sampleAgg = new DiversifiedAggregationBuilder("sample").shardSize(100).field("author").maxDocsPerValue(1);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped_author", "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");
    assertThat(sample.getDocCount(), greaterThan(0L));
    Terms authors = sample.getAggregations().get("authors");
    assertThat(authors.getBuckets().size(), greaterThan(0));
}
Also used : Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) DiversifiedAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.DiversifiedAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with Terms

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

the class DoubleTermsIT method testSingleValueFieldOrderedByTermAsc.

public void testSingleValueFieldOrderedByTermAsc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.term(true))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    int i = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("" + (double) i));
        assertThat(bucket.getKeyAsNumber().intValue(), equalTo(i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        i++;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 24 with Terms

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

the class MissingValueIT method testLongTerms.

public void testLongTerms() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("my_terms").field("long").missing(4)).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("my_terms");
    assertEquals(2, terms.getBuckets().size());
    assertEquals(1, terms.getBucketByKey("3").getDocCount());
    assertEquals(1, terms.getBucketByKey("4").getDocCount());
    response = client().prepareSearch("idx").addAggregation(terms("my_terms").field("long").missing(3)).get();
    assertSearchResponse(response);
    terms = response.getAggregations().get("my_terms");
    assertEquals(1, terms.getBuckets().size());
    assertEquals(2, terms.getBucketByKey("3").getDocCount());
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 25 with Terms

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

the class DoubleTermsIT method testMultiValuedFieldWithValueScriptNotUnique.

public void testMultiValuedFieldWithValueScriptNotUnique() 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, "(long) (_value / 1000 + 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(1));
    Terms.Bucket bucket = terms.getBucketByKey("1.0");
    assertThat(bucket, notNullValue());
    assertThat(key(bucket), equalTo("1.0"));
    assertThat(bucket.getKeyAsNumber().intValue(), equalTo(1));
    assertThat(bucket.getDocCount(), equalTo(5L));
}
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)

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