Search in sources :

Example 96 with Terms

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

the class MaxBucketIT method testMetricAsSubAgg.

public void testMetricAsSubAgg() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).subAggregation(maxBucket("max_bucket", "histo>sum"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Bucket> buckets = histo.getBuckets();
        List<String> maxKeys = new ArrayList<>();
        double maxValue = Double.NEGATIVE_INFINITY;
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            if (bucket.getDocCount() != 0) {
                Sum sum = bucket.getAggregations().get("sum");
                assertThat(sum, notNullValue());
                if (sum.value() > maxValue) {
                    maxValue = sum.value();
                    maxKeys = new ArrayList<>();
                    maxKeys.add(bucket.getKeyAsString());
                } else if (sum.value() == maxValue) {
                    maxKeys.add(bucket.getKeyAsString());
                }
            }
        }
        InternalBucketMetricValue maxBucketValue = termsBucket.getAggregations().get("max_bucket");
        assertThat(maxBucketValue, notNullValue());
        assertThat(maxBucketValue.getName(), equalTo("max_bucket"));
        assertThat(maxBucketValue.value(), equalTo(maxValue));
        assertThat(maxBucketValue.keys(), equalTo(maxKeys.toArray(new String[maxKeys.size()])));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.maxBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)

Example 97 with Terms

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

the class MaxBucketIT method testMetricTopLevel.

public void testMetricTopLevel() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).addAggregation(maxBucket("max_bucket", "terms>sum")).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(interval));
    List<String> maxKeys = new ArrayList<>();
    double maxValue = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat((String) bucket.getKey(), equalTo("tag" + (i % interval)));
        assertThat(bucket.getDocCount(), greaterThan(0L));
        Sum sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        if (sum.value() > maxValue) {
            maxValue = sum.value();
            maxKeys = new ArrayList<>();
            maxKeys.add(bucket.getKeyAsString());
        } else if (sum.value() == maxValue) {
            maxKeys.add(bucket.getKeyAsString());
        }
    }
    InternalBucketMetricValue maxBucketValue = response.getAggregations().get("max_bucket");
    assertThat(maxBucketValue, notNullValue());
    assertThat(maxBucketValue.getName(), equalTo("max_bucket"));
    assertThat(maxBucketValue.value(), equalTo(maxValue));
    assertThat(maxBucketValue.keys(), equalTo(maxKeys.toArray(new String[maxKeys.size()])));
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.maxBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)

Example 98 with Terms

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

the class MaxBucketIT method testNested.

public void testNested() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(maxBucket("max_histo_bucket", "histo>_count"))).addAggregation(maxBucket("max_terms_bucket", "terms>max_histo_bucket")).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    List<String> maxTermsKeys = new ArrayList<>();
    double maxTermsValue = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Bucket> buckets = histo.getBuckets();
        List<String> maxHistoKeys = new ArrayList<>();
        double maxHistoValue = Double.NEGATIVE_INFINITY;
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            if (bucket.getDocCount() > maxHistoValue) {
                maxHistoValue = bucket.getDocCount();
                maxHistoKeys = new ArrayList<>();
                maxHistoKeys.add(bucket.getKeyAsString());
            } else if (bucket.getDocCount() == maxHistoValue) {
                maxHistoKeys.add(bucket.getKeyAsString());
            }
        }
        InternalBucketMetricValue maxBucketValue = termsBucket.getAggregations().get("max_histo_bucket");
        assertThat(maxBucketValue, notNullValue());
        assertThat(maxBucketValue.getName(), equalTo("max_histo_bucket"));
        assertThat(maxBucketValue.value(), equalTo(maxHistoValue));
        assertThat(maxBucketValue.keys(), equalTo(maxHistoKeys.toArray(new String[maxHistoKeys.size()])));
        if (maxHistoValue > maxTermsValue) {
            maxTermsValue = maxHistoValue;
            maxTermsKeys = new ArrayList<>();
            maxTermsKeys.add(termsBucket.getKeyAsString());
        } else if (maxHistoValue == maxTermsValue) {
            maxTermsKeys.add(termsBucket.getKeyAsString());
        }
    }
    InternalBucketMetricValue maxBucketValue = response.getAggregations().get("max_terms_bucket");
    assertThat(maxBucketValue, notNullValue());
    assertThat(maxBucketValue.getName(), equalTo("max_terms_bucket"));
    assertThat(maxBucketValue.value(), equalTo(maxTermsValue));
    assertThat(maxBucketValue.keys(), equalTo(maxTermsKeys.toArray(new String[maxTermsKeys.size()])));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.maxBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)

Example 99 with Terms

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

the class MaxBucketIT method testMetricAsSubAggWithInsertZeros.

public void testMetricAsSubAggWithInsertZeros() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).subAggregation(maxBucket("max_bucket", "histo>sum").gapPolicy(GapPolicy.INSERT_ZEROS))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> termsBuckets = terms.getBuckets();
    assertThat(termsBuckets.size(), equalTo(interval));
    for (int i = 0; i < interval; ++i) {
        Terms.Bucket termsBucket = termsBuckets.get(i);
        assertThat(termsBucket, notNullValue());
        assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
        Histogram histo = termsBucket.getAggregations().get("histo");
        assertThat(histo, notNullValue());
        assertThat(histo.getName(), equalTo("histo"));
        List<? extends Bucket> buckets = histo.getBuckets();
        List<String> maxKeys = new ArrayList<>();
        double maxValue = Double.NEGATIVE_INFINITY;
        for (int j = 0; j < numValueBuckets; ++j) {
            Histogram.Bucket bucket = buckets.get(j);
            assertThat(bucket, notNullValue());
            assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
            Sum sum = bucket.getAggregations().get("sum");
            assertThat(sum, notNullValue());
            if (sum.value() > maxValue) {
                maxValue = sum.value();
                maxKeys = new ArrayList<>();
                maxKeys.add(bucket.getKeyAsString());
            } else if (sum.value() == maxValue) {
                maxKeys.add(bucket.getKeyAsString());
            }
        }
        InternalBucketMetricValue maxBucketValue = termsBucket.getAggregations().get("max_bucket");
        assertThat(maxBucketValue, notNullValue());
        assertThat(maxBucketValue.getName(), equalTo("max_bucket"));
        assertThat(maxBucketValue.value(), equalTo(maxValue));
        assertThat(maxBucketValue.keys(), equalTo(maxKeys.toArray(new String[maxKeys.size()])));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) ArrayList(java.util.ArrayList) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.maxBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.maxBucket)

Example 100 with Terms

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

the class MinBucketIT method testNoBuckets.

public void testNoBuckets() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").includeExclude(new IncludeExclude(null, "tag.*")).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).addAggregation(minBucket("min_bucket", "terms>sum")).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    List<Terms.Bucket> buckets = terms.getBuckets();
    assertThat(buckets.size(), equalTo(0));
    InternalBucketMetricValue minBucketValue = response.getAggregations().get("min_bucket");
    assertThat(minBucketValue, notNullValue());
    assertThat(minBucketValue.getName(), equalTo("min_bucket"));
    assertThat(minBucketValue.value(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(minBucketValue.keys(), equalTo(new String[0]));
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.minBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.minBucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) InternalBucketMetricValue(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue) 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