Search in sources :

Example 96 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class MovAvgIT method testEwmaSingleValuedField.

public void testEwmaSingleValuedField() {
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(histogram("histo").field(INTERVAL_FIELD).interval(interval).extendedBounds(0L, (long) (interval * (numBuckets - 1))).subAggregation(metric).subAggregation(movingAvg("movavg_counts", "_count").window(windowSize).modelBuilder(new EwmaModel.EWMAModelBuilder().alpha(alpha)).gapPolicy(gapPolicy)).subAggregation(movingAvg("movavg_values", "the_metric").window(windowSize).modelBuilder(new EwmaModel.EWMAModelBuilder().alpha(alpha)).gapPolicy(gapPolicy))).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("Size of buckets array is not correct.", buckets.size(), equalTo(mockHisto.size()));
    List<Double> expectedCounts = testValues.get(MovAvgType.EWMA.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.EWMA.name() + "_" + MetricTarget.VALUE.name());
    Iterator<? extends Histogram.Bucket> actualIter = buckets.iterator();
    Iterator<PipelineAggregationHelperTests.MockBucket> expectedBucketIter = mockHisto.iterator();
    Iterator<Double> expectedCountsIter = expectedCounts.iterator();
    Iterator<Double> expectedValuesIter = expectedValues.iterator();
    while (actualIter.hasNext()) {
        assertValidIterators(expectedBucketIter, expectedCountsIter, expectedValuesIter);
        Histogram.Bucket actual = actualIter.next();
        PipelineAggregationHelperTests.MockBucket expected = expectedBucketIter.next();
        Double expectedCount = expectedCountsIter.next();
        Double expectedValue = expectedValuesIter.next();
        assertThat("keys do not match", ((Number) actual.getKey()).longValue(), equalTo(expected.key));
        assertThat("doc counts do not match", actual.getDocCount(), equalTo((long) expected.count));
        assertBucketContents(actual, expectedCount, expectedValue);
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PipelineAggregationHelperTests(org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) EwmaModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.EwmaModel)

Example 97 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class MovAvgIT method testTwoMovAvgsWithPredictions.

public void testTwoMovAvgsWithPredictions() {
    SearchResponse response = client().prepareSearch("double_predict").setTypes("type").addAggregation(histogram("histo").field(INTERVAL_FIELD).interval(1).subAggregation(avg("avg").field(VALUE_FIELD)).subAggregation(derivative("deriv", "avg").gapPolicy(gapPolicy)).subAggregation(movingAvg("avg_movavg", "avg").window(windowSize).modelBuilder(new SimpleModel.SimpleModelBuilder()).gapPolicy(gapPolicy).predict(12)).subAggregation(movingAvg("deriv_movavg", "deriv").window(windowSize).modelBuilder(new SimpleModel.SimpleModelBuilder()).gapPolicy(gapPolicy).predict(12))).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("Size of buckets array is not correct.", buckets.size(), equalTo(24));
    Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKey(), equalTo(0d));
    assertThat(bucket.getDocCount(), equalTo(1L));
    Avg avgAgg = bucket.getAggregations().get("avg");
    assertThat(avgAgg, notNullValue());
    assertThat(avgAgg.value(), equalTo(10d));
    SimpleValue movAvgAgg = bucket.getAggregations().get("avg_movavg");
    assertThat(movAvgAgg, nullValue());
    Derivative deriv = bucket.getAggregations().get("deriv");
    assertThat(deriv, nullValue());
    SimpleValue derivMovAvg = bucket.getAggregations().get("deriv_movavg");
    assertThat(derivMovAvg, nullValue());
    // Second bucket
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKey(), equalTo(1d));
    assertThat(bucket.getDocCount(), equalTo(1L));
    avgAgg = bucket.getAggregations().get("avg");
    assertThat(avgAgg, notNullValue());
    assertThat(avgAgg.value(), equalTo(10d));
    deriv = bucket.getAggregations().get("deriv");
    assertThat(deriv, notNullValue());
    assertThat(deriv.value(), equalTo(0d));
    movAvgAgg = bucket.getAggregations().get("avg_movavg");
    assertThat(movAvgAgg, notNullValue());
    assertThat(movAvgAgg.value(), equalTo(10d));
    derivMovAvg = bucket.getAggregations().get("deriv_movavg");
    // still null because of movavg delay
    assertThat(derivMovAvg, Matchers.nullValue());
    for (int i = 2; i < 12; i++) {
        bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKey(), equalTo((double) i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        avgAgg = bucket.getAggregations().get("avg");
        assertThat(avgAgg, notNullValue());
        assertThat(avgAgg.value(), equalTo(10d));
        deriv = bucket.getAggregations().get("deriv");
        assertThat(deriv, notNullValue());
        assertThat(deriv.value(), equalTo(0d));
        movAvgAgg = bucket.getAggregations().get("avg_movavg");
        assertThat(movAvgAgg, notNullValue());
        assertThat(movAvgAgg.value(), equalTo(10d));
        derivMovAvg = bucket.getAggregations().get("deriv_movavg");
        assertThat(derivMovAvg, notNullValue());
        assertThat(derivMovAvg.value(), equalTo(0d));
    }
    // Predictions
    for (int i = 12; i < 24; i++) {
        bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKey(), equalTo((double) i));
        assertThat(bucket.getDocCount(), equalTo(0L));
        avgAgg = bucket.getAggregations().get("avg");
        assertThat(avgAgg, nullValue());
        deriv = bucket.getAggregations().get("deriv");
        assertThat(deriv, nullValue());
        movAvgAgg = bucket.getAggregations().get("avg_movavg");
        assertThat(movAvgAgg, notNullValue());
        assertThat(movAvgAgg.value(), equalTo(10d));
        derivMovAvg = bucket.getAggregations().get("deriv_movavg");
        assertThat(derivMovAvg, notNullValue());
        assertThat(derivMovAvg.value(), equalTo(0d));
    }
}
Also used : SimpleModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) PipelineAggregatorBuilders.movingAvg(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.movingAvg) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Derivative(org.elasticsearch.search.aggregations.pipeline.derivative.Derivative) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

