Search in sources :

Example 1 with Derivative

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

the class DateDerivativeIT method assertBucket.

private static void assertBucket(Histogram.Bucket bucket, DateTime expectedKey, long expectedDocCount, Matcher<Object> derivativeMatcher, Double derivative, Double normalizedDerivative) {
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(expectedKey));
    assertThat(bucket.getDocCount(), equalTo(expectedDocCount));
    Derivative docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, derivativeMatcher);
    if (docCountDeriv != null) {
        assertThat(docCountDeriv.value(), closeTo(derivative, 0.00001));
        assertThat(docCountDeriv.normalizedValue(), closeTo(normalizedDerivative, 0.00001));
    }
}
Also used : Derivative(org.elasticsearch.search.aggregations.pipeline.derivative.Derivative)

Example 2 with Derivative

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

the class DerivativeIT method testSingleValuedField_normalised.

/**
     * test first and second derivative on the sing
     */
public void testSingleValuedField_normalised() {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).minDocCount(0).subAggregation(derivative("deriv", "_count").unit("1ms")).subAggregation(derivative("2nd_deriv", "deriv").unit("10ms"))).execute().actionGet();
    assertSearchResponse(response);
    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(numValueBuckets));
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        checkBucketKeyAndDocCount("InternalBucket " + i, bucket, i * interval, valueCounts[i]);
        Derivative docCountDeriv = bucket.getAggregations().get("deriv");
        if (i > 0) {
            assertThat(docCountDeriv, notNullValue());
            assertThat(docCountDeriv.value(), closeTo((firstDerivValueCounts[i - 1]), 0.00001));
            assertThat(docCountDeriv.normalizedValue(), closeTo((double) (firstDerivValueCounts[i - 1]) / 5, 0.00001));
        } else {
            assertThat(docCountDeriv, nullValue());
        }
        Derivative docCount2ndDeriv = bucket.getAggregations().get("2nd_deriv");
        if (i > 1) {
            assertThat(docCount2ndDeriv, notNullValue());
            assertThat(docCount2ndDeriv.value(), closeTo((secondDerivValueCounts[i - 2]), 0.00001));
            assertThat(docCount2ndDeriv.normalizedValue(), closeTo((double) (secondDerivValueCounts[i - 2]) * 2, 0.00001));
        } else {
            assertThat(docCount2ndDeriv, nullValue());
        }
    }
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) 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)

Example 3 with Derivative

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

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

the class DateDerivativeIT method testSingleValuedFieldNormalised.

public void testSingleValuedFieldNormalised() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(dateHistogram("histo").field("date").dateHistogramInterval(DateHistogramInterval.MONTH).minDocCount(0).subAggregation(derivative("deriv", "_count").unit(DateHistogramInterval.DAY))).execute().actionGet();
    assertSearchResponse(response);
    Histogram deriv = response.getAggregations().get("histo");
    assertThat(deriv, notNullValue());
    assertThat(deriv.getName(), equalTo("histo"));
    List<? extends Bucket> buckets = deriv.getBuckets();
    assertThat(buckets.size(), equalTo(3));
    DateTime key = new DateTime(2012, 1, 1, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(1L));
    Derivative docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, nullValue());
    key = new DateTime(2012, 2, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(1);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(2L));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), closeTo(1d, 0.00001));
    assertThat(docCountDeriv.normalizedValue(), closeTo(1d / 31d, 0.00001));
    key = new DateTime(2012, 3, 1, 0, 0, DateTimeZone.UTC);
    bucket = buckets.get(2);
    assertThat(bucket, notNullValue());
    assertThat((DateTime) bucket.getKey(), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(3L));
    docCountDeriv = bucket.getAggregations().get("deriv");
    assertThat(docCountDeriv, notNullValue());
    assertThat(docCountDeriv.value(), closeTo(1d, 0.00001));
    assertThat(docCountDeriv.normalizedValue(), closeTo(1d / 29d, 0.00001));
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) AggregationBuilders.dateHistogram(org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Derivative(org.elasticsearch.search.aggregations.pipeline.derivative.Derivative) DateTime(org.joda.time.DateTime) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Derivative (org.elasticsearch.search.aggregations.pipeline.derivative.Derivative)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)3 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)3 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)3 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)3 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)1 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)1 PipelineAggregatorBuilders.movingAvg (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.movingAvg)1 SimpleValue (org.elasticsearch.search.aggregations.pipeline.SimpleValue)1 SimpleModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel)1 DateTime (org.joda.time.DateTime)1