Search in sources :

Example 21 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.

the class StatsIT method testSingleValuedFieldWithValueScriptWithParams.

@Override
public void testSingleValuedFieldWithValueScriptWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 1);
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("value").script(new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "_value + inc", 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) (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));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 22 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.

the class StatsIT method testScriptMultiValuedWithParams.

@Override
public void testScriptMultiValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("dec", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "[ doc['value'].value, doc['value'].value - dec ]", params);
    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) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) / 20));
    assertThat(stats.getMin(), equalTo(0.0));
    assertThat(stats.getMax(), equalTo(10.0));
    assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9));
    assertThat(stats.getCount(), equalTo(20L));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 23 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.

the class StatsIT method testMultiValuedFieldWithValueScript.

@Override
public void testMultiValuedFieldWithValueScript() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(stats("stats").field("values").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) (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));
}
Also used : Script(org.elasticsearch.script.Script) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 24 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.

the class StatsIT method testScriptSingleValuedWithParams.

@Override
public void testScriptSingleValuedWithParams() throws Exception {
    Map<String, Object> params = new HashMap<>();
    params.put("inc", 1);
    Script script = new Script(ScriptType.INLINE, AggregationTestScriptsPlugin.NAME, "doc['value'].value + inc", params);
    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) / 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));
}
Also used : Script(org.elasticsearch.script.Script) HashMap(java.util.HashMap) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 25 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.

the class StatsIT method testUnmapped.

@Override
public void testUnmapped() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("idx_unmapped").setQuery(matchAllQuery()).addAggregation(stats("stats").field("value")).execute().actionGet();
    assertShardExecutionState(searchResponse, 0);
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(0L));
    Stats stats = searchResponse.getAggregations().get("stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getName(), equalTo("stats"));
    assertThat(stats.getAvg(), equalTo(Double.NaN));
    assertThat(stats.getMin(), equalTo(Double.POSITIVE_INFINITY));
    assertThat(stats.getMax(), equalTo(Double.NEGATIVE_INFINITY));
    assertThat(stats.getSum(), equalTo(0.0));
    assertThat(stats.getCount(), equalTo(0L));
}
Also used : Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

Stats (org.elasticsearch.search.aggregations.metrics.stats.Stats)34 SearchResponse (org.elasticsearch.action.search.SearchResponse)31 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)31 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)11 Script (org.elasticsearch.script.Script)9 AggregationBuilders.extendedStats (org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats)9 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)9 ExtendedStats (org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats)9 HashMap (java.util.HashMap)4 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)4 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Global (org.elasticsearch.search.aggregations.bucket.global.Global)2 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 LongHashSet (com.carrotsearch.hppc.LongHashSet)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 CompiledScript (org.elasticsearch.script.CompiledScript)1