Search in sources :

Example 31 with Stats

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

the class NestedIT method testSimple.

public void testSimple() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(nested("nested", "nested").subAggregation(stats("nested_value_stats").field("nested.value"))).execute().actionGet();
    assertSearchResponse(response);
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    long sum = 0;
    long count = 0;
    for (int i = 0; i < numParents; ++i) {
        for (int j = 0; j < numChildren[i]; ++j) {
            final long value = i + 1 + j;
            min = Math.min(min, value);
            max = Math.max(max, value);
            sum += value;
            ++count;
        }
    }
    Nested nested = response.getAggregations().get("nested");
    assertThat(nested, notNullValue());
    assertThat(nested.getName(), equalTo("nested"));
    assertThat(nested.getDocCount(), equalTo(count));
    assertThat(nested.getAggregations().asList().isEmpty(), is(false));
    Stats stats = nested.getAggregations().get("nested_value_stats");
    assertThat(stats, notNullValue());
    assertThat(stats.getMin(), equalTo(min));
    assertThat(stats.getMax(), equalTo(max));
    assertThat(stats.getCount(), equalTo(count));
    assertThat(stats.getSum(), equalTo((double) sum));
    assertThat(stats.getAvg(), equalTo((double) sum / count));
}
Also used : Nested(org.elasticsearch.search.aggregations.bucket.nested.Nested) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 32 with Stats

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

the class StringTermsIT method testSingleValuedFieldOrderedByMultiValueSubAggregationDesc.

public void testSingleValuedFieldOrderedByMultiValueSubAggregationDesc() throws Exception {
    boolean asc = false;
    SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("stats.avg", asc)).subAggregation(stats("stats").field("i"))).execute().actionGet();
    assertSearchResponse(response);
    Terms terms = response.getAggregations().get("terms");
    assertThat(terms, notNullValue());
    assertThat(terms.getName(), equalTo("terms"));
    assertThat(terms.getBuckets().size(), equalTo(5));
    int i = 4;
    for (Terms.Bucket bucket : terms.getBuckets()) {
        assertThat(bucket, notNullValue());
        assertThat(key(bucket), equalTo("val" + i));
        assertThat(bucket.getDocCount(), equalTo(1L));
        Stats stats = bucket.getAggregations().get("stats");
        assertThat(stats, notNullValue());
        assertThat(stats.getMax(), equalTo((double) i));
        i--;
    }
}
Also used : Bucket(org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) AggregationBuilders.extendedStats(org.elasticsearch.search.aggregations.AggregationBuilders.extendedStats) ExtendedStats(org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 33 with Stats

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

the class HistogramIT method testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc.

public void testSingleValuedFieldOrderedByMultiValuedSubAggregationDesc() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).order(Histogram.Order.aggregation("stats.sum", false)).subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME))).execute().actionGet();
    assertSearchResponse(response);
    Histogram histo = response.getAggregations().get("histo");
    assertThat(histo, notNullValue());
    assertThat(histo.getName(), equalTo("histo"));
    assertThat(histo.getBuckets().size(), equalTo(numValueBuckets));
    LongHashSet visited = new LongHashSet();
    double previousSum = Double.POSITIVE_INFINITY;
    // TODO: use diamond once JI-9019884 is fixed
    List<Histogram.Bucket> buckets = new ArrayList<>(histo.getBuckets());
    for (int i = 0; i < numValueBuckets; ++i) {
        Histogram.Bucket bucket = buckets.get(i);
        assertThat(bucket, notNullValue());
        long key = ((Number) bucket.getKey()).longValue();
        assertTrue(visited.add(key));
        int b = (int) (key / interval);
        assertThat(bucket.getDocCount(), equalTo(valueCounts[b]));
        assertThat(bucket.getAggregations().asList().isEmpty(), is(false));
        Stats stats = bucket.getAggregations().get("stats");
        assertThat(stats, notNullValue());
        long s = 0;
        for (int j = 0; j < numDocs; ++j) {
            if ((j + 1) / interval == b) {
                s += j + 1;
            }
        }
        assertThat(stats.getSum(), equalTo((double) s));
        assertThat(stats.getSum(), lessThanOrEqualTo(previousSum));
        previousSum = s;
    }
}
Also used : LongHashSet(com.carrotsearch.hppc.LongHashSet) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) Bucket(org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket) ArrayList(java.util.ArrayList) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 34 with Stats

use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project graylog2-server by Graylog2.

the class FieldHistogramResult method getResults.

@Override
public Map<Long, Map<String, Number>> getResults() {
    if (result.getBuckets().isEmpty()) {
        return Collections.emptyMap();
    }
    final Map<Long, Map<String, Number>> results = Maps.newTreeMap();
    for (Histogram.Bucket b : result.getBuckets()) {
        final ImmutableMap.Builder<String, Number> resultMap = ImmutableMap.builder();
        resultMap.put("total_count", b.getDocCount());
        final Stats stats = b.getAggregations().get(Searches.AGG_STATS);
        resultMap.put("count", stats.getCount());
        resultMap.put("min", stats.getMin());
        resultMap.put("max", stats.getMax());
        resultMap.put("total", stats.getSum());
        resultMap.put("mean", stats.getAvg());
        // cardinality is only calculated if it was explicitly requested, so this might be null
        final Cardinality cardinality = b.getAggregations().get(Searches.AGG_CARDINALITY);
        resultMap.put("cardinality", cardinality == null ? 0 : cardinality.getValue());
        final DateTime keyAsDate = (DateTime) b.getKey();
        final long timestamp = keyAsDate.getMillis() / 1000L;
        results.put(timestamp, resultMap.build());
    }
    final long minTimestamp = Collections.min(results.keySet());
    final long maxTimestamp = Collections.max(results.keySet());
    final MutableDateTime currentTime = new MutableDateTime(minTimestamp, DateTimeZone.UTC);
    while (currentTime.getMillis() < maxTimestamp) {
        final Map<String, Number> entry = results.get(currentTime.getMillis());
        // advance timestamp by the interval's seconds value
        currentTime.add(interval.getPeriod());
        if (entry == null) {
            // synthesize a 0 value for this timestamp
            results.put(currentTime.getMillis(), EMPTY_RESULT);
        }
    }
    return results;
}
Also used : Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Cardinality(org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality) MutableDateTime(org.joda.time.MutableDateTime) ImmutableMap(com.google.common.collect.ImmutableMap) DateTime(org.joda.time.DateTime) MutableDateTime(org.joda.time.MutableDateTime) Stats(org.elasticsearch.search.aggregations.metrics.stats.Stats) ImmutableMap(com.google.common.collect.ImmutableMap) 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