use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testSingleValuedFieldGetProperty.
@Override
public void testSingleValuedFieldGetProperty() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx").setQuery(matchAllQuery()).addAggregation(global("global").subAggregation(stats("stats").field("value"))).execute().actionGet();
assertHitCount(searchResponse, 10);
Global global = searchResponse.getAggregations().get("global");
assertThat(global, notNullValue());
assertThat(global.getName(), equalTo("global"));
assertThat(global.getDocCount(), equalTo(10L));
assertThat(global.getAggregations(), notNullValue());
assertThat(global.getAggregations().asMap().size(), equalTo(1));
Stats stats = global.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("stats"));
Stats statsFromProperty = (Stats) global.getProperty("stats");
assertThat(statsFromProperty, notNullValue());
assertThat(statsFromProperty, sameInstance(stats));
double expectedAvgValue = (double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10;
assertThat(stats.getAvg(), equalTo(expectedAvgValue));
assertThat((double) global.getProperty("stats.avg"), equalTo(expectedAvgValue));
double expectedMinValue = 1.0;
assertThat(stats.getMin(), equalTo(expectedMinValue));
assertThat((double) global.getProperty("stats.min"), equalTo(expectedMinValue));
double expectedMaxValue = 10.0;
assertThat(stats.getMax(), equalTo(expectedMaxValue));
assertThat((double) global.getProperty("stats.max"), equalTo(expectedMaxValue));
double expectedSumValue = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;
assertThat(stats.getSum(), equalTo(expectedSumValue));
assertThat((double) global.getProperty("stats.sum"), equalTo(expectedSumValue));
long expectedCountValue = 10;
assertThat(stats.getCount(), equalTo(expectedCountValue));
assertThat((double) global.getProperty("stats.count"), equalTo((double) expectedCountValue));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StatsIT method testSingleValuedFieldPartiallyUnmapped.
@Override
public void testSingleValuedFieldPartiallyUnmapped() throws Exception {
SearchResponse searchResponse = client().prepareSearch("idx", "idx_unmapped").setQuery(matchAllQuery()).addAggregation(stats("stats").field("value")).execute().actionGet();
assertShardExecutionState(searchResponse, 0);
assertHitCount(searchResponse, 10);
Stats stats = searchResponse.getAggregations().get("stats");
assertThat(stats, notNullValue());
assertThat(stats.getName(), equalTo("stats"));
assertThat(stats.getAvg(), equalTo((double) (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) / 10));
assertThat(stats.getMin(), equalTo(1.0));
assertThat(stats.getMax(), equalTo(10.0));
assertThat(stats.getSum(), equalTo((double) 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10));
assertThat(stats.getCount(), equalTo(10L));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class StringTermsIT method testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars.
public void testSingleValuedFieldOrderedBySubAggregationAscMultiHierarchyLevelsSpecialChars() throws Exception {
StringBuilder filter2NameBuilder = new StringBuilder("filt.er2");
filter2NameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
String filter2Name = filter2NameBuilder.toString();
StringBuilder statsNameBuilder = new StringBuilder("st.ats");
statsNameBuilder.append(randomAsciiOfLengthBetween(3, 10).replace("[", "").replace("]", "").replace(">", ""));
String statsName = statsNameBuilder.toString();
boolean asc = randomBoolean();
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("tags").executionHint(randomExecutionHint()).field("tag").collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("filter1>" + filter2Name + ">" + statsName + ".max", asc)).subAggregation(filter("filter1", QueryBuilders.matchAllQuery()).subAggregation(filter(filter2Name, QueryBuilders.matchAllQuery()).subAggregation(stats(statsName).field("i"))))).execute().actionGet();
assertSearchResponse(response);
Terms tags = response.getAggregations().get("tags");
assertThat(tags, notNullValue());
assertThat(tags.getName(), equalTo("tags"));
assertThat(tags.getBuckets().size(), equalTo(2));
Iterator<Terms.Bucket> iters = tags.getBuckets().iterator();
// the max for "more" is 2
// the max for "less" is 4
Terms.Bucket tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "more" : "less"));
assertThat(tag.getDocCount(), equalTo(asc ? 3L : 2L));
Filter filter1 = tag.getAggregations().get("filter1");
assertThat(filter1, notNullValue());
assertThat(filter1.getDocCount(), equalTo(asc ? 3L : 2L));
Filter filter2 = filter1.getAggregations().get(filter2Name);
assertThat(filter2, notNullValue());
assertThat(filter2.getDocCount(), equalTo(asc ? 3L : 2L));
Stats stats = filter2.getAggregations().get(statsName);
assertThat(stats, notNullValue());
assertThat(stats.getMax(), equalTo(asc ? 2.0 : 4.0));
tag = iters.next();
assertThat(tag, notNullValue());
assertThat(key(tag), equalTo(asc ? "less" : "more"));
assertThat(tag.getDocCount(), equalTo(asc ? 2L : 3L));
filter1 = tag.getAggregations().get("filter1");
assertThat(filter1, notNullValue());
assertThat(filter1.getDocCount(), equalTo(asc ? 2L : 3L));
filter2 = filter1.getAggregations().get(filter2Name);
assertThat(filter2, notNullValue());
assertThat(filter2.getDocCount(), equalTo(asc ? 2L : 3L));
stats = filter2.getAggregations().get(statsName);
assertThat(stats, notNullValue());
assertThat(stats.getMax(), equalTo(asc ? 4.0 : 2.0));
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class MissingValueIT method testStats.
public void testStats() {
SearchResponse response = client().prepareSearch("idx").addAggregation(stats("stats").field("long").missing(5)).get();
assertSearchResponse(response);
Stats stats = response.getAggregations().get("stats");
assertEquals(2, stats.getCount());
assertEquals(4, stats.getAvg(), 0);
}
use of org.elasticsearch.search.aggregations.metrics.stats.Stats in project elasticsearch by elastic.
the class DerivativeIT method testMultiValueAggDerivative.
public void testMultiValueAggDerivative() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(histogram("histo").field(SINGLE_VALUED_FIELD_NAME).interval(interval).subAggregation(stats("stats").field(SINGLE_VALUED_FIELD_NAME)).subAggregation(derivative("deriv", "stats.sum"))).execute().actionGet();
assertSearchResponse(response);
Histogram deriv = response.getAggregations().get("histo");
assertThat(deriv, notNullValue());
assertThat(deriv.getName(), equalTo("histo"));
assertThat(deriv.getBuckets().size(), equalTo(numValueBuckets));
Object[] propertiesKeys = (Object[]) deriv.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) deriv.getProperty("_count");
Object[] propertiesSumCounts = (Object[]) deriv.getProperty("stats.sum");
List<Bucket> buckets = new ArrayList<Bucket>(deriv.getBuckets());
// start value, gets
Long expectedSumPreviousBucket = Long.MIN_VALUE;
// overwritten
for (int i = 0; i < numValueBuckets; ++i) {
Histogram.Bucket bucket = buckets.get(i);
checkBucketKeyAndDocCount("InternalBucket " + i, bucket, i * interval, valueCounts[i]);
Stats stats = bucket.getAggregations().get("stats");
assertThat(stats, notNullValue());
long expectedSum = valueCounts[i] * (i * interval);
assertThat(stats.getSum(), equalTo((double) expectedSum));
SimpleValue sumDeriv = bucket.getAggregations().get("deriv");
if (i > 0) {
assertThat(sumDeriv, notNullValue());
long sumDerivValue = expectedSum - expectedSumPreviousBucket;
assertThat(sumDeriv.value(), equalTo((double) sumDerivValue));
assertThat((double) bucket.getProperty("histo", AggregationPath.parse("deriv.value").getPathElementsAsStringList()), equalTo((double) sumDerivValue));
} else {
assertThat(sumDeriv, nullValue());
}
expectedSumPreviousBucket = expectedSum;
assertThat(propertiesKeys[i], equalTo((double) i * interval));
assertThat((long) propertiesDocCounts[i], equalTo(valueCounts[i]));
assertThat((double) propertiesSumCounts[i], equalTo((double) expectedSum));
}
}
Aggregations