use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testSingleValuedField_WithFormatter.
public void testSingleValuedField_WithFormatter() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").format("0000.0").field("value")).execute().actionGet();
assertHitCount(searchResponse, 10);
Stats stats = searchResponse.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("stats"));
assertThat(stats.getAvg(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10));
assertThat(stats.getAvgAsString(), equalTo("0005.5"));
assertThat(stats.getMin(), equalTo(1.0));
assertThat(stats.getMinAsString(), equalTo("0001.0"));
assertThat(stats.getMax(), equalTo(10.0));
assertThat(stats.getMaxAsString(), equalTo("0010.0"));
assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10));
assertThat(stats.getSumAsString(), equalTo("0055.0"));
assertThat(stats.getCount(), equalTo(10L));
assertThat(stats.getCountAsString(), equalTo("0010.0"));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testOrderByEmptyAggregation.
@Override
public void testOrderByEmptyAggregation() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(terms("terms").field("value").order(Order.compound(Order.aggregation("filter>stats.avg", true))).subAggregation(filter("filter", termQuery("value", 100)).subAggregation(stats("stats").field("value")))).get();
assertHitCount(searchResponse, 10);
Terms terms = searchResponse.getAggregations().get("terms");
assertThat(terms, notNullValue());
List<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets, notNullValue());
assertThat(buckets.size(), equalTo(10));
for (int i = 0; i < 10; i++) {
Terms.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(bucket.getKeyAsNumber(), equalTo((long) i + 1));
assertThat(bucket.getDocCount(), equalTo(1L));
Filter filter = bucket.getAggregations().get("filter");
assertThat(filter, notNullValue());
assertThat(filter.getDocCount(), equalTo(0L));
Stats stats = filter.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY));
assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY));
assertThat(stats.getAvg(), equalTo(Double.NaN));
assertThat(stats.getSum(), equalTo(0.0));
assertThat(stats.getCount(), equalTo(0L));
}
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testSingleValuedField.
@Override
public void testSingleValuedField() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("value")).execute().actionGet();
assertShardExecutionState(searchResponse, 0);
assertHitCount(searchResponse, 10);
Stats stats = searchResponse.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("stats"));
assertThat(stats.getAvg(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10));
assertThat(stats.getMin(), equalTo(1.0));
assertThat(stats.getMax(), equalTo(10.0));
assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10));
assertThat(stats.getCount(), equalTo(10L));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testMultiValuedFieldWithValueScriptWithParams.
@Override
public void testMultiValuedFieldWithValueScriptWithParams() throws Exception {
Map<String, Object> params = new HashMap<>();
params.put("dec", 1);
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("values").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value - dec", params))).get();
assertShardExecutionState(searchResponse, 0);
assertHitCount(searchResponse, 10);
Stats stats = searchResponse.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("stats"));
assertThat(stats.getAvg(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11) / 20));
assertThat(stats.getMin(), equalTo(1.0));
assertThat(stats.getMax(), equalTo(11.0));
assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11));
assertThat(stats.getCount(), equalTo(20L));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testPartiallyUnmapped.
public void testPartiallyUnmapped() {
Stats s1 = client().prepareSearch("idx").addAggregation(stats("stats").field("value")).get().getAggregations().get("stats");
Stats s2 = client().prepareSearch("idx", "idx_unmapped").addAggregation(stats("stats").field("value")).get().getAggregations().get("stats");
assertEquals(s1.getAvg(), s2.getAvg(), 1e-10);
assertEquals(s1.getCount(), s2.getCount());
assertEquals(s1.getMin(), s2.getMin(), 0d);
assertEquals(s1.getMax(), s2.getMax(), 0d);
}
Aggregations