Search in sources :

Example 1 with SimpleValue

use of org.elasticsearch.search.aggregations.pipeline.SimpleValue 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 2 with SimpleValue

use of org.elasticsearch.search.aggregations.pipeline.SimpleValue 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)

Example 3 with SimpleValue

use of org.elasticsearch.search.aggregations.pipeline.SimpleValue 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 4 with SimpleValue

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

the class SerialDiffIT method assertBucketContents.

private void assertBucketContents(Histogram.Bucket actual, Double expectedCount, Double expectedValue) {
    // This is a gap bucket
    SimpleValue countDiff = actual.getAggregations().get("diff_counts");
    if (expectedCount == null) {
        assertThat("[_count] diff is not null", countDiff, nullValue());
    } else {
        assertThat("[_count] diff is null", countDiff, notNullValue());
        assertThat("[_count] diff does not match expected [" + countDiff.value() + " vs " + expectedCount + "]", countDiff.value(), closeTo(expectedCount, 0.1));
    }
    // This is a gap bucket
    SimpleValue valuesDiff = actual.getAggregations().get("diff_values");
    if (expectedValue == null) {
        assertThat("[value] diff is not null", valuesDiff, Matchers.nullValue());
    } else {
        assertThat("[value] diff is null", valuesDiff, notNullValue());
        assertThat("[value] diff does not match expected [" + valuesDiff.value() + " vs " + expectedValue + "]", valuesDiff.value(), closeTo(expectedValue, 0.1));
    }
}
Also used : SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

Example 5 with SimpleValue

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

the class MoreExpressionTests method testPipelineAggregationScript.

// test to make sure expressions are allowed to be used for reduce in pipeline aggregations
public void testPipelineAggregationScript() throws Exception {
    createIndex("agg_index");
    ensureGreen("agg_index");
    indexRandom(true, client().prepareIndex("agg_index", "doc", "1").setSource("one", 1.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "2").setSource("one", 2.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "3").setSource("one", 3.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "4").setSource("one", 4.0, "two", 2.0, "three", 3.0, "four", 4.0), client().prepareIndex("agg_index", "doc", "5").setSource("one", 5.0, "two", 2.0, "three", 3.0, "four", 4.0));
    SearchResponse response = client().prepareSearch("agg_index").addAggregation(histogram("histogram").field("one").interval(2).subAggregation(sum("twoSum").field("two")).subAggregation(sum("threeSum").field("three")).subAggregation(sum("fourSum").field("four")).subAggregation(bucketScript("totalSum", new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value0 + _value1 + _value2", Collections.emptyMap()), "twoSum", "threeSum", "fourSum"))).execute().actionGet();
    Histogram histogram = response.getAggregations().get("histogram");
    assertThat(histogram, notNullValue());
    assertThat(histogram.getName(), equalTo("histogram"));
    List<Histogram.Bucket> buckets = histogram.getBuckets();
    for (int bucketCount = 0; bucketCount < buckets.size(); ++bucketCount) {
        Histogram.Bucket bucket = buckets.get(bucketCount);
        if (bucket.getDocCount() == 1) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertEquals(9.0, seriesArithmeticValue, 0.001);
        } else if (bucket.getDocCount() == 2) {
            SimpleValue seriesArithmetic = bucket.getAggregations().get("totalSum");
            assertThat(seriesArithmetic, notNullValue());
            double seriesArithmeticValue = seriesArithmetic.value();
            assertEquals(18.0, seriesArithmeticValue, 0.001);
        } else {
            fail("Incorrect number of documents in a bucket in the histogram.");
        }
    }
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SimpleValue(org.elasticsearch.search.aggregations.pipeline.SimpleValue)

Aggregations

SimpleValue (org.elasticsearch.search.aggregations.pipeline.SimpleValue)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)4 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)4 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)3 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)2 PipelineAggregatorBuilders.movingAvg (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.movingAvg)2 SimpleModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel)2 CompiledScript (org.elasticsearch.script.CompiledScript)1 Script (org.elasticsearch.script.Script)1 PipelineAggregationHelperTests (org.elasticsearch.search.aggregations.pipeline.PipelineAggregationHelperTests)1 PipelineAggregatorBuilders.bucketScript (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript)1 Derivative (org.elasticsearch.search.aggregations.pipeline.derivative.Derivative)1 HoltWintersModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.HoltWintersModel)1