use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.
the class MinBucketIT 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(minBucket("min_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));
List<String> minKeys = new ArrayList<>();
double minValue = Double.POSITIVE_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]));
if (bucket.getDocCount() < minValue) {
minValue = bucket.getDocCount();
minKeys = new ArrayList<>();
minKeys.add(bucket.getKeyAsString());
} else if (bucket.getDocCount() == minValue) {
minKeys.add(bucket.getKeyAsString());
}
}
InternalBucketMetricValue minBucketValue = response.getAggregations().get("min_bucket");
assertThat(minBucketValue, notNullValue());
assertThat(minBucketValue.getName(), equalTo("min_bucket"));
assertThat(minBucketValue.value(), equalTo(minValue));
assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
}
use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.
the class MinBucketIT 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(minBucket("min_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();
List<String> minKeys = new ArrayList<>();
double minValue = Double.POSITIVE_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());
if (sum.value() < minValue) {
minValue = sum.value();
minKeys = new ArrayList<>();
minKeys.add(bucket.getKeyAsString());
} else if (sum.value() == minValue) {
minKeys.add(bucket.getKeyAsString());
}
}
}
InternalBucketMetricValue minBucketValue = termsBucket.getAggregations().get("min_bucket");
assertThat(minBucketValue, notNullValue());
assertThat(minBucketValue.getName(), equalTo("min_bucket"));
assertThat(minBucketValue.value(), equalTo(minValue));
assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
}
}
use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.
the class MinBucketIT 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(minBucket("min_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();
List<String> minKeys = new ArrayList<>();
double minValue = Double.POSITIVE_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() < minValue) {
minValue = bucket.getDocCount();
minKeys = new ArrayList<>();
minKeys.add(bucket.getKeyAsString());
} else if (bucket.getDocCount() == minValue) {
minKeys.add(bucket.getKeyAsString());
}
}
InternalBucketMetricValue minBucketValue = termsBucket.getAggregations().get("min_bucket");
assertThat(minBucketValue, notNullValue());
assertThat(minBucketValue.getName(), equalTo("min_bucket"));
assertThat(minBucketValue.value(), equalTo(minValue));
assertThat(minBucketValue.keys(), equalTo(minKeys.toArray(new String[minKeys.size()])));
}
}
use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.
the class PercentilesBucketIT method testNested.
public void testNested() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Terms.Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(percentilesBucket("percentile_histo_bucket", "histo>_count").percents(PERCENTS))).addAggregation(percentilesBucket("percentile_terms_bucket", "terms>percentile_histo_bucket.50").percents(PERCENTS)).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[] values = new double[termsBuckets.size()];
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 Histogram.Bucket> buckets = histo.getBuckets();
double[] innerValues = new double[numValueBuckets];
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));
innerValues[j] = bucket.getDocCount();
}
Arrays.sort(innerValues);
PercentilesBucket percentilesBucketValue = termsBucket.getAggregations().get("percentile_histo_bucket");
assertThat(percentilesBucketValue, notNullValue());
assertThat(percentilesBucketValue.getName(), equalTo("percentile_histo_bucket"));
assertPercentileBucket(PERCENTS, innerValues, percentilesBucketValue);
values[i] = percentilesBucketValue.percentile(50.0);
}
Arrays.sort(values);
PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentile_terms_bucket");
assertThat(percentilesBucketValue, notNullValue());
assertThat(percentilesBucketValue.getName(), equalTo("percentile_terms_bucket"));
assertPercentileBucket(PERCENTS, values, percentilesBucketValue);
}
use of org.elasticsearch.search.aggregations.bucket.histogram.Histogram in project elasticsearch by elastic.
the class PercentilesBucketIT method testNestedWithDecimal.
public void testNestedWithDecimal() throws Exception {
double[] percent = { 99.9 };
SearchResponse response = client().prepareSearch("idx").addAggregation(terms("terms").field("tag").order(Terms.Order.term(true)).subAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).extendedBounds(minRandomValue, maxRandomValue)).subAggregation(percentilesBucket("percentile_histo_bucket", "histo>_count").percents(percent))).addAggregation(percentilesBucket("percentile_terms_bucket", "terms>percentile_histo_bucket[99.9]").percents(percent)).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[] values = new double[termsBuckets.size()];
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 Histogram.Bucket> buckets = histo.getBuckets();
double[] innerValues = new double[numValueBuckets];
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));
innerValues[j] = bucket.getDocCount();
}
Arrays.sort(innerValues);
PercentilesBucket percentilesBucketValue = termsBucket.getAggregations().get("percentile_histo_bucket");
assertThat(percentilesBucketValue, notNullValue());
assertThat(percentilesBucketValue.getName(), equalTo("percentile_histo_bucket"));
assertPercentileBucket(innerValues, percentilesBucketValue);
values[i] = percentilesBucketValue.percentile(99.9);
}
Arrays.sort(values);
PercentilesBucket percentilesBucketValue = response.getAggregations().get("percentile_terms_bucket");
assertThat(percentilesBucketValue, notNullValue());
assertThat(percentilesBucketValue.getName(), equalTo("percentile_terms_bucket"));
for (Double p : percent) {
double expected = values[(int) ((p / 100) * values.length)];
assertThat(percentilesBucketValue.percentile(p), equalTo(expected));
}
}
Aggregations