@@ -43,7 +43,7 @@ public abstract class AbstractSearchableSnapshotsRestTestCase extends ESRestTest
43
43
44
44
protected abstract Settings repositorySettings ();
45
45
46
- public void testSearchableSnapshots ( ) throws Exception {
46
+ private void runSearchableSnapshotsTest ( SearchableSnapshotsTestCaseBody testCaseBody ) throws Exception {
47
47
final String repositoryType = repositoryType ();
48
48
final Settings repositorySettings = repositorySettings ();
49
49
@@ -117,34 +117,7 @@ public void testSearchableSnapshots() throws Exception {
117
117
final Number count = count (restoredIndexName );
118
118
assertThat ("Wrong index count for index " + restoredIndexName , count .intValue (), equalTo (numDocs ));
119
119
120
- for (int i = 0 ; i < 10 ; i ++) {
121
- final int randomTieBreaker = randomIntBetween (1 , numDocs - 1 );
122
- Map <String , Object > searchResults ;
123
- switch (randomInt (3 )) {
124
- case 0 :
125
- searchResults = search (restoredIndexName , QueryBuilders .termQuery ("field" , String .valueOf (randomTieBreaker )));
126
- assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (1 ));
127
- @ SuppressWarnings ("unchecked" )
128
- Map <String , Object > searchHit = (Map <String , Object >) ((List <?>) extractValue (searchResults , "hits.hits" )).get (0 );
129
- assertThat (extractValue (searchHit , "_index" ), equalTo (restoredIndexName ));
130
- assertThat (extractValue (searchHit , "_source.field" ), equalTo (randomTieBreaker ));
131
- break ;
132
- case 1 :
133
- searchResults = search (restoredIndexName , QueryBuilders .rangeQuery ("field" ).lt (randomTieBreaker ));
134
- assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (randomTieBreaker ));
135
- break ;
136
- case 2 :
137
- searchResults = search (restoredIndexName , QueryBuilders .rangeQuery ("field" ).gte (randomTieBreaker ));
138
- assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (numDocs - randomTieBreaker ));
139
- break ;
140
- case 3 :
141
- searchResults = search (restoredIndexName , QueryBuilders .matchQuery ("text" , "document" ));
142
- assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (numDocs ));
143
- break ;
144
- default :
145
- fail ("Unsupported randomized search query" );
146
- }
147
- }
120
+ testCaseBody .runTest (restoredIndexName , numDocs );
148
121
149
122
logger .info ("deleting snapshot [{}]" , snapshot );
150
123
deleteSnapshot (repository , snapshot , false );
@@ -154,6 +127,71 @@ public void testSearchableSnapshots() throws Exception {
154
127
searchableSnapshotStats .size (), equalTo (numberOfShards ));
155
128
}
156
129
130
+ public void testSearchResults () throws Exception {
131
+ runSearchableSnapshotsTest ((restoredIndexName , numDocs ) -> {
132
+ for (int i = 0 ; i < 10 ; i ++) {
133
+ assertSearchResults (restoredIndexName , numDocs , randomFrom (Boolean .TRUE , Boolean .FALSE , null ));
134
+ }
135
+ });
136
+ }
137
+
138
+ public void testSearchResultsWhenFrozen () throws Exception {
139
+ runSearchableSnapshotsTest ((restoredIndexName , numDocs ) -> {
140
+ final Request freezeRequest = new Request (HttpPost .METHOD_NAME , restoredIndexName + "/_freeze" );
141
+ assertOK (client ().performRequest (freezeRequest ));
142
+ ensureGreen (restoredIndexName );
143
+ for (int i = 0 ; i < 10 ; i ++) {
144
+ assertSearchResults (restoredIndexName , numDocs , Boolean .FALSE );
145
+ }
146
+ });
147
+ }
148
+
149
+ public void testCloseAndReopen () throws Exception {
150
+ runSearchableSnapshotsTest ((restoredIndexName , numDocs ) -> {
151
+ final Request closeRequest = new Request (HttpPost .METHOD_NAME , restoredIndexName + "/_close" );
152
+ assertOK (client ().performRequest (closeRequest ));
153
+ ensureGreen (restoredIndexName );
154
+
155
+ final Request openRequest = new Request (HttpPost .METHOD_NAME , restoredIndexName + "/_open" );
156
+ assertOK (client ().performRequest (openRequest ));
157
+ ensureGreen (restoredIndexName );
158
+
159
+ for (int i = 0 ; i < 10 ; i ++) {
160
+ assertSearchResults (restoredIndexName , numDocs , randomFrom (Boolean .TRUE , Boolean .FALSE , null ));
161
+ }
162
+ });
163
+ }
164
+
165
+ public void assertSearchResults (String indexName , int numDocs , Boolean ignoreThrottled ) throws IOException {
166
+ final int randomTieBreaker = randomIntBetween (1 , numDocs - 1 );
167
+ Map <String , Object > searchResults ;
168
+ switch (randomInt (3 )) {
169
+ case 0 :
170
+ searchResults
171
+ = search (indexName , QueryBuilders .termQuery ("field" , String .valueOf (randomTieBreaker )), ignoreThrottled );
172
+ assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (1 ));
173
+ @ SuppressWarnings ("unchecked" )
174
+ Map <String , Object > searchHit = (Map <String , Object >) ((List <?>) extractValue (searchResults , "hits.hits" )).get (0 );
175
+ assertThat (extractValue (searchHit , "_index" ), equalTo (indexName ));
176
+ assertThat (extractValue (searchHit , "_source.field" ), equalTo (randomTieBreaker ));
177
+ break ;
178
+ case 1 :
179
+ searchResults = search (indexName , QueryBuilders .rangeQuery ("field" ).lt (randomTieBreaker ), ignoreThrottled );
180
+ assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (randomTieBreaker ));
181
+ break ;
182
+ case 2 :
183
+ searchResults = search (indexName , QueryBuilders .rangeQuery ("field" ).gte (randomTieBreaker ), ignoreThrottled );
184
+ assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (numDocs - randomTieBreaker ));
185
+ break ;
186
+ case 3 :
187
+ searchResults = search (indexName , QueryBuilders .matchQuery ("text" , "document" ), ignoreThrottled );
188
+ assertThat (extractValue (searchResults , "hits.total.value" ), equalTo (numDocs ));
189
+ break ;
190
+ default :
191
+ fail ("Unsupported randomized search query" );
192
+ }
193
+ }
194
+
157
195
protected static void registerRepository (String repository , String type , boolean verify , Settings settings ) throws IOException {
158
196
final Request request = new Request (HttpPut .METHOD_NAME , "_snapshot/" + repository );
159
197
request .setJsonEntity (Strings .toString (new PutRepositoryRequest (repository ).type (type ).verify (verify ).settings (settings )));
@@ -218,9 +256,12 @@ protected static Number count(String index) throws IOException {
218
256
return (Number ) extractValue (responseAsMap , "count" );
219
257
}
220
258
221
- protected static Map <String , Object > search (String index , QueryBuilder query ) throws IOException {
259
+ protected static Map <String , Object > search (String index , QueryBuilder query , Boolean ignoreThrottled ) throws IOException {
222
260
final Request request = new Request (HttpPost .METHOD_NAME , '/' + index + "/_search" );
223
261
request .setJsonEntity (new SearchSourceBuilder ().trackTotalHits (true ).query (query ).toString ());
262
+ if (ignoreThrottled != null ) {
263
+ request .addParameter ("ignore_throttled" , ignoreThrottled .toString ());
264
+ }
224
265
225
266
final Response response = client ().performRequest (request );
226
267
assertThat ("Failed to execute search request on index [" + index + "]: " + response ,
@@ -260,4 +301,12 @@ protected static Map<String, Object> responseAsMap(Response response) throws IOE
260
301
protected static <T > T extractValue (Map <String , Object > map , String path ) {
261
302
return (T ) XContentMapValues .extractValue (path , map );
262
303
}
304
+
305
+ /**
306
+ * The body of a test case, which runs after the searchable snapshot has been created and restored.
307
+ */
308
+ @ FunctionalInterface
309
+ interface SearchableSnapshotsTestCaseBody {
310
+ void runTest (String indexName , int numDocs ) throws IOException ;
311
+ }
263
312
}
0 commit comments