Search in sources :

Example 6 with StatsBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.

the class StatsBucketIT 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(statsBucket("stats_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();
        double bucketSum = 0;
        int count = 0;
        double min = Double.POSITIVE_INFINITY;
        double max = 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());
                count++;
                bucketSum += sum.value();
                min = Math.min(min, sum.value());
                max = Math.max(max, sum.value());
            }
        }
        double avgValue = count == 0 ? Double.NaN : (bucketSum / count);
        StatsBucket statsBucketValue = termsBucket.getAggregations().get("stats_bucket");
        assertThat(statsBucketValue, notNullValue());
        assertThat(statsBucketValue.getName(), equalTo("stats_bucket"));
        assertThat(statsBucketValue.getAvg(), equalTo(avgValue));
        assertThat(statsBucketValue.getMin(), equalTo(min));
        assertThat(statsBucketValue.getMax(), equalTo(max));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) StatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket) PipelineAggregatorBuilders.statsBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.statsBucket) StatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket) 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 7 with StatsBucket

use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.

the class StatsBucketIT method testDocCountTopLevel.

public void testDocCountTopLevel() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).addAggregation(statsBucket("stats_bucket", "histo>_count")).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(numValueBuckets));
    double sum = 0;
    int count = 0;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
        assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
        count++;
        sum += bucket.getDocCount();
        min = Math.min(min, bucket.getDocCount());
        max = Math.max(max, bucket.getDocCount());
    }
    double avgValue = count == 0 ? Double.NaN : (sum / count);
    StatsBucket statsBucketValue = response.getAggregations().get("stats_bucket");
    assertThat(statsBucketValue, notNullValue());
    assertThat(statsBucketValue.getName(), equalTo("stats_bucket"));
    assertThat(statsBucketValue.getAvg(), equalTo(avgValue));
    assertThat(statsBucketValue.getMin(), equalTo(min));
    assertThat(statsBucketValue.getMax(), equalTo(max));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) StatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)7 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)7 StatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket)7 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 PipelineAggregatorBuilders.statsBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.statsBucket)6 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)5 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)3 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)1