use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.
the class AdjacencyMatrixIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
BoolQueryBuilder boolQ = new BoolQueryBuilder();
boolQ.must(termQuery("tag", "tag1"));
boolQ.must(termQuery("tag", "tag2"));
SearchResponse response = client().prepareSearch("idx").addAggregation(adjacencyMatrix("tags", newMap("tag1", termQuery("tag", "tag1")).add("tag2", termQuery("tag", "tag2")).add("both", boolQ)).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
AdjacencyMatrix matrix = response.getAggregations().get("tags");
assertThat(matrix, notNullValue());
assertThat(matrix.getName(), equalTo("tags"));
int expectedBuckets = 0;
if (numTag1Docs > 0) {
expectedBuckets++;
}
if (numTag2Docs > 0) {
expectedBuckets++;
}
if (numMultiTagDocs > 0) {
// both, both&tag1, both&tag2, tag1&tag2
expectedBuckets += 4;
}
assertThat(matrix.getBuckets().size(), equalTo(expectedBuckets));
assertThat(matrix.getProperty("_bucket_count"), equalTo(expectedBuckets));
Object[] propertiesKeys = (Object[]) matrix.getProperty("_key");
Object[] propertiesDocCounts = (Object[]) matrix.getProperty("_count");
Object[] propertiesCounts = (Object[]) matrix.getProperty("avg_value.value");
assertEquals(expectedBuckets, propertiesKeys.length);
assertEquals(propertiesKeys.length, propertiesDocCounts.length);
assertEquals(propertiesKeys.length, propertiesCounts.length);
for (int i = 0; i < propertiesCounts.length; i++) {
AdjacencyMatrix.Bucket bucket = matrix.getBucketByKey(propertiesKeys[i].toString());
assertThat(bucket, Matchers.notNullValue());
Avg avgValue = bucket.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat((long) propertiesDocCounts[i], equalTo(bucket.getDocCount()));
assertThat((double) propertiesCounts[i], equalTo(avgValue.getValue()));
}
AdjacencyMatrix.Bucket tag1Bucket = matrix.getBucketByKey("tag1");
assertThat(tag1Bucket, Matchers.notNullValue());
assertThat(tag1Bucket.getDocCount(), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numSingleTag1Docs; i++) {
sum += i + 1;
}
for (int i = numSingleTag1Docs + numSingleTag2Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag1Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket1Value = tag1Bucket.getAggregations().get("avg_value");
assertThat(avgBucket1Value, notNullValue());
assertThat(avgBucket1Value.getName(), equalTo("avg_value"));
assertThat(avgBucket1Value.getValue(), equalTo((double) sum / numTag1Docs));
Bucket tag2Bucket = matrix.getBucketByKey("tag2");
assertThat(tag2Bucket, Matchers.notNullValue());
assertThat(tag2Bucket.getDocCount(), equalTo((long) numTag2Docs));
sum = 0;
for (int i = numSingleTag1Docs; i < numDocs; i++) {
sum += i + 1;
}
assertThat(tag2Bucket.getAggregations().asList().isEmpty(), is(false));
Avg avgBucket2Value = tag2Bucket.getAggregations().get("avg_value");
assertThat(avgBucket2Value, notNullValue());
assertThat(avgBucket2Value.getName(), equalTo("avg_value"));
assertThat(avgBucket2Value.getValue(), equalTo((double) sum / numTag2Docs));
// Check intersection buckets are computed correctly by comparing with
// ANDed query bucket results
Bucket bucketBothQ = matrix.getBucketByKey("both");
if (numMultiTagDocs == 0) {
// Empty intersections are not returned.
assertThat(bucketBothQ, Matchers.nullValue());
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.nullValue());
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.nullValue());
} else {
assertThat(bucketBothQ, Matchers.notNullValue());
assertThat(bucketBothQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueBothQ = bucketBothQ.getAggregations().get("avg_value");
Bucket bucketIntersectQ = matrix.getBucketByKey("tag1&tag2");
assertThat(bucketIntersectQ, Matchers.notNullValue());
assertThat(bucketIntersectQ.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueIntersectQ = bucketBothQ.getAggregations().get("avg_value");
assertThat(avgValueIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
Bucket tag1Both = matrix.getBucketByKey("both&tag1");
assertThat(tag1Both, Matchers.notNullValue());
assertThat(tag1Both.getDocCount(), equalTo((long) numMultiTagDocs));
Avg avgValueTag1BothIntersectQ = tag1Both.getAggregations().get("avg_value");
assertThat(avgValueTag1BothIntersectQ.getValue(), equalTo(avgValueBothQ.getValue()));
}
}
use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.
the class FilterIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx").addAggregation(filter("tag1", termQuery("tag", "tag1")).subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
Filter filter = response.getAggregations().get("tag1");
assertThat(filter, notNullValue());
assertThat(filter.getName(), equalTo("tag1"));
assertThat(filter.getDocCount(), equalTo((long) numTag1Docs));
assertThat((long) filter.getProperty("_count"), equalTo((long) numTag1Docs));
long sum = 0;
for (int i = 0; i < numTag1Docs; ++i) {
sum += i + 1;
}
assertThat(filter.getAggregations().asList().isEmpty(), is(false));
Avg avgValue = filter.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / numTag1Docs));
assertThat((double) filter.getProperty("avg_value.value"), equalTo((double) sum / numTag1Docs));
}
use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.
the class DoubleTermsIT method testSingleValuedFieldOrderedBySingleValueSubAggregationDesc.
public void testSingleValuedFieldOrderedBySingleValueSubAggregationDesc() throws Exception {
boolean asc = false;
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("avg_i", asc)).subAggregation(avg("avg_i").field(SINGLE_VALUED_FIELD_NAME))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 4; i >= 0; i--) {
Terms.Bucket bucket = terms.getBucketByKey("" + (double) i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + (double) i));
assertThat(bucket.getDocCount(), equalTo(1L));
Avg avg = bucket.getAggregations().get("avg_i");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo((double) i));
}
}
use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.
the class LongTermsIT method testSingleValuedFieldOrderedBySingleValueSubAggregationAsc.
public void testSingleValuedFieldOrderedBySingleValueSubAggregationAsc() throws Exception {
boolean asc = true;
SearchResponse response = client().prepareSearch("idx").setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("avg_i", asc)).subAggregation(avg("avg_i").field(SINGLE_VALUED_FIELD_NAME))).execute().actionGet();
assertSearchResponse(response);
Terms terms = response.getAggregations().get("terms");
assertThat(terms, notNullValue());
assertThat(terms.getName(), equalTo("terms"));
assertThat(terms.getBuckets().size(), equalTo(5));
for (int i = 0; i < 5; i++) {
Terms.Bucket bucket = terms.getBucketByKey("" + i);
assertThat(bucket, notNullValue());
assertThat(key(bucket), equalTo("" + i));
assertThat(bucket.getDocCount(), equalTo(1L));
Avg avg = bucket.getAggregations().get("avg_i");
assertThat(avg, notNullValue());
assertThat(avg.getValue(), equalTo((double) i));
}
}
use of org.elasticsearch.search.aggregations.metrics.avg.Avg in project elasticsearch by elastic.
the class MissingIT method testWithSubAggregation.
public void testWithSubAggregation() throws Exception {
SearchResponse response = client().prepareSearch("idx", "unmapped_idx").addAggregation(missing("missing_tag").field("tag").subAggregation(avg("avg_value").field("value"))).execute().actionGet();
assertSearchResponse(response);
assertThat("Not all shards are initialized", response.getSuccessfulShards(), equalTo(response.getTotalShards()));
Missing missing = response.getAggregations().get("missing_tag");
assertThat(missing, notNullValue());
assertThat(missing.getName(), equalTo("missing_tag"));
assertThat(missing.getDocCount(), equalTo((long) numDocsMissing + numDocsUnmapped));
assertThat((long) missing.getProperty("_count"), equalTo((long) numDocsMissing + numDocsUnmapped));
assertThat(missing.getAggregations().asList().isEmpty(), is(false));
long sum = 0;
for (int i = 0; i < numDocsMissing; ++i) {
sum += i;
}
for (int i = 0; i < numDocsUnmapped; ++i) {
sum += i;
}
Avg avgValue = missing.getAggregations().get("avg_value");
assertThat(avgValue, notNullValue());
assertThat(avgValue.getName(), equalTo("avg_value"));
assertThat(avgValue.getValue(), equalTo((double) sum / (numDocsMissing + numDocsUnmapped)));
assertThat((double) missing.getProperty("avg_value.value"), equalTo((double) sum / (numDocsMissing + numDocsUnmapped)));
}
Aggregations