|
20 | 20 |
|
21 | 21 | import org.elasticsearch.action.index.IndexRequestBuilder;
|
22 | 22 | import org.elasticsearch.action.search.SearchResponse;
|
| 23 | +import org.elasticsearch.action.update.UpdateResponse; |
23 | 24 | import org.elasticsearch.search.SearchHit;
|
24 | 25 | import org.elasticsearch.search.aggregations.bucket.children.Children;
|
25 | 26 | import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
| 27 | +import org.elasticsearch.search.aggregations.metrics.sum.Sum; |
26 | 28 | import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
|
27 | 29 | import org.elasticsearch.search.sort.SortOrder;
|
28 | 30 | import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
|
33 | 35 | import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
34 | 36 | import static org.elasticsearch.search.aggregations.AggregationBuilders.*;
|
35 | 37 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
| 38 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; |
36 | 39 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
37 | 40 | import static org.hamcrest.Matchers.equalTo;
|
| 41 | +import static org.hamcrest.Matchers.greaterThan; |
38 | 42 | import static org.hamcrest.Matchers.is;
|
39 | 43 |
|
40 | 44 | /**
|
@@ -215,6 +219,45 @@ public void testParentWithMultipleBuckets() throws Exception {
|
215 | 219 | assertThat(topHits.getHits().getAt(0).getType(), equalTo("comment"));
|
216 | 220 | }
|
217 | 221 |
|
| 222 | + @Test |
| 223 | + public void testWithDeletes() throws Exception { |
| 224 | + String indexName = "xyz"; |
| 225 | + assertAcked( |
| 226 | + prepareCreate(indexName) |
| 227 | + .addMapping("parent") |
| 228 | + .addMapping("child", "_parent", "type=parent", "count", "type=long") |
| 229 | + ); |
| 230 | + |
| 231 | + List<IndexRequestBuilder> requests = new ArrayList<>(); |
| 232 | + requests.add(client().prepareIndex(indexName, "parent", "1").setSource("{}")); |
| 233 | + requests.add(client().prepareIndex(indexName, "child", "0").setParent("1").setSource("count", 1)); |
| 234 | + requests.add(client().prepareIndex(indexName, "child", "1").setParent("1").setSource("count", 1)); |
| 235 | + requests.add(client().prepareIndex(indexName, "child", "2").setParent("1").setSource("count", 1)); |
| 236 | + requests.add(client().prepareIndex(indexName, "child", "3").setParent("1").setSource("count", 1)); |
| 237 | + indexRandom(true, requests); |
| 238 | + |
| 239 | + for (int i = 0; i < 10; i++) { |
| 240 | + SearchResponse searchResponse = client().prepareSearch(indexName) |
| 241 | + .addAggregation(children("children").childType("child").subAggregation(sum("counts").field("count"))) |
| 242 | + .get(); |
| 243 | + |
| 244 | + assertNoFailures(searchResponse); |
| 245 | + Children children = searchResponse.getAggregations().get("children"); |
| 246 | + assertThat(children.getDocCount(), equalTo(4l)); |
| 247 | + |
| 248 | + Sum count = children.getAggregations().get("counts"); |
| 249 | + assertThat(count.getValue(), equalTo(4.)); |
| 250 | + |
| 251 | + String idToUpdate = Integer.toString(randomInt(3)); |
| 252 | + UpdateResponse updateResponse = client().prepareUpdate(indexName, "child", idToUpdate) |
| 253 | + .setParent("1") |
| 254 | + .setDoc("count", 1) |
| 255 | + .get(); |
| 256 | + assertThat(updateResponse.getVersion(), greaterThan(1l)); |
| 257 | + refresh(); |
| 258 | + } |
| 259 | + } |
| 260 | + |
218 | 261 | private static final class Control {
|
219 | 262 |
|
220 | 263 | final String category;
|
|
0 commit comments