|
35 | 35 | import org.elasticsearch.common.xcontent.XContentHelper;
|
36 | 36 | import org.elasticsearch.index.IndexSettings;
|
37 | 37 | import org.elasticsearch.index.query.QueryBuilders;
|
| 38 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
38 | 39 | import org.elasticsearch.rest.RestStatus;
|
39 | 40 | import org.elasticsearch.search.SearchHit;
|
40 | 41 | import org.elasticsearch.search.sort.FieldSortBuilder;
|
|
53 | 54 | import static org.elasticsearch.index.query.QueryBuilders.termQuery;
|
54 | 55 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
55 | 56 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertHitCount;
|
| 57 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoFailures; |
56 | 58 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertNoSearchHits;
|
57 | 59 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchHits;
|
58 | 60 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
|
@@ -616,6 +618,43 @@ public void testInvalidScrollKeepAlive() throws IOException {
|
616 | 618 | assertThat(illegalArgumentException.getMessage(), containsString("Keep alive for scroll (3h) is too large"));
|
617 | 619 | }
|
618 | 620 |
|
| 621 | + /** |
| 622 | + * Ensures that we always create and retain search contexts on every target shards for a scroll request |
| 623 | + * regardless whether that query can be written to match_no_docs on some target shards or not. |
| 624 | + */ |
| 625 | + public void testScrollRewrittenToMatchNoDocs() { |
| 626 | + final int numShards = randomIntBetween(3, 5); |
| 627 | + assertAcked( |
| 628 | + client().admin().indices().prepareCreate("test") |
| 629 | + .setSettings(Settings.builder().put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, numShards)) |
| 630 | + .addMapping("_doc", "created_date", "type=date,format=yyyy-MM-dd")); |
| 631 | + client().prepareIndex("test", "_doc").setId("1").setSource("created_date", "2020-01-01").get(); |
| 632 | + client().prepareIndex("test", "_doc").setId("2").setSource("created_date", "2020-01-02").get(); |
| 633 | + client().prepareIndex("test", "_doc").setId("3").setSource("created_date", "2020-01-03").get(); |
| 634 | + client().admin().indices().prepareRefresh("test").get(); |
| 635 | + SearchResponse resp = null; |
| 636 | + try { |
| 637 | + int totalHits = 0; |
| 638 | + resp = client().prepareSearch("test") |
| 639 | + .setQuery(new RangeQueryBuilder("created_date").gte("2020-01-02").lte("2020-01-03")) |
| 640 | + .setMaxConcurrentShardRequests(randomIntBetween(1, 3)) // sometimes fan out shard requests one by one |
| 641 | + .setSize(randomIntBetween(1, 2)) |
| 642 | + .setScroll(TimeValue.timeValueMinutes(1)) |
| 643 | + .get(); |
| 644 | + assertNoFailures(resp); |
| 645 | + while (resp.getHits().getHits().length > 0) { |
| 646 | + totalHits += resp.getHits().getHits().length; |
| 647 | + resp = client().prepareSearchScroll(resp.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get(); |
| 648 | + assertNoFailures(resp); |
| 649 | + } |
| 650 | + assertThat(totalHits, equalTo(2)); |
| 651 | + } finally { |
| 652 | + if (resp != null && resp.getScrollId() != null) { |
| 653 | + client().prepareClearScroll().addScrollId(resp.getScrollId()).get(); |
| 654 | + } |
| 655 | + } |
| 656 | + } |
| 657 | + |
619 | 658 | private void assertToXContentResponse(ClearScrollResponse response, boolean succeed, int numFreed) throws IOException {
|
620 | 659 | XContentBuilder builder = XContentFactory.jsonBuilder();
|
621 | 660 | response.toXContent(builder, ToXContent.EMPTY_PARAMS);
|
|
0 commit comments