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));
}
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));
}
}
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));
}
}
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));
}
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));
}
Aggregations