49
49
import java .util .Map ;
50
50
import java .util .Optional ;
51
51
import java .util .concurrent .ExecutionException ;
52
- import java .util .function .Consumer ;
52
+ import java .util .function .BiConsumer ;
53
53
54
54
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
55
55
import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
@@ -97,7 +97,10 @@ public void testSnapshotAndRestore() throws Exception {
97
97
boolean requireRouting = randomBoolean ();
98
98
boolean useNested = randomBoolean ();
99
99
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 );
101
104
assertMappings (sourceIdx , requireRouting , useNested );
102
105
SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () -> {
103
106
client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ()
@@ -116,7 +119,7 @@ public void testSnapshotAndRestore() throws Exception {
116
119
client ().admin ().indices ().prepareUpdateSettings (sourceIdx )
117
120
.setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 )).get ();
118
121
ensureGreen (sourceIdx );
119
- assertHits (sourceIdx , builders .length );
122
+ assertHits (sourceIdx , builders .length , sourceHadDeletions );
120
123
}
121
124
122
125
public void testSnapshotAndRestoreWithNested () throws Exception {
@@ -125,7 +128,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
125
128
IndexRequestBuilder [] builders = snashotAndRestore (sourceIdx , 1 , true , requireRouting , true );
126
129
IndicesStatsResponse indicesStatsResponse = client ().admin ().indices ().prepareStats ().clear ().setDocs (true ).get ();
127
130
assertThat (indicesStatsResponse .getTotal ().docs .getDeleted (), Matchers .greaterThan (0L ));
128
- assertHits (sourceIdx , builders .length );
131
+ assertHits (sourceIdx , builders .length , true );
129
132
assertMappings (sourceIdx , requireRouting , true );
130
133
SearchPhaseExecutionException e = expectThrows (SearchPhaseExecutionException .class , () ->
131
134
client ().prepareSearch (sourceIdx ).setQuery (QueryBuilders .idsQuery ().addIds ("" + randomIntBetween (0 , builders .length ))).get ());
@@ -141,7 +144,7 @@ public void testSnapshotAndRestoreWithNested() throws Exception {
141
144
client ().admin ().indices ().prepareUpdateSettings (sourceIdx ).setSettings (Settings .builder ().put ("index.number_of_replicas" , 1 ))
142
145
.get ();
143
146
ensureGreen (sourceIdx );
144
- assertHits (sourceIdx , builders .length );
147
+ assertHits (sourceIdx , builders .length , true );
145
148
}
146
149
147
150
private void assertMappings (String sourceIdx , boolean requireRouting , boolean useNested ) throws IOException {
@@ -165,15 +168,12 @@ private void assertMappings(String sourceIdx, boolean requireRouting, boolean us
165
168
}
166
169
}
167
170
168
- private void assertHits (String index , int numDocsExpected ) {
171
+ private void assertHits (String index , int numDocsExpected , boolean sourceHadDeletions ) {
169
172
SearchResponse searchResponse = client ().prepareSearch (index )
170
173
.addSort (SeqNoFieldMapper .NAME , SortOrder .ASC )
171
174
.setSize (numDocsExpected ).get ();
172
- Consumer <SearchResponse > assertConsumer = res -> {
175
+ BiConsumer <SearchResponse , Boolean > assertConsumer = ( res , allowHoles ) -> {
173
176
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
177
177
long i = 0 ;
178
178
for (SearchHit hit : hits ) {
179
179
String id = hit .getId ();
@@ -190,18 +190,24 @@ private void assertHits(String index, int numDocsExpected) {
190
190
assertEquals ("r" + id , hit .field ("_routing" ).getValue ());
191
191
}
192
192
};
193
- assertConsumer .accept (searchResponse );
193
+ assertConsumer .accept (searchResponse , sourceHadDeletions );
194
194
assertEquals (numDocsExpected , searchResponse .getHits ().totalHits );
195
195
searchResponse = client ().prepareSearch (index )
196
196
.addSort (SeqNoFieldMapper .NAME , SortOrder .ASC )
197
197
.setScroll ("1m" )
198
198
.slice (new SliceBuilder (SeqNoFieldMapper .NAME , randomIntBetween (0 ,1 ), 2 ))
199
199
.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
+ }
205
211
206
212
}
207
213
0 commit comments