25
25
import org .elasticsearch .client .RestClient ;
26
26
import org .elasticsearch .client .RestHighLevelClient ;
27
27
import org .elasticsearch .client .indices .CloseIndexRequest ;
28
+ import org .elasticsearch .client .indices .GetMappingsRequest ;
28
29
import org .elasticsearch .client .indices .PutMappingRequest ;
29
30
import org .elasticsearch .client .searchable_snapshots .MountSnapshotRequest ;
30
31
import org .elasticsearch .cluster .SnapshotsInProgress ;
31
32
import org .elasticsearch .cluster .health .ClusterHealthStatus ;
32
33
import org .elasticsearch .cluster .metadata .IndexMetadata ;
34
+ import org .elasticsearch .cluster .metadata .MappingMetadata ;
35
+ import org .elasticsearch .cluster .routing .Murmur3HashFunction ;
33
36
import org .elasticsearch .common .Strings ;
34
37
import org .elasticsearch .common .settings .SecureString ;
35
38
import org .elasticsearch .common .settings .Settings ;
59
62
import java .util .Set ;
60
63
import java .util .stream .Collectors ;
61
64
65
+ import static org .hamcrest .Matchers .empty ;
62
66
import static org .hamcrest .Matchers .greaterThan ;
67
+ import static org .hamcrest .Matchers .hasKey ;
63
68
import static org .hamcrest .Matchers .hasSize ;
69
+ import static org .hamcrest .Matchers .instanceOf ;
70
+ import static org .hamcrest .Matchers .not ;
71
+ import static org .hamcrest .Matchers .startsWith ;
64
72
65
73
public class OldRepositoryAccessIT extends ESRestTestCase {
66
74
@ Override
@@ -127,7 +135,9 @@ && randomBoolean()) {
127
135
for (int i = 0 ; i < numDocs + extraDocs ; i ++) {
128
136
String id = "testdoc" + i ;
129
137
expectedIds .add (id );
130
- Request doc = new Request ("PUT" , "/test/doc/" + id );
138
+ // use multiple types for ES versions < 6.0.0
139
+ String type = "doc" + (oldVersion .before (Version .fromString ("6.0.0" )) ? Murmur3HashFunction .hash (id ) % 2 : 0 );
140
+ Request doc = new Request ("PUT" , "/test/" + type + "/" + id );
131
141
doc .addParameter ("refresh" , "true" );
132
142
doc .setJsonEntity (sourceForDoc (i ));
133
143
assertOK (oldEs .performRequest (doc ));
@@ -136,7 +146,8 @@ && randomBoolean()) {
136
146
for (int i = 0 ; i < extraDocs ; i ++) {
137
147
String id = randomFrom (expectedIds );
138
148
expectedIds .remove (id );
139
- Request doc = new Request ("DELETE" , "/test/doc/" + id );
149
+ String type = "doc" + (oldVersion .before (Version .fromString ("6.0.0" )) ? Murmur3HashFunction .hash (id ) % 2 : 0 );
150
+ Request doc = new Request ("DELETE" , "/test/" + type + "/" + id );
140
151
doc .addParameter ("refresh" , "true" );
141
152
oldEs .performRequest (doc );
142
153
}
@@ -218,7 +229,7 @@ && randomBoolean()) {
218
229
219
230
if (Build .CURRENT .isSnapshot ()) {
220
231
// restore / mount and check whether searches work
221
- restoreMountAndVerify (numDocs , expectedIds , client , numberOfShards , sourceOnlyRepository );
232
+ restoreMountAndVerify (numDocs , expectedIds , client , numberOfShards , sourceOnlyRepository , oldVersion );
222
233
223
234
// close indices
224
235
assertTrue (
@@ -236,7 +247,7 @@ && randomBoolean()) {
236
247
);
237
248
238
249
// restore / mount again
239
- restoreMountAndVerify (numDocs , expectedIds , client , numberOfShards , sourceOnlyRepository );
250
+ restoreMountAndVerify (numDocs , expectedIds , client , numberOfShards , sourceOnlyRepository , oldVersion );
240
251
}
241
252
} finally {
242
253
IOUtils .closeWhileHandlingException (
@@ -266,7 +277,8 @@ private void restoreMountAndVerify(
266
277
Set <String > expectedIds ,
267
278
RestHighLevelClient client ,
268
279
int numberOfShards ,
269
- boolean sourceOnlyRepository
280
+ boolean sourceOnlyRepository ,
281
+ Version oldVersion
270
282
) throws IOException {
271
283
// restore index
272
284
RestoreSnapshotResponse restoreSnapshotResponse = client .snapshot ()
@@ -291,6 +303,39 @@ private void restoreMountAndVerify(
291
303
.getStatus ()
292
304
);
293
305
306
+ MappingMetadata mapping = client .indices ()
307
+ .getMapping (new GetMappingsRequest ().indices ("restored_test" ), RequestOptions .DEFAULT )
308
+ .mappings ()
309
+ .get ("restored_test" );
310
+ logger .info ("mapping for {}: {}" , mapping .type (), mapping .source ().string ());
311
+ Map <String , Object > root = mapping .sourceAsMap ();
312
+ assertThat (root , hasKey ("_meta" ));
313
+ assertThat (root .get ("_meta" ), instanceOf (Map .class ));
314
+ @ SuppressWarnings ("unchecked" )
315
+ Map <String , Object > meta = (Map <String , Object >) root .get ("_meta" );
316
+ assertThat (meta , hasKey ("legacy_mappings" ));
317
+ assertThat (meta .get ("legacy_mappings" ), instanceOf (Map .class ));
318
+ @ SuppressWarnings ("unchecked" )
319
+ Map <String , Object > legacyMappings = (Map <String , Object >) meta .get ("legacy_mappings" );
320
+ assertThat (legacyMappings .keySet (), not (empty ()));
321
+ for (Map .Entry <String , Object > entry : legacyMappings .entrySet ()) {
322
+ String type = entry .getKey ();
323
+ assertThat (type , startsWith ("doc" ));
324
+ assertThat (entry .getValue (), instanceOf (Map .class ));
325
+ @ SuppressWarnings ("unchecked" )
326
+ Map <String , Object > legacyMapping = (Map <String , Object >) entry .getValue ();
327
+ assertThat (legacyMapping , hasKey ("properties" ));
328
+ assertThat (legacyMapping .get ("properties" ), instanceOf (Map .class ));
329
+ @ SuppressWarnings ("unchecked" )
330
+ Map <String , Object > propertiesMapping = (Map <String , Object >) legacyMapping .get ("properties" );
331
+ assertThat (propertiesMapping , hasKey ("val" ));
332
+ assertThat (propertiesMapping .get ("val" ), instanceOf (Map .class ));
333
+ @ SuppressWarnings ("unchecked" )
334
+ Map <String , Object > valMapping = (Map <String , Object >) propertiesMapping .get ("val" );
335
+ assertThat (valMapping , hasKey ("type" ));
336
+ assertEquals ("long" , valMapping .get ("type" ));
337
+ }
338
+
294
339
// run a search against the index
295
340
assertDocs ("restored_test" , numDocs , expectedIds , client , sourceOnlyRepository );
296
341
0 commit comments