Search in sources :

Example 56 with Sum

use of org.elasticsearch.search.aggregations.metrics.sum.Sum in project elasticsearch by elastic.

the class BucketScriptIT method testInlineScript.

public void testInlineScript() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(interval).subAggregation(sum("field2Sum").field(FIELD_2_NAME)).subAggregation(sum("field3Sum").field(FIELD_3_NAME)).subAggregation(sum("field4Sum").field(FIELD_4_NAME)).subAggregation(bucketScript("seriesArithmetic", new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "_value0 + _value1 + _value2", Collections.emptyMap()), "field2Sum", "field3Sum", "field4Sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Histogram.Bucket> buckets = histo.getBuckets();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        if (bucket.getDocCount() == 0) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
            assertThat(seriesArithmetic, nullValue());
        } else {
            Sum field2Sum = bucket.getAggregations().get("field2Sum");
            assertThat(field2Sum, notNullValue());
            double field2SumValue = field2Sum.getValue();
            Sum field3Sum = bucket.getAggregations().get("field3Sum");
            assertThat(field3Sum, notNullValue());
            double field3SumValue = field3Sum.getValue();
            Sum field4Sum = bucket.getAggregations().get("field4Sum");
            assertThat(field4Sum, notNullValue());
            double field4SumValue = field4Sum.getValue();
            SimpleValue seriesArithmetic = bucket.getAggregations().get("seriesArithmetic");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertThat(seriesArithmeticValue, equalTo(field2SumValue + field3SumValue + field4SumValue));
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 57 with Sum

use of org.elasticsearch.search.aggregations.metrics.sum.Sum 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 58 with Sum

use of org.elasticsearch.search.aggregations.metrics.sum.Sum 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 59 with Sum

use of org.elasticsearch.search.aggregations.metrics.sum.Sum in project elasticsearch by elastic.

the class BucketSelectorIT method testInlineScript.

public void testInlineScript() {
    Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "Double.isNaN(_value0) ? false : (_value0 + _value1 > 100)", Collections.emptyMap());
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(FIELD_1_NAME).interval(interval).subAggregation(sum("field2Sum").field(FIELD_2_NAME)).subAggregation(sum("field3Sum").field(FIELD_3_NAME)).subAggregation(bucketSelector("bucketSelector", script, "field2Sum", "field3Sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    for (int i = 0; i < buckets.size(); ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        Sum field2Sum = bucket.getAggregations().get("field2Sum");
        assertThat(field2Sum, notNullValue());
        double field2SumValue = field2Sum.getValue();
        Sum field3Sum = bucket.getAggregations().get("field3Sum");
        assertThat(field3Sum, notNullValue());
        double field3SumValue = field3Sum.getValue();
        assertThat(field2SumValue + field3SumValue, greaterThan(100.0));
    }
}
Also used : Script(org.elasticsearch.script.Script) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 60 with Sum

use of org.elasticsearch.search.aggregations.metrics.sum.Sum in project elasticsearch by elastic.

the class CumulativeSumIT method testMetric.

public void testMetric() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME)).subAggregation(cumulativeSum("cumulative_sum", "sum"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = histo.getBuckets();
    double bucketSum = 0;
    for (int i = 0; i < buckets.size(); ++i) {
        Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
        Sum sum = bucket.getAggregations().get("sum");
        assertThat(sum, notNullValue());
        bucketSum += sum.value();
        InternalSimpleValue sumBucketValue = bucket.getAggregations().get("cumulative_sum");
        assertThat(sumBucketValue, notNullValue());
        assertThat(sumBucketValue.getName(), equalTo("cumulative_sum"));
        assertThat(sumBucketValue.value(), equalTo(bucketSum));
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) PipelineAggregatorBuilders.cumulativeSum(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.cumulativeSum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)79 SearchResponse (org.elasticsearch.action.search.SearchResponse)78 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)78 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)43 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)38 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)31 Script (org.elasticsearch.script.Script)26 ArrayList (java.util.ArrayList)14 PipelineAggregatorBuilders.bucketScript (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript)9 HashMap (java.util.HashMap)8 CompiledScript (org.elasticsearch.script.CompiledScript)8 ExecutableScript (org.elasticsearch.script.ExecutableScript)8 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)8 SearchScript (org.elasticsearch.script.SearchScript)8 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)8 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)7 Range (org.elasticsearch.search.aggregations.bucket.range.Range)4 PipelineAggregatorBuilders.percentilesBucket (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.percentilesBucket)4 PercentilesBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket)4 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)3