4949import java .util .Map ;
5050import java .util .Optional ;
5151import java .util .concurrent .ExecutionException ;
52- import java .util .function .BiConsumer ;
52+ import java .util .function .Consumer ;
5353
5454import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
5555import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
@@ -97,10 +97,7 @@ public void testSnapshotAndRestore() throws Exception {
9797 boolean requireRouting = randomBoolean ();
9898 boolean useNested = randomBoolean ();
9999 IndexRequestBuilder [] builders = snashotAndRestore (sourceIdx , 1 , true , requireRouting , useNested );
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 );
100+ assertHits (sourceIdx , builders .length );
104101 assertMappings (sourceIdx , requireRouting , useNested );
105102 SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> {
106103 client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ()
@@ -119,7 +116,7 @@ public void testSnapshotAndRestore() throws Exception {
119116 client ().admin ().indices ().prepareUpdateSettings (sourceIdx )
120117 .setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 )).get ();
121118 ensureGreen (sourceIdx );
122- assertHits (sourceIdx , builders .length , sourceHadDeletions );
119+ assertHits (sourceIdx , builders .length );
123120 }
124121
125122 public void testSnapshotAndRestoreWithNested () throws Exception {
@@ -128,7 +125,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
128125 IndexRequestBuilder [] builders = snashotAndRestore (sourceIdx , 1 , true , requireRouting , true );
129126 IndicesStatsResponse indicesStatsResponse = client ().admin ().indices ().prepareStats ().clear ().setDocs (true ).get ();
130127 assertThat (indicesStatsResponse .getTotal ().docs .getDeleted (), Matchers .greaterThan (0L ));
131- assertHits (sourceIdx , builders .length , true );
128+ assertHits (sourceIdx , builders .length );
132129 assertMappings (sourceIdx , requireRouting , true );
133130 SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () ->
134131 client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ().addIds ("" + randomIntBetween (0 , builders .length ))).get ());
@@ -144,7 +141,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
144141 client ().admin ().indices ().prepareUpdateSettings (sourceIdx ).setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 ))
145142 .get ();
146143 ensureGreen (sourceIdx );
147- assertHits (sourceIdx , builders .length , true );
144+ assertHits (sourceIdx , builders .length );
148145 }
149146
150147 private void assertMappings (String sourceIdx , boolean requireRouting , boolean useNested ) throws IOException {
@@ -168,12 +165,15 @@ private void assertMappings(String sourceIdx, boolean requireRouting, boolean us
168165 }
169166 }
170167
171- private void assertHits (String index , int numDocsExpected , boolean sourceHadDeletions ) {
168+ private void assertHits (String index , int numDocsExpected ) {
172169 SearchResponse searchResponse = client ().prepareSearch (index )
173170 .addSort (SeqNoFieldMapper .NAME , SortOrder .ASC )
174171 .setSize (numDocsExpected ).get ();
175- BiConsumer <SearchResponse , Boolean > assertConsumer = ( res , allowHoles ) -> {
172+ Consumer <SearchResponse > assertConsumer = res -> {
176173 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,24 +190,18 @@ private void assertHits(String index, int numDocsExpected, boolean sourceHadDele
190190 assertEquals ("r" + id , hit .field ("_routing" ).getValue ());
191191 }
192192 };
193- assertConsumer .accept (searchResponse , sourceHadDeletions );
193+ assertConsumer .accept (searchResponse );
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- 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- }
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 );
211205
212206 }
213207
0 commit comments