use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.
the class StatsBucketIT method testMetricTopLevel.
public void testMetricTopLevel() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).addAggregation(statsBucket("stats_bucket", "terms>sum")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
List<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(interval));
double bucketSum = 0;
int count = 0;
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < interval; ++i) {
Terms.Bucket bucket = buckets.get(i);
assertThat(bucket, notNullValue());
assertThat((String) bucket.getKey(), equalTo("tag" + (i % interval)));
assertThat(bucket.getDocCount(), greaterThan(0L));
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 = 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));
}
use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.
the class StatsBucketIT method testNested.
public void testNested() 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(statsBucket("avg_histo_bucket", "histo>_count"))).addAggregation(statsBucket("avg_terms_bucket", "terms>avg_histo_bucket.avg")).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));
double aggTermsSum = 0;
int aggTermsCount = 0;
double min = Double.POSITIVE_INFINITY;
double max = Double.NEGATIVE_INFINITY;
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 aggHistoSum = 0;
int aggHistoCount = 0;
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));
aggHistoCount++;
aggHistoSum += bucket.getDocCount();
}
double avgHistoValue = aggHistoCount == 0 ? Double.NaN : (aggHistoSum / aggHistoCount);
StatsBucket statsBucketValue = termsBucket.getAggregations().get("avg_histo_bucket");
assertThat(statsBucketValue, notNullValue());
assertThat(statsBucketValue.getName(), equalTo("avg_histo_bucket"));
assertThat(statsBucketValue.getAvg(), equalTo(avgHistoValue));
aggTermsCount++;
aggTermsSum += avgHistoValue;
min = Math.min(min, avgHistoValue);
max = Math.max(max, avgHistoValue);
}
double avgTermsValue = aggTermsCount == 0 ? Double.NaN : (aggTermsSum / aggTermsCount);
StatsBucket statsBucketValue = response.getAggregations().get("avg_terms_bucket");
assertThat(statsBucketValue, notNullValue());
assertThat(statsBucketValue.getName(), equalTo("avg_terms_bucket"));
assertThat(statsBucketValue.getAvg(), equalTo(avgTermsValue));
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 testNoBuckets.
public void testNoBuckets() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").includeExclude(new IncludeExclude(null, "tag.*")).subAggregation(sum("sum").field(SINGLE_VALUED_FIELD_NAME))).addAggregation(statsBucket("stats_bucket", "terms>sum")).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
List<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(0));
StatsBucket statsBucketValue = response.getAggregations().get("stats_bucket");
assertThat(statsBucketValue, notNullValue());
assertThat(statsBucketValue.getName(), equalTo("stats_bucket"));
assertThat(statsBucketValue.getAvg(), equalTo(Double.NaN));
}
use of org.elasticsearch.search.aggregations.pipeline.bucketmetrics.stats.StatsBucket in project elasticsearch by elastic.
the class StatsBucketIT method testDocCountAsSubAgg.
public void testDocCountAsSubAgg() 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(statsBucket("stats_bucket", "histo>_count"))).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 sum = 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));
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 = 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 testMetricAsSubAggWithInsertZeros.
public void testMetricAsSubAggWithInsertZeros() 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").gapPolicy(GapPolicy.INSERT_ZEROS))).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));
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));
}
}
Aggregations