use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testSingleValuedFieldWithValueScript.
@Override
public void testSingleValuedFieldWithValueScript() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("value").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value + 1", emptyMap()))).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) (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11) / 10));
assertThat(stats.getMin(), equalTo(2.0));
assertThat(stats.getMax(), equalTo(11.0));
assertThat(stats.getSum(), equalTo((double) 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11));
assertThat(stats.getCount(), equalTo(10L));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testScriptSingleValued.
@Override
public void testScriptSingleValued() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value", emptyMap()))).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) / 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 testScriptMultiValued.
@Override
public void testScriptMultiValued() throws Exception {
Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['values'].values", emptyMap());
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").script(script)).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) (2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12) / 20));
assertThat(stats.getMin(), equalTo(2.0));
assertThat(stats.getMax(), equalTo(12.0));
assertThat(stats.getSum(), equalTo((double) 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12));
assertThat(stats.getCount(), equalTo(20L));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class MoreExpressionTests method testSpecialValueVariable.
public void testSpecialValueVariable() throws Exception {
// i.e. _value for aggregations
createIndex("test");
ensureGreen("test");
indexRandom(true, client().prepareIndex("test", "doc", "1").setSource("x", 5, "y", 1.2), client().prepareIndex("test", "doc", "2").setSource("x", 10, "y", 1.4), client().prepareIndex("test", "doc", "3").setSource("x", 13, "y", 1.8));
SearchRequestBuilder req = client().prepareSearch().setIndices("test");
req.setQuery(QueryBuilders.matchAllQuery()).addAggregation(AggregationBuilders.stats("int_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value * 3", Collections.emptyMap()))).addAggregation(AggregationBuilders.stats("double_agg").field("y").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "_value - 1.1", Collections.emptyMap()))).addAggregation(// specifically to test a script w/o _value
AggregationBuilders.stats("const_agg").field("x").script(new Script(ScriptType.INLINE, ExpressionScriptEngineService.NAME, "3.0", Collections.emptyMap())));
SearchResponse rsp = req.get();
assertEquals(3, rsp.getHits().getTotalHits());
Stats stats = rsp.getAggregations().get("int_agg");
assertEquals(39.0, stats.getMax(), 0.0001);
assertEquals(15.0, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("double_agg");
assertEquals(0.7, stats.getMax(), 0.0001);
assertEquals(0.1, stats.getMin(), 0.0001);
stats = rsp.getAggregations().get("const_agg");
assertThat(stats.getMax(), equalTo(3.0));
assertThat(stats.getMin(), equalTo(3.0));
assertThat(stats.getAvg(), equalTo(3.0));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project graylog2-server by Graylog2.
the class TermsStatsResult method getResults.
public List<Map<String, Object>> getResults() {
List<Map<String, Object>> results = Lists.newArrayList();
for (Terms.Bucket e : facet.getBuckets()) {
Map<String, Object> resultMap = Maps.newHashMap();
resultMap.put("key_field", e.getKey());
resultMap.put("count", e.getDocCount());
final Stats stats = e.getAggregations().get(Searches.AGG_STATS);
resultMap.put("min", stats.getMin());
resultMap.put("max", stats.getMax());
resultMap.put("total", stats.getSum());
resultMap.put("total_count", stats.getCount());
resultMap.put("mean", stats.getAvg());
results.add(resultMap);
}
// Sort results by descending mean value
Collections.sort(results, COMPARATOR);
return results;
}
Aggregations