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));
}
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--;
}
}
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;
}
}
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;
}
Aggregations