public void testNestedAsSubAggregation() throws Exception { SearchResponse response = client() .prepareSearch("idx") .addAggregation( terms("top_values") .field("value") .size(100) .collectMode(aggCollectionMode) .subAggregation( nested("nested", "nested") .subAggregation(max("max_value").field("nested.value")))) .execute() .actionGet(); assertSearchResponse(response); LongTerms values = response.getAggregations().get("top_values"); assertThat(values, notNullValue()); assertThat(values.getName(), equalTo("top_values")); assertThat(values.getBuckets(), notNullValue()); assertThat(values.getBuckets().size(), equalTo(numParents)); for (int i = 0; i < numParents; i++) { String topValue = "" + (i + 1); assertThat(values.getBucketByKey(topValue), notNullValue()); Nested nested = values.getBucketByKey(topValue).getAggregations().get("nested"); assertThat(nested, notNullValue()); Max max = nested.getAggregations().get("max_value"); assertThat(max, notNullValue()); assertThat( max.getValue(), equalTo(numChildren[i] == 0 ? Double.NEGATIVE_INFINITY : (double) i + numChildren[i])); } }
public void testNestedWithSubTermsAgg() throws Exception { SearchResponse response = client() .prepareSearch("idx") .addAggregation( nested("nested", "nested") .subAggregation( terms("values") .field("nested.value") .size(100) .collectMode(aggCollectionMode))) .execute() .actionGet(); assertSearchResponse(response); long docCount = 0; long[] counts = new long[numParents + 6]; for (int i = 0; i < numParents; ++i) { for (int j = 0; j < numChildren[i]; ++j) { final int value = i + 1 + j; ++counts[value]; ++docCount; } } int uniqueValues = 0; for (long count : counts) { if (count > 0) { ++uniqueValues; } } Nested nested = response.getAggregations().get("nested"); assertThat(nested, notNullValue()); assertThat(nested.getName(), equalTo("nested")); assertThat(nested.getDocCount(), equalTo(docCount)); assertThat((long) nested.getProperty("_count"), equalTo(docCount)); assertThat(nested.getAggregations().asList().isEmpty(), is(false)); LongTerms values = nested.getAggregations().get("values"); assertThat(values, notNullValue()); assertThat(values.getName(), equalTo("values")); assertThat(values.getBuckets(), notNullValue()); assertThat(values.getBuckets().size(), equalTo(uniqueValues)); for (int i = 0; i < counts.length; ++i) { final String key = Long.toString(i); if (counts[i] == 0) { assertNull(values.getBucketByKey(key)); } else { Bucket bucket = values.getBucketByKey(key); assertNotNull(bucket); assertEquals(counts[i], bucket.getDocCount()); } } assertThat((LongTerms) nested.getProperty("values"), sameInstance(values)); }