use of org.elasticsearch.search.aggregations.bucket.nested.ReverseNested in project elasticsearch by elastic.
the class ReverseNestedIT method testSameParentDocHavingMultipleBuckets.
public void testSameParentDocHavingMultipleBuckets() throws Exception {
XContentBuilder mapping = jsonBuilder().startObject().startObject("product").field("dynamic", "strict").startObject("properties").startObject("id").field("type", "long").endObject().startObject("category").field("type", "nested").startObject("properties").startObject("name").field("type", "keyword").endObject().endObject().endObject().startObject("sku").field("type", "nested").startObject("properties").startObject("sku_type").field("type", "keyword").endObject().startObject("colors").field("type", "nested").startObject("properties").startObject("name").field("type", "keyword").endObject().endObject().endObject().endObject().endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("idx3").setSettings(Settings.builder().put(SETTING_NUMBER_OF_SHARDS, 1).put(SETTING_NUMBER_OF_REPLICAS, 0)).addMapping("product", mapping));
client().prepareIndex("idx3", "product", "1").setRefreshPolicy(IMMEDIATE).setSource(jsonBuilder().startObject().startArray("sku").startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "red").endObject().startObject().field("name", "green").endObject().startObject().field("name", "yellow").endObject().endArray().endObject().startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "red").endObject().startObject().field("name", "blue").endObject().startObject().field("name", "white").endObject().endArray().endObject().startObject().field("sku_type", "bar1").startArray("colors").startObject().field("name", "black").endObject().startObject().field("name", "blue").endObject().endArray().endObject().startObject().field("sku_type", "bar2").startArray("colors").startObject().field("name", "orange").endObject().endArray().endObject().startObject().field("sku_type", "bar2").startArray("colors").startObject().field("name", "pink").endObject().endArray().endObject().endArray().startArray("category").startObject().field("name", "abc").endObject().startObject().field("name", "klm").endObject().startObject().field("name", "xyz").endObject().endArray().endObject()).get();
SearchResponse response = client().prepareSearch("idx3").addAggregation(nested("nested_0", "category").subAggregation(terms("group_by_category").field("category.name").subAggregation(reverseNested("to_root").subAggregation(nested("nested_1", "sku").subAggregation(filter("filter_by_sku", termQuery("sku.sku_type", "bar1")).subAggregation(count("sku_count").field("sku.sku_type"))))))).get();
assertNoFailures(response);
assertHitCount(response, 1);
Nested nested0 = response.getAggregations().get("nested_0");
assertThat(nested0.getDocCount(), equalTo(3L));
Terms terms = nested0.getAggregations().get("group_by_category");
assertThat(terms.getBuckets().size(), equalTo(3));
for (String bucketName : new String[] { "abc", "klm", "xyz" }) {
logger.info("Checking results for bucket {}", bucketName);
Terms.Bucket bucket = terms.getBucketByKey(bucketName);
assertThat(bucket.getDocCount(), equalTo(1L));
ReverseNested toRoot = bucket.getAggregations().get("to_root");
assertThat(toRoot.getDocCount(), equalTo(1L));
Nested nested1 = toRoot.getAggregations().get("nested_1");
assertThat(nested1.getDocCount(), equalTo(5L));
Filter filterByBar = nested1.getAggregations().get("filter_by_sku");
assertThat(filterByBar.getDocCount(), equalTo(3L));
ValueCount barCount = filterByBar.getAggregations().get("sku_count");
assertThat(barCount.getValue(), equalTo(3L));
}
response = client().prepareSearch("idx3").addAggregation(nested("nested_0", "category").subAggregation(terms("group_by_category").field("category.name").subAggregation(reverseNested("to_root").subAggregation(nested("nested_1", "sku").subAggregation(filter("filter_by_sku", termQuery("sku.sku_type", "bar1")).subAggregation(nested("nested_2", "sku.colors").subAggregation(filter("filter_sku_color", termQuery("sku.colors.name", "red")).subAggregation(reverseNested("reverse_to_sku").path("sku").subAggregation(count("sku_count").field("sku.sku_type")))))))))).get();
assertNoFailures(response);
assertHitCount(response, 1);
nested0 = response.getAggregations().get("nested_0");
assertThat(nested0.getDocCount(), equalTo(3L));
terms = nested0.getAggregations().get("group_by_category");
assertThat(terms.getBuckets().size(), equalTo(3));
for (String bucketName : new String[] { "abc", "klm", "xyz" }) {
logger.info("Checking results for bucket {}", bucketName);
Terms.Bucket bucket = terms.getBucketByKey(bucketName);
assertThat(bucket.getDocCount(), equalTo(1L));
ReverseNested toRoot = bucket.getAggregations().get("to_root");
assertThat(toRoot.getDocCount(), equalTo(1L));
Nested nested1 = toRoot.getAggregations().get("nested_1");
assertThat(nested1.getDocCount(), equalTo(5L));
Filter filterByBar = nested1.getAggregations().get("filter_by_sku");
assertThat(filterByBar.getDocCount(), equalTo(3L));
Nested nested2 = filterByBar.getAggregations().get("nested_2");
assertThat(nested2.getDocCount(), equalTo(8L));
Filter filterBarColor = nested2.getAggregations().get("filter_sku_color");
assertThat(filterBarColor.getDocCount(), equalTo(2L));
ReverseNested reverseToBar = filterBarColor.getAggregations().get("reverse_to_sku");
assertThat(reverseToBar.getDocCount(), equalTo(2L));
ValueCount barCount = reverseToBar.getAggregations().get("sku_count");
assertThat(barCount.getValue(), equalTo(2L));
}
}
Aggregations