Search in sources :

Example 16 with Avg

use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.

the class AvgIT method testSingleValuedField.

@Override
public void testSingleValuedField() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(avg("avg").field("value")).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Avg avg = searchResponse.getAggregations().get("avg");
    assertThat(avg, notNullValue());
    assertThat(avg.getName(), equalTo("avg"));
    assertThat(avg.getValue(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10));
}
Also used : Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 17 with Avg

use of org.elasticsearch.search.aggregations.metrics.avg.Avg 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 18 with Avg

use of org.elasticsearch.search.aggregations.metrics.avg.Avg 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 19 with Avg

use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.

the class AvgIT method testSingleValuedFieldWithValueScriptWithParams.

@Override
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = Collections.singletonMap("inc", 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(avg("avg").field("value").script(new Script(ScriptType.INLINE, FieldValueScriptEngine.NAME, "", params))).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Avg avg = searchResponse.getAggregations().get("avg");
    assertThat(avg, notNullValue());
    assertThat(avg.getName(), equalTo("avg"));
    assertThat(avg.getValue(), equalTo((double) (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11) / 10));
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with Avg

use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.

the class AvgIT method testMultiValuedFieldWithValueScriptWithParams.

@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = Collections.singletonMap("inc", 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(avg("avg").field("values").script(new Script(ScriptType.INLINE, FieldValueScriptEngine.NAME, "", params))).execute().actionGet();
    assertHitCount(searchResponse, 10);
    Avg avg = searchResponse.getAggregations().get("avg");
    assertThat(avg, notNullValue());
    assertThat(avg.getName(), equalTo("avg"));
    assertThat(avg.getValue(), equalTo((double) (3 + 4 + 4 + 5 + 5 + 6 + 6 + 7 + 7 + 8 + 8 + 9 + 9 + 10 + 10 + 11 + 11 + 12 + 12 + 13) / 20));
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) Script(org.elasticsearch.script.Script) CompiledScript(org.elasticsearch.script.CompiledScript) ExecutableScript(org.elasticsearch.script.ExecutableScript) Avg(org.elasticsearch.search.aggregations.metrics.avg.Avg) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)34 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)34 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)34 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)12 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)11 CompiledScript (org.elasticsearch.script.CompiledScript)8 ExecutableScript (org.elasticsearch.script.ExecutableScript)8 LeafSearchScript (org.elasticsearch.script.LeafSearchScript)8 Script (org.elasticsearch.script.Script)8 SearchScript (org.elasticsearch.script.SearchScript)8 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)3 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)3 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)2 Filters (org.elasticsearch.search.aggregations.bucket.filters.Filters)2 KeyedFilter (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)2 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)2 PipelineAggregatorBuilders.movingAvg (org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.movingAvg)2 SimpleValue (org.elasticsearch.search.aggregations.pipeline.SimpleValue)2 SimpleModel (org.elasticsearch.search.aggregations.pipeline.movavg.models.SimpleModel)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1