Search in sources :

Example 1 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("avg_histo_bucket", "histo>_count"))).addAggregation(extendedStatsBucket("avg_terms_bucket", "terms>avg_histo_bucket.avg")).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));
    double aggTermsSum = 0;
    int aggTermsCount = 0;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    double sumOfSquares = 0;
    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 aggHistoSum = 0;
        int aggHistoCount = 0;
        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));
            aggHistoCount++;
            aggHistoSum += bucket.getDocCount();
        }
        double avgHistoValue = aggHistoCount == 0 ? Double.NaN : (aggHistoSum / aggHistoCount);
        ExtendedStatsBucket extendedStatsBucketValue = termsBucket.getAggregations().get("avg_histo_bucket");
        assertThat(extendedStatsBucketValue, notNullValue());
        assertThat(extendedStatsBucketValue.getName(), equalTo("avg_histo_bucket"));
        assertThat(extendedStatsBucketValue.getAvg(), equalTo(avgHistoValue));
        aggTermsCount++;
        aggTermsSum += avgHistoValue;
        min = Math.min(min, avgHistoValue);
        max = Math.max(max, avgHistoValue);
        sumOfSquares += avgHistoValue * avgHistoValue;
    }
    double avgTermsValue = aggTermsCount == 0 ? Double.NaN : (aggTermsSum / aggTermsCount);
    ExtendedStatsBucket extendedStatsBucketValue = response.getAggregations().get("avg_terms_bucket");
    assertThat(extendedStatsBucketValue, notNullValue());
    assertThat(extendedStatsBucketValue.getName(), equalTo("avg_terms_bucket"));
    assertThat(extendedStatsBucketValue.getAvg(), equalTo(avgTermsValue));
    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) 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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 2 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("extended_stats_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();
        double bucketSum = 0;
        int count = 0;
        double min = Double.POSITIVE_INFINITY;
        double max = Double.NEGATIVE_INFINITY;
        double sumOfSquares = 0;
        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());
            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 = termsBucket.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) 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 3 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT method testDocCountAsSubAgg.

public void testDocCountAsSubAgg() 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(extendedStatsBucket("extended_stats_bucket", "histo>_count"))).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 sum = 0;
        int count = 0;
        double min = Double.POSITIVE_INFINITY;
        double max = Double.NEGATIVE_INFINITY;
        double sumOfSquares = 0;
        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));
            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 = termsBucket.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) 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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT 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(extendedStatsBucket("extended_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;
        double sumOfSquares = 0;
        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());
                sumOfSquares += sum.value() * sum.value();
            }
        }
        double avgValue = count == 0 ? Double.NaN : (bucketSum / count);
        ExtendedStatsBucket extendedStatsBucketValue = termsBucket.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) 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 5 with ExtendedStatsBucket

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

the class ExtendedStatsBucketIT method testGappyIndexWithSigma.

/**
     * Test for https://github.com/elastic/elasticsearch/issues/17701
     */
public void testGappyIndexWithSigma() {
    double sigma = randomDoubleBetween(1.0, 6.0, true);
    SearchResponse response = client().prepareSearch("idx_gappy").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(1L)).addAggregation(extendedStatsBucket("extended_stats_bucket", "histo>_count").sigma(sigma)).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(6));
    for (int i = 0; i < 6; ++i) {
        long expectedDocCount;
        if (i == 3) {
            expectedDocCount = 2;
        } else if (i == 4) {
            expectedDocCount = 0;
        } else {
            expectedDocCount = 1;
        }
        Histogram.Bucket bucket = buckets.get(i);
        assertThat("i: " + i, bucket, notNullValue());
        assertThat("i: " + i, ((Number) bucket.getKey()).longValue(), equalTo((long) i));
        assertThat("i: " + i, bucket.getDocCount(), equalTo(expectedDocCount));
    }
    ExtendedStatsBucket extendedStatsBucketValue = response.getAggregations().get("extended_stats_bucket");
    long count = 6L;
    double sum = 1.0 + 1.0 + 1.0 + 2.0 + 0.0 + 1.0;
    double sumOfSqrs = 1.0 + 1.0 + 1.0 + 4.0 + 0.0 + 1.0;
    double avg = sum / count;
    double var = (sumOfSqrs - ((sum * sum) / count)) / count;
    double stdDev = Math.sqrt(var);
    assertThat(extendedStatsBucketValue, notNullValue());
    assertThat(extendedStatsBucketValue.getName(), equalTo("extended_stats_bucket"));
    assertThat(extendedStatsBucketValue.getMin(), equalTo(0.0));
    assertThat(extendedStatsBucketValue.getMax(), equalTo(2.0));
    assertThat(extendedStatsBucketValue.getCount(), equalTo(count));
    assertThat(extendedStatsBucketValue.getSum(), equalTo(sum));
    assertThat(extendedStatsBucketValue.getAvg(), equalTo(avg));
    assertThat(extendedStatsBucketValue.getSumOfSquares(), equalTo(sumOfSqrs));
    assertThat(extendedStatsBucketValue.getVariance(), equalTo(var));
    assertThat(extendedStatsBucketValue.getStdDeviation(), equalTo(stdDev));
    assertThat(extendedStatsBucketValue.getStdDeviationBound(Bounds.LOWER), equalTo(avg - (sigma * stdDev)));
    assertThat(extendedStatsBucketValue.getStdDeviationBound(Bounds.UPPER), equalTo(avg + (sigma * stdDev)));
}
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)

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