Search in sources :

Example 6 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("extended_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;
    double sumOfSquares = 0;
    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());
        sumOfSquares += bucket.getDocCount() * bucket.getDocCount();
    }
    double avgValue = count == 0 ? Double.NaN : (sum / count);
    ExtendedStatsBucket extendedStatsBucketValue = response.getAggregations().get("extended_stats_bucket");
    assertThat(extendedStatsBucketValue, notNullValue());
    assertThat(extendedStatsBucketValue.getName(), equalTo("extended_stats_bucket"));
    assertThat(extendedStatsBucketValue.getAvg(), equalTo(avgValue));
    assertThat(extendedStatsBucketValue.getMin(), equalTo(min));
    assertThat(extendedStatsBucketValue.getMax(), equalTo(max));
    assertThat(extendedStatsBucketValue.getSumOfSquares(), equalTo(sumOfSquares));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) ExtendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 7 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("extended_stats_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));
    double bucketSum = 0;
    int count = 0;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    double sumOfSquares = 0;
    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());
        count++;
        bucketSum += sum.value();
        min = Math.min(min, sum.value());
        max = Math.max(max, sum.value());
        sumOfSquares += sum.value() * sum.value();
    }
    double avgValue = count == 0 ? Double.NaN : (bucketSum / count);
    ExtendedStatsBucket extendedStatsBucketValue = response.getAggregations().get("extended_stats_bucket");
    assertThat(extendedStatsBucketValue, notNullValue());
    assertThat(extendedStatsBucketValue.getName(), equalTo("extended_stats_bucket"));
    assertThat(extendedStatsBucketValue.getAvg(), equalTo(avgValue));
    assertThat(extendedStatsBucketValue.getMin(), equalTo(min));
    assertThat(extendedStatsBucketValue.getMax(), equalTo(max));
    assertThat(extendedStatsBucketValue.getSumOfSquares(), equalTo(sumOfSquares));
}
Also used : ExtendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.extendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.extendedStatsBucket) ExtendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket) 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 8 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("extended_stats_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));
    ExtendedStatsBucket extendedStatsBucketValue = response.getAggregations().get("extended_stats_bucket");
    assertThat(extendedStatsBucketValue, notNullValue());
    assertThat(extendedStatsBucketValue.getName(), equalTo("extended_stats_bucket"));
    assertThat(extendedStatsBucketValue.getAvg(), equalTo(Double.NaN));
}
Also used : ExtendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) PipelineAggregatorBuilders.extendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.extendedStatsBucket) ExtendedStatsBucket(org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)8 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)8 ExtendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)8 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)6 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 PipelineAggregatorBuilders.extendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.extendedStatsBucket)6 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)3 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)1