Search in sources :

Example 11 with GapPolicy

use of org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy in project elasticsearch by elastic.

the class DerivativeIT method testSingleValueAggDerivativeWithGaps_random.

public void testSingleValueAggDerivativeWithGaps_random() throws Exception {
    GapPolicy gapPolicy = randomFrom(GapPolicy.values());
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx_rnd").setQuery(matchAllQuery()).addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(1).extendedBounds(0L, (long) numBuckets_empty_rnd - 1).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME)).subAggregation(derivative("deriv", "sum").gapPolicy(gapPolicy))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(numDocsEmptyIdx_rnd));
    Histogram deriv = searchResponse.getAggregations().get("histo");
    assertThat(deriv, Matchers.notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(numBuckets_empty_rnd));
    double lastSumValue = Double.NaN;
    for (int i = 0; i < valueCounts_empty_rnd.length; i++) {
        Histogram.Bucket bucket = buckets.get(i);
        checkBucketKeyAndDocCount("InternalBucket " + i, bucket, i, valueCounts_empty_rnd[i]);
        Sum sum = bucket.getAggregations().get("sum");
        double thisSumValue = sum.value();
        if (bucket.getDocCount() == 0) {
            thisSumValue = gapPolicy == GapPolicy.INSERT_ZEROS ? 0 : Double.NaN;
        }
        SimpleValue sumDeriv = bucket.getAggregations().get("deriv");
        if (i == 0) {
            assertThat(sumDeriv, nullValue());
        } else {
            double expectedDerivative = thisSumValue - lastSumValue;
            if (Double.isNaN(expectedDerivative)) {
                assertThat(sumDeriv.value(), equalTo(expectedDerivative));
            } else {
                assertThat(sumDeriv.value(), closeTo(expectedDerivative, 0.00001));
            }
        }
        lastSumValue = thisSumValue;
    }
}
Also used : GapPolicy(org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy) 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) Sum(org.elasticsearch.search.aggregations.metrics.sum.Sum) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

GapPolicy (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)11 ArrayList (java.util.ArrayList)10 List (java.util.List)6 Map (java.util.Map)6 ParsingException (org.elasticsearch.common.ParsingException)6 XContentParser (org.elasticsearch.common.xcontent.XContentParser)6 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Collectors (java.util.stream.Collectors)4 StreamSupport (java.util.stream.StreamSupport)4 StreamInput (org.elasticsearch.common.io.stream.StreamInput)4 StreamOutput (org.elasticsearch.common.io.stream.StreamOutput)4 DocValueFormat (org.elasticsearch.search.DocValueFormat)4 InternalAggregation (org.elasticsearch.search.aggregations.InternalAggregation)4 ReduceContext (org.elasticsearch.search.aggregations.InternalAggregation.ReduceContext)4 InternalAggregations (org.elasticsearch.search.aggregations.InternalAggregations)4 Bucket (org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation.Bucket)4 BucketHelpers.resolveBucketValue (org.elasticsearch.search.aggregations.pipeline.BucketHelpers.resolveBucketValue)4 PipelineAggregator (org.elasticsearch.search.aggregations.pipeline.PipelineAggregator)4 Script (org.elasticsearch.script.Script)3