Search in sources :

Example 91 with Histogram

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

the class MovAvgIT method testHoltWintersMinimization.

public void testHoltWintersMinimization() {
    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 HoltWintersModel.HoltWintersModelBuilder().period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(true)).subAggregation(movingAvg("movavg_values", "the_metric").window(windowSize).modelBuilder(new HoltWintersModel.HoltWintersModelBuilder().period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(true))).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.HOLT_WINTERS.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.HOLT_WINTERS.name() + "_" + MetricTarget.VALUE.name());
    Iterator<? extends Histogram.Bucket> actualIter = buckets.iterator();
    Iterator<PipelineAggregationHelperTests.MockBucket> expectedBucketIter = mockHisto.iterator();
    Iterator<Double> expectedCountsIter = expectedCounts.iterator();
    Iterator<Double> expectedValueIter = expectedValues.iterator();
    // The minimizer is stochastic, so just make sure all the values coming back aren't null
    while (actualIter.hasNext()) {
        Histogram.Bucket actual = actualIter.next();
        PipelineAggregationHelperTests.MockBucket expected = expectedBucketIter.next();
        Double expectedCount = expectedCountsIter.next();
        Double expectedValue = expectedValueIter.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));
        SimpleValue countMovAvg = actual.getAggregations().get("movavg_counts");
        SimpleValue valuesMovAvg = actual.getAggregations().get("movavg_values");
        if (expectedCount == null) {
            //this bucket wasn't supposed to have a value (empty, skipped, etc), so
            //movavg should be null too
            assertThat(countMovAvg, nullValue());
        } else {
            // Note that we don't compare against the mock values, since those are assuming
            // a non-minimized set of coefficients.  Just check for not-nullness
            assertThat(countMovAvg, notNullValue());
        }
        if (expectedValue == null) {
            //this bucket wasn't supposed to have a value (empty, skipped, etc), so
            //movavg should be null too
            assertThat(valuesMovAvg, nullValue());
        } else {
            // Note that we don't compare against the mock values, since those are assuming
            // a non-minimized set of coefficients.  Just check for not-nullness
            assertThat(valuesMovAvg, notNullValue());
        }
    }
}
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) HoltWintersModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

Example 92 with Histogram

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

the class MovAvgIT method testHoltWintersValuedField.

public void testHoltWintersValuedField() {
    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 HoltWintersModel.HoltWintersModelBuilder().alpha(alpha).beta(beta).gamma(gamma).period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(false)).subAggregation(movingAvg("movavg_values", "the_metric").window(windowSize).modelBuilder(new HoltWintersModel.HoltWintersModelBuilder().alpha(alpha).beta(beta).gamma(gamma).period(period).seasonalityType(seasonalityType)).gapPolicy(gapPolicy).minimize(false))).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.HOLT_WINTERS.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.HOLT_WINTERS.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) HoltWintersModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 93 with Histogram

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

the class MovAvgIT method testHoltSingleValuedField.

public void testHoltSingleValuedField() {
    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 HoltLinearModel.HoltLinearModelBuilder().alpha(alpha).beta(beta)).gapPolicy(gapPolicy)).subAggregation(movingAvg("movavg_values", "the_metric").window(windowSize).modelBuilder(new HoltLinearModel.HoltLinearModelBuilder().alpha(alpha).beta(beta)).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.HOLT.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.HOLT.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) HoltLinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 94 with Histogram

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

the class MovAvgIT method testMinimizeNotEnoughData.

/**
     * If the minimizer is turned on, but there isn't enough data to minimize with, it will simply use
     * the default settings.  Which means our mock histo will match the generated result (which it won't
     * if the minimizer is actually working, since the coefficients will be different and thus generate different
     * data)
     *
     * We can simulate this by setting the window size == size of histo
     */
public void testMinimizeNotEnoughData() {
    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(numBuckets).modelBuilder(new HoltLinearModel.HoltLinearModelBuilder().alpha(alpha).beta(beta)).gapPolicy(gapPolicy).minimize(true)).subAggregation(movingAvg("movavg_values", "the_metric").window(numBuckets).modelBuilder(new HoltLinearModel.HoltLinearModelBuilder().alpha(alpha).beta(beta)).gapPolicy(gapPolicy).minimize(true))).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.HOLT_BIG_MINIMIZE.name() + "_" + MetricTarget.COUNT.name());
    List<Double> expectedValues = testValues.get(MovAvgType.HOLT_BIG_MINIMIZE.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) HoltLinearModel(org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltLinearModel) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 95 with Histogram

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

the class MovAvgIT method testPredictNegativeKeysAtStart.

public void testPredictNegativeKeysAtStart() {
    SearchResponse response = client().prepareSearch("neg_idx").setTypes("type").addAggregation(histogram("histo").field(INTERVAL_FIELD).interval(1).subAggregation(avg("avg").field(VALUE_FIELD)).subAggregation(movingAvg("movavg_values", "avg").window(windowSize).modelBuilder(new SimpleModel.SimpleModelBuilder()).gapPolicy(gapPolicy).predict(5))).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(25));
    SimpleValue current = buckets.get(0).getAggregations().get("movavg_values");
    assertThat(current, nullValue());
    for (int i = 1; i < 20; i++) {
        Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKey(), equalTo(i - 10d));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Avg avgAgg = bucket.getAggregations().get("avg");
        assertThat(avgAgg, notNullValue());
        assertThat(avgAgg.value(), equalTo(10d));
        SimpleValue movAvgAgg = bucket.getAggregations().get("movavg_values");
        assertThat(movAvgAgg, notNullValue());
        assertThat(movAvgAgg.value(), equalTo(10d));
    }
    for (int i = 20; i < 25; i++) {
        Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        assertThat(bucket.getKey(), equalTo(i - 10d));
        assertThat(bucket.getDocCount(), equalTo(0L));
        Avg avgAgg = bucket.getAggregations().get("avg");
        assertThat(avgAgg, nullValue());
        SimpleValue movAvgAgg = bucket.getAggregations().get("movavg_values");
        assertThat(movAvgAgg, notNullValue());
        assertThat(movAvgAgg.value(), equalTo(10d));
    }
}
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) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

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