Search in sources :

Example 16 with Stats

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));
}
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 17 with Stats

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));
}
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 18 with Stats

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));
}
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 19 with Stats

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));
}
Also used : Script(org.elasticsearch.script.Script) PipelineAggregatorBuilders.bucketScript(org.elasticsearch.search.aggregations.pipeline.PipelineAggregatorBuilders.bucketScript) CompiledScript(org.elasticsearch.script.CompiledScript) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 20 with Stats

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;
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) Map(java.util.Map)

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