Example 98 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class StatsBucketIT 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(statsBucket("avg_histo_bucket", "histo>_count"))).addAggregation(statsBucket("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;
    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);
        StatsBucket statsBucketValue = termsBucket.getAggregations().get("avg_histo_bucket");
        assertThat(statsBucketValue, notNullValue());
        assertThat(statsBucketValue.getName(), equalTo("avg_histo_bucket"));
        assertThat(statsBucketValue.getAvg(), equalTo(avgHistoValue));
        aggTermsCount++;
        aggTermsSum += avgHistoValue;
        min = Math.min(min, avgHistoValue);
        max = Math.max(max, avgHistoValue);
    }
    double avgTermsValue = aggTermsCount == 0 ? Double.NaN : (aggTermsSum / aggTermsCount);
    StatsBucket statsBucketValue = response.getAggregations().get("avg_terms_bucket");
    assertThat(statsBucketValue, notNullValue());
    assertThat(statsBucketValue.getName(), equalTo("avg_terms_bucket"));
    assertThat(statsBucketValue.getAvg(), equalTo(avgTermsValue));
    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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 99 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class SumBucketIT 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(sumBucket("sum_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;
        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());
            bucketSum += sum.value();
        }
        InternalSimpleValue sumBucketValue = termsBucket.getAggregations().get("sum_bucket");
        assertThat(sumBucketValue, notNullValue());
        assertThat(sumBucketValue.getName(), equalTo("sum_bucket"));
        assertThat(sumBucketValue.value(), equalTo(bucketSum));
    }
}
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) PipelineAggregatorBuilders.sumBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.sumBucket) 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 100 with Histogram

use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.

the class SumBucketIT 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(sumBucket("sum_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;
        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 += bucket.getDocCount();
        }
        InternalSimpleValue sumBucketValue = termsBucket.getAggregations().get("sum_bucket");
        assertThat(sumBucketValue, notNullValue());
        assertThat(sumBucketValue.getName(), equalTo("sum_bucket"));
        assertThat(sumBucketValue.value(), equalTo(sum));
    }
}
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) PipelineAggregatorBuilders.sumBucket(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.sumBucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 SearchResponse (org.elasticsearch.action.search.SearchResponse)208 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)202 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)121 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)43 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)37 ArrayList (java.util.ArrayList)36 DateTime (org.joda.time.DateTime)34 Script (org.elasticsearch.script.Script)32 InternalBucketMetricValue (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.InternalBucketMetricValue)11 HashMap (java.util.HashMap)10 PipelineAggregatorBuilders.bucketScript (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript)10 PipelineAggregationHelperTests (org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)7 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)6 Range (org.elasticsearch.search.aggregations.bucket.range.Range)6 PercentilesBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.percentile.PercentilesBucket)6 ExtendedStatsBucket (org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.extended.ExtendedStatsBucket)6