34
34
import org .elasticsearch .cluster .metadata .MappingMetadata ;
35
35
import org .elasticsearch .cluster .routing .Murmur3HashFunction ;
36
36
import org .elasticsearch .common .Strings ;
37
+ import org .elasticsearch .common .document .DocumentField ;
37
38
import org .elasticsearch .common .settings .SecureString ;
38
39
import org .elasticsearch .common .settings .Settings ;
39
40
import org .elasticsearch .common .util .concurrent .ThreadContext ;
@@ -136,7 +137,7 @@ && randomBoolean()) {
136
137
String id = "testdoc" + i ;
137
138
expectedIds .add (id );
138
139
// 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
+ String type = getType (oldVersion , id );
140
141
Request doc = new Request ("PUT" , "/test/" + type + "/" + id );
141
142
doc .addParameter ("refresh" , "true" );
142
143
doc .setJsonEntity (sourceForDoc (i ));
@@ -146,7 +147,7 @@ && randomBoolean()) {
146
147
for (int i = 0 ; i < extraDocs ; i ++) {
147
148
String id = randomFrom (expectedIds );
148
149
expectedIds .remove (id );
149
- String type = "doc" + (oldVersion . before ( Version . fromString ( "6.0.0" )) ? Murmur3HashFunction . hash ( id ) % 2 : 0 );
150
+ String type = getType (oldVersion , id );
150
151
Request doc = new Request ("DELETE" , "/test/" + type + "/" + id );
151
152
doc .addParameter ("refresh" , "true" );
152
153
oldEs .performRequest (doc );
@@ -267,6 +268,10 @@ && randomBoolean()) {
267
268
}
268
269
}
269
270
271
+ private String getType (Version oldVersion , String id ) {
272
+ return "doc" + (oldVersion .before (Version .fromString ("6.0.0" )) ? Math .abs (Murmur3HashFunction .hash (id ) % 2 ) : 0 );
273
+ }
274
+
270
275
private static String sourceForDoc (int i ) {
271
276
return "{\" test\" :\" test" + i + "\" ,\" val\" :" + i + "}" ;
272
277
}
@@ -337,7 +342,7 @@ private void restoreMountAndVerify(
337
342
}
338
343
339
344
// run a search against the index
340
- assertDocs ("restored_test" , numDocs , expectedIds , client , sourceOnlyRepository );
345
+ assertDocs ("restored_test" , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion );
341
346
342
347
// mount as full copy searchable snapshot
343
348
RestoreSnapshotResponse mountSnapshotResponse = client .searchableSnapshots ()
@@ -363,7 +368,7 @@ private void restoreMountAndVerify(
363
368
);
364
369
365
370
// run a search against the index
366
- assertDocs ("mounted_full_copy_test" , numDocs , expectedIds , client , sourceOnlyRepository );
371
+ assertDocs ("mounted_full_copy_test" , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion );
367
372
368
373
// mount as shared cache searchable snapshot
369
374
mountSnapshotResponse = client .searchableSnapshots ()
@@ -378,12 +383,18 @@ private void restoreMountAndVerify(
378
383
assertEquals (numberOfShards , mountSnapshotResponse .getRestoreInfo ().successfulShards ());
379
384
380
385
// run a search against the index
381
- assertDocs ("mounted_shared_cache_test" , numDocs , expectedIds , client , sourceOnlyRepository );
386
+ assertDocs ("mounted_shared_cache_test" , numDocs , expectedIds , client , sourceOnlyRepository , oldVersion );
382
387
}
383
388
384
389
@ SuppressWarnings ("removal" )
385
- private void assertDocs (String index , int numDocs , Set <String > expectedIds , RestHighLevelClient client , boolean sourceOnlyRepository )
386
- throws IOException {
390
+ private void assertDocs (
391
+ String index ,
392
+ int numDocs ,
393
+ Set <String > expectedIds ,
394
+ RestHighLevelClient client ,
395
+ boolean sourceOnlyRepository ,
396
+ Version oldVersion
397
+ ) throws IOException {
387
398
// run a search against the index
388
399
SearchResponse searchResponse = client .search (new SearchRequest (index ), RequestOptions .DEFAULT );
389
400
logger .info (searchResponse );
@@ -420,9 +431,9 @@ private void assertDocs(String index, int numDocs, Set<String> expectedIds, Rest
420
431
// check that doc values can be accessed by (reverse) sorting on numeric val field
421
432
// first add mapping for field (this will be done automatically in the future)
422
433
XContentBuilder mappingBuilder = JsonXContent .contentBuilder ();
423
- mappingBuilder .startObject ().startObject ("properties" ). startObject ( "val" ) ;
424
- mappingBuilder .field ("type" , "long" );
425
- mappingBuilder .endObject ().endObject (). endObject () ;
434
+ mappingBuilder .startObject ().startObject ("properties" );
435
+ mappingBuilder .startObject ( "val" ). field ("type" , "long" ). endObject ( );
436
+ mappingBuilder .endObject ().endObject ();
426
437
assertTrue (
427
438
client .indices ().putMapping (new PutMappingRequest (index ).source (mappingBuilder ), RequestOptions .DEFAULT ).isAcknowledged ()
428
439
);
@@ -442,6 +453,24 @@ private void assertDocs(String index, int numDocs, Set<String> expectedIds, Rest
442
453
expectedIds .stream ().sorted (Comparator .comparingInt (this ::getIdAsNumeric ).reversed ()).collect (Collectors .toList ()),
443
454
Arrays .stream (searchResponse .getHits ().getHits ()).map (SearchHit ::getId ).collect (Collectors .toList ())
444
455
);
456
+
457
+ if (oldVersion .before (Version .fromString ("6.0.0" ))) {
458
+ // search on _type and check that results contain _type information
459
+ String randomType = getType (oldVersion , randomFrom (expectedIds ));
460
+ long typeCount = expectedIds .stream ().filter (idd -> getType (oldVersion , idd ).equals (randomType )).count ();
461
+ searchResponse = client .search (
462
+ new SearchRequest (index ).source (SearchSourceBuilder .searchSource ().query (QueryBuilders .termQuery ("_type" , randomType ))),
463
+ RequestOptions .DEFAULT
464
+ );
465
+ logger .info (searchResponse );
466
+ assertEquals (typeCount , searchResponse .getHits ().getTotalHits ().value );
467
+ for (SearchHit hit : searchResponse .getHits ().getHits ()) {
468
+ DocumentField typeField = hit .field ("_type" );
469
+ assertNotNull (typeField );
470
+ assertThat (typeField .getValue (), instanceOf (String .class ));
471
+ assertEquals (randomType , typeField .getValue ());
472
+ }
473
+ }
445
474
}
446
475
}
447
476
0 commit comments