Search in sources :

Example 71 with Terms

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

the class StringTermsIT method assertMultiSortResponse.

private void assertMultiSortResponse(String[] expectedKeys, Terms.Order... order) {
    SearchResponse response = client().prepareSearch("sort_idx").setTypes("multi_sort_type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.compound(order)).subAggregation(avg("avg_l").field("l")).subAggregation(sum("sum_d").field("d"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(expectedKeys.length));
    int i = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo(expectedKeys[i]));
        assertThat(bucket.getDocCount(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("_count")));
        Avg avg = bucket.getAggregations().get("avg_l");
        assertThat(avg, notNullValue());
        assertThat(avg.getValue(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("avg_l")));
        Sum sum = bucket.getAggregations().get("sum_d");
        assertThat(sum, notNullValue());
        assertThat(sum.getValue(), equalTo(expectedMultiSortBuckets.get(expectedKeys[i]).get("sum_d")));
        i++;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 72 with Terms

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

the class StringTermsIT method testIndexMetaField.

public void testIndexMetaField() throws Exception {
    SearchResponse response = client().prepareSearch("idx", "empty_bucket_idx").setTypes("type").addAggregation(terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).field(IndexFieldMapper.NAME)).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(2));
    int i = 0;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo(i == 0 ? "idx" : "empty_bucket_idx"));
        assertThat(bucket.getDocCount(), equalTo(i == 0 ? 5L : 2L));
        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 73 with Terms

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

the class StringTermsIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(1L).minDocCount(0).subAggregation(terms("terms").field("value"))).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());
    Terms terms = bucket.getAggregations().get("terms");
    assertThat(terms, Matchers.notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().isEmpty(), is(true));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 74 with Terms

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

the class StringTermsIT method testSingleValuedFieldOrderedBySingleValueSubAggregationDesc.

public void testSingleValuedFieldOrderedBySingleValueSubAggregationDesc() throws Exception {
    boolean asc = false;
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("avg_i", asc)).subAggregation(avg("avg_i").field("i"))).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 = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Avg avg = bucket.getAggregations().get("avg_i");
        assertThat(avg, notNullValue());
        assertThat(avg.getValue(), equalTo((double) i));
        i--;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 75 with Terms

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

the class StringTermsIT method testScriptSingleValue.

/*
     *
     * [foo_val0, foo_val1] [foo_val1, foo_val2] [foo_val2, foo_val3] [foo_val3,
     * foo_val4] [foo_val4, foo_val5]
     *
     *
     * foo_val0 - doc_count: 1 - val_count: 2 foo_val1 - doc_count: 2 -
     * val_count: 4 foo_val2 - doc_count: 2 - val_count: 4 foo_val3 - doc_count:
     * 2 - val_count: 4 foo_val4 - doc_count: 2 - val_count: 4 foo_val5 -
     * doc_count: 1 - val_count: 2
     */
public void testScriptSingleValue() throws Exception {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['" + SINGLE_VALUED_FIELD_NAME + "'].value", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").collectMode(randomFrom(SubAggCollectionMode.values())).executionHint(randomExecutionHint()).script(script)).get();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    for (int i = 0; i < 5; i++) {
        Terms.Bucket bucket = terms.getBucketByKey("val" + i);
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
    }
}
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)252 SearchResponse (org.elasticsearch.action.search.SearchResponse)238 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)209 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)97 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)37 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)36 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)30 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)26 Script (org.elasticsearch.script.Script)22 ArrayList (java.util.ArrayList)21 HashMap (java.util.HashMap)21 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)16 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)16 SearchHits (org.elasticsearch.search.SearchHits)13 TopHits (org.elasticsearch.search.aggregations.metrics.tophits.TopHits)13 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)13 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)12 StringTerms (org.elasticsearch.search.aggregations.bucket.terms.StringTerms)11 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)10 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)9