4949import java .util .Map ;
5050import java .util .Optional ;
5151import java .util .concurrent .ExecutionException ;
52- import java .util .function .Consumer ;
52+ import java .util .function .BiConsumer ;
5353
5454import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
5555import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
@@ -97,7 +97,10 @@ public void testSnapshotAndRestore() throws Exception {
9797 boolean requireRouting = randomBoolean ();
9898 boolean useNested = randomBoolean ();
9999 IndexRequestBuilder [] builders = snashotAndRestore (sourceIdx , 1 , true , requireRouting , useNested );
100- assertHits (sourceIdx , builders .length );
100+ IndicesStatsResponse indicesStatsResponse = client ().admin ().indices ().prepareStats (sourceIdx ).clear ().setDocs (true ).get ();
101+ long deleted = indicesStatsResponse .getTotal ().docs .getDeleted ();
102+ boolean sourceHadDeletions = deleted > 0 ; // we use indexRandom which might create holes ie. deleted docs
103+ assertHits (sourceIdx , builders .length , sourceHadDeletions );
101104 assertMappings (sourceIdx , requireRouting , useNested );
102105 SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> {
103106 client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ()
@@ -116,7 +119,7 @@ public void testSnapshotAndRestore() throws Exception {
116119 client ().admin ().indices ().prepareUpdateSettings (sourceIdx )
117120 .setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 )).get ();
118121 ensureGreen (sourceIdx );
119- assertHits (sourceIdx , builders .length );
122+ assertHits (sourceIdx , builders .length , sourceHadDeletions );
120123 }
121124
122125 public void testSnapshotAndRestoreWithNested () throws Exception {
@@ -125,7 +128,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
125128 IndexRequestBuilder [] builders = snashotAndRestore (sourceIdx , 1 , true , requireRouting , true );
126129 IndicesStatsResponse indicesStatsResponse = client ().admin ().indices ().prepareStats ().clear ().setDocs (true ).get ();
127130 assertThat (indicesStatsResponse .getTotal ().docs .getDeleted (), Matchers .greaterThan (0L ));
128- assertHits (sourceIdx , builders .length );
131+ assertHits (sourceIdx , builders .length , true );
129132 assertMappings (sourceIdx , requireRouting , true );
130133 SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () ->
131134 client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ().addIds ("" + randomIntBetween (0 , builders .length ))).get ());
@@ -141,7 +144,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
141144 client ().admin ().indices ().prepareUpdateSettings (sourceIdx ).setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 ))
142145 .get ();
143146 ensureGreen (sourceIdx );
144- assertHits (sourceIdx , builders .length );
147+ assertHits (sourceIdx , builders .length , true );
145148 }
146149
147150 private void assertMappings (String sourceIdx , boolean requireRouting , boolean useNested ) throws IOException {
@@ -165,15 +168,12 @@ private void assertMappings(String sourceIdx, boolean requireRouting, boolean us
165168 }
166169 }
167170
168- private void assertHits (String index , int numDocsExpected ) {
171+ private void assertHits (String index , int numDocsExpected , boolean sourceHadDeletions ) {
169172 SearchResponse searchResponse = client ().prepareSearch (index )
170173 .addSort (SeqNoFieldMapper .NAME , SortOrder .ASC )
171174 .setSize (numDocsExpected ).get ();
172- Consumer <SearchResponse > assertConsumer = res -> {
175+ BiConsumer <SearchResponse , Boolean > assertConsumer = ( res , allowHoles ) -> {
173176 SearchHits hits = res .getHits ();
174- IndicesStatsResponse indicesStatsResponse = client ().admin ().indices ().prepareStats ().clear ().setDocs (true ).get ();
175- long deleted = indicesStatsResponse .getTotal ().docs .getDeleted ();
176- boolean allowHoles = deleted > 0 ; // we use indexRandom which might create holes ie. deleted docs
177177 long i = 0 ;
178178 for (SearchHit hit : hits ) {
179179 String id = hit .getId ();
@@ -190,18 +190,24 @@ private void assertHits(String index, int numDocsExpected) {
190190 assertEquals ("r" + id , hit .field ("_routing" ).getValue ());
191191 }
192192 };
193- assertConsumer .accept (searchResponse );
193+ assertConsumer .accept (searchResponse , sourceHadDeletions );
194194 assertEquals (numDocsExpected , searchResponse .getHits ().totalHits );
195195 searchResponse = client ().prepareSearch (index )
196196 .addSort (SeqNoFieldMapper .NAME , SortOrder .ASC )
197197 .setScroll ("1m" )
198198 .slice (new SliceBuilder (SeqNoFieldMapper .NAME , randomIntBetween (0 ,1 ), 2 ))
199199 .setSize (randomIntBetween (1 , 10 )).get ();
200- do {
201- // now do a scroll with a slice
202- assertConsumer .accept (searchResponse );
203- searchResponse = client ().prepareSearchScroll (searchResponse .getScrollId ()).setScroll (TimeValue .timeValueMinutes (1 )).get ();
204- } while (searchResponse .getHits ().getHits ().length > 0 );
200+ try {
201+ do {
202+ // now do a scroll with a slice
203+ assertConsumer .accept (searchResponse , true );
204+ searchResponse = client ().prepareSearchScroll (searchResponse .getScrollId ()).setScroll (TimeValue .timeValueMinutes (1 )).get ();
205+ } while (searchResponse .getHits ().getHits ().length > 0 );
206+ } finally {
207+ if (searchResponse .getScrollId () != null ) {
208+ client ().prepareClearScroll ().addScrollId (searchResponse .getScrollId ()).get ();
209+ }
210+ }
205211
206212 }
207213
0 commit comments