|
18 | 18 | */
|
19 | 19 | package org.elasticsearch.search.aggregations.bucket;
|
20 | 20 |
|
| 21 | +import org.elasticsearch.action.index.IndexRequest; |
21 | 22 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
22 | 23 | import org.elasticsearch.action.search.SearchResponse;
|
23 | 24 | import org.elasticsearch.action.update.UpdateResponse;
|
24 | 25 | import org.elasticsearch.cluster.metadata.IndexMetaData;
|
25 | 26 | import org.elasticsearch.common.settings.Settings;
|
26 | 27 | import org.elasticsearch.search.SearchHit;
|
| 28 | +import org.elasticsearch.search.aggregations.AggregationBuilder; |
| 29 | +import org.elasticsearch.search.aggregations.AggregationBuilders; |
27 | 30 | import org.elasticsearch.search.aggregations.bucket.children.Children;
|
28 | 31 | import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
29 | 32 | import org.elasticsearch.search.aggregations.metrics.sum.Sum;
|
@@ -392,6 +395,65 @@ public void testHierarchicalChildrenAggs() {
|
392 | 395 | assertThat(terms.getBuckets().get(0).getDocCount(), equalTo(1l));
|
393 | 396 | }
|
394 | 397 |
|
| 398 | + public void testPostCollectAllLeafReaders() throws Exception { |
| 399 | + // The 'towns' and 'parent_names' aggs operate on parent docs and if child docs are in different segments we need |
| 400 | + // to ensure those segments which child docs are also evaluated to in the post collect phase. |
| 401 | + |
| 402 | + // Before we only evaluated segments that yielded matches in 'towns' and 'parent_names' aggs, which caused |
| 403 | + // us to miss to evaluate child docs in segments we didn't have parent matches for. |
| 404 | + |
| 405 | + assertAcked( |
| 406 | + prepareCreate("index") |
| 407 | + .addMapping("parentType", "name", "type=string,index=not_analyzed", "town", "type=string,index=not_analyzed") |
| 408 | + .addMapping("childType", "_parent", "type=parentType", "name", "type=string,index=not_analyzed", "age", "type=integer") |
| 409 | + ); |
| 410 | + List<IndexRequestBuilder> requests = new ArrayList<>(); |
| 411 | + requests.add(client().prepareIndex("index", "parentType", "1").setSource("name", "Bob", "town", "Memphis")); |
| 412 | + requests.add(client().prepareIndex("index", "parentType", "2").setSource("name", "Alice", "town", "Chicago")); |
| 413 | + requests.add(client().prepareIndex("index", "parentType", "3").setSource("name", "Bill", "town", "Chicago")); |
| 414 | + requests.add(client().prepareIndex("index", "childType", "1").setSource("name", "Jill", "age", 5).setParent("1")); |
| 415 | + requests.add(client().prepareIndex("index", "childType", "2").setSource("name", "Joey", "age", 3).setParent("1")); |
| 416 | + requests.add(client().prepareIndex("index", "childType", "3").setSource("name", "John", "age", 2).setParent("2")); |
| 417 | + requests.add(client().prepareIndex("index", "childType", "4").setSource("name", "Betty", "age", 6).setParent("3")); |
| 418 | + requests.add(client().prepareIndex("index", "childType", "5").setSource("name", "Dan", "age", 1).setParent("3")); |
| 419 | + indexRandom(true, requests); |
| 420 | + |
| 421 | + SearchResponse response = client().prepareSearch("index") |
| 422 | + .setSize(0) |
| 423 | + .addAggregation(AggregationBuilders.terms("towns").field("town") |
| 424 | + .subAggregation(AggregationBuilders.terms("parent_names").field("name") |
| 425 | + .subAggregation(AggregationBuilders.children("child_docs").childType("childType")) |
| 426 | + ) |
| 427 | + ) |
| 428 | + .get(); |
| 429 | + |
| 430 | + Terms towns = response.getAggregations().get("towns"); |
| 431 | + assertThat(towns.getBuckets().size(), equalTo(2)); |
| 432 | + assertThat(towns.getBuckets().get(0).getKeyAsString(), equalTo("Chicago")); |
| 433 | + assertThat(towns.getBuckets().get(0).getDocCount(), equalTo(2L)); |
| 434 | + |
| 435 | + Terms parents = towns.getBuckets().get(0).getAggregations().get("parent_names"); |
| 436 | + assertThat(parents.getBuckets().size(), equalTo(2)); |
| 437 | + assertThat(parents.getBuckets().get(0).getKeyAsString(), equalTo("Alice")); |
| 438 | + assertThat(parents.getBuckets().get(0).getDocCount(), equalTo(1L)); |
| 439 | + Children children = parents.getBuckets().get(0).getAggregations().get("child_docs"); |
| 440 | + assertThat(children.getDocCount(), equalTo(1L)); |
| 441 | + |
| 442 | + assertThat(parents.getBuckets().get(1).getKeyAsString(), equalTo("Bill")); |
| 443 | + assertThat(parents.getBuckets().get(1).getDocCount(), equalTo(1L)); |
| 444 | + children = parents.getBuckets().get(1).getAggregations().get("child_docs"); |
| 445 | + assertThat(children.getDocCount(), equalTo(2L)); |
| 446 | + |
| 447 | + assertThat(towns.getBuckets().get(1).getKeyAsString(), equalTo("Memphis")); |
| 448 | + assertThat(towns.getBuckets().get(1).getDocCount(), equalTo(1L)); |
| 449 | + parents = towns.getBuckets().get(1).getAggregations().get("parent_names"); |
| 450 | + assertThat(parents.getBuckets().size(), equalTo(1)); |
| 451 | + assertThat(parents.getBuckets().get(0).getKeyAsString(), equalTo("Bob")); |
| 452 | + assertThat(parents.getBuckets().get(0).getDocCount(), equalTo(1L)); |
| 453 | + children = parents.getBuckets().get(0).getAggregations().get("child_docs"); |
| 454 | + assertThat(children.getDocCount(), equalTo(2L)); |
| 455 | + } |
| 456 | + |
395 | 457 | private static final class Control {
|
396 | 458 |
|
397 | 459 | final String category;
|
|
0 commit comments