use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.
the class StatsBucketIT method testMetricAsSubAgg.
public void testMetricAsSubAgg() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).subAggregation(statsBucket("stats_bucket", "histo>sum"))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
List<Terms.Bucket> termsBuckets = terms.getBuckets();
assertThat(termsBuckets.size(), equalTo(interval));
for (int i = 0; i < interval; ++i) {
Terms.Bucket termsBucket = termsBuckets.get(i);
assertThat(termsBucket, notNullValue());
assertThat((String) termsBucket.getKey(), equalTo("tag" + (i % interval)));
Histogram histo = termsBucket.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
double bucketSum = 0;
int count = 0;
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (int j = 0; j < numValueBuckets; ++j) {
Histogram.Bucket bucket = buckets.get(j);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) j * interval));
if (bucket.getDocCount() != 0) {
Sum sum = bucket.getAggregations().get("sum");
assertThat(sum, notNullValue());
count++;
bucketSum += sum.value();
min = Math.min(min, sum.value());
max = Math.max(max, sum.value());
}
}
double avgValue = count == 0 ? Double.NaN : (bucketSum / count);
StatsBucket statsBucketValue = termsBucket.getAggregations().get("stats_bucket");
assertThat(statsBucketValue, notNullValue());
assertThat(statsBucketValue.getName(), equalTo("stats_bucket"));
assertThat(statsBucketValue.getAvg(), equalTo(avgValue));
assertThat(statsBucketValue.getMin(), equalTo(min));
assertThat(statsBucketValue.getMax(), equalTo(max));
}
}
use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.
the class StatsBucketIT method testDocCountTopLevel.
public void testDocCountTopLevel() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).addAggregation(statsBucket("stats_bucket", "histo>_count")).execute().actionGet();
assertSearchResponse(response);
Histogram histo = response.getAggregations().get("histo");
assertThat(histo, notNullValue());
assertThat(histo.getName(), equalTo("histo"));
List<? extends Bucket> buckets = histo.getBuckets();
assertThat(buckets.size(), equalTo(numValueBuckets));
double sum = 0;
int count = 0;
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat(((Number) bucket.getKey()).longValue(), equalTo((long) i * interval));
assertThat(bucket.getDocCount(), equalTo(valueCounts[i]));
count++;
sum += bucket.getDocCount();
min = Math.min(min, bucket.getDocCount());
max = Math.max(max, bucket.getDocCount());
}
double avgValue = count == 0 ? Double.NaN : (sum / count);
StatsBucket statsBucketValue = response.getAggregations().get("stats_bucket");
assertThat(statsBucketValue, notNullValue());
assertThat(statsBucketValue.getName(), equalTo("stats_bucket"));
assertThat(statsBucketValue.getAvg(), equalTo(avgValue));
assertThat(statsBucketValue.getMin(), equalTo(min));
assertThat(statsBucketValue.getMax(), equalTo(max));
}
Aggregations