@@ -1338,9 +1338,6 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, IndexReque
13381338 indexRandom (forceRefresh , dummyDocuments , Arrays .asList (builders ));
13391339 }
13401340
1341-
1342- private static final String RANDOM_BOGUS_TYPE = "RANDOM_BOGUS_TYPE______" ;
1343-
13441341 /**
13451342 * Indexes the given {@link IndexRequestBuilder} instances randomly. It shuffles the given builders and either
13461343 * indexes them in a blocking or async fashion. This is very useful to catch problems that relate to internal document
@@ -1388,31 +1385,33 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, List<Index
13881385 * @param builders the documents to index.
13891386 */
13901387 public void indexRandom (boolean forceRefresh , boolean dummyDocuments , boolean maybeFlush , List <IndexRequestBuilder > builders ) throws InterruptedException , ExecutionException {
1391-
13921388 Random random = random ();
1393- Set <String > indicesSet = new HashSet <>();
1389+ Map < String , Set <String >> indicesAndTypes = new HashMap <>();
13941390 for (IndexRequestBuilder builder : builders ) {
1395- indicesSet .add (builder .request ().index ());
1391+ final Set <String > types = indicesAndTypes .computeIfAbsent (builder .request ().index (), index -> new HashSet <>());
1392+ types .add (builder .request ().type ());
13961393 }
1397- Set <Tuple <String , String >> bogusIds = new HashSet <>();
1394+ Set <List <String >> bogusIds = new HashSet <>(); // (index, type, id)
13981395 if (random .nextBoolean () && !builders .isEmpty () && dummyDocuments ) {
13991396 builders = new ArrayList <>(builders );
1400- final String [] indices = indicesSet .toArray (new String [indicesSet .size ()]);
14011397 // inject some bogus docs
14021398 final int numBogusDocs = scaledRandomIntBetween (1 , builders .size () * 2 );
14031399 final int unicodeLen = between (1 , 10 );
14041400 for (int i = 0 ; i < numBogusDocs ; i ++) {
1405- String id = randomRealisticUnicodeOfLength (unicodeLen ) + Integer .toString (dummmyDocIdGenerator .incrementAndGet ());
1406- String index = RandomPicks .randomFrom (random , indices );
1407- bogusIds .add (new Tuple <>(index , id ));
1408- builders .add (client ().prepareIndex (index , RANDOM_BOGUS_TYPE , id ).setSource ("{}" , XContentType .JSON ));
1401+ String id = "bogus_doc_" + randomRealisticUnicodeOfLength (unicodeLen ) + Integer .toString (dummmyDocIdGenerator .incrementAndGet ());
1402+ Map .Entry <String , Set <String >> indexAndTypes = RandomPicks .randomFrom (random , indicesAndTypes .entrySet ());
1403+ String index = indexAndTypes .getKey ();
1404+ String type = RandomPicks .randomFrom (random , indexAndTypes .getValue ());
1405+ bogusIds .add (Arrays .asList (index , type , id ));
1406+ // We configure a routing key in case the mapping requires it
1407+ builders .add (client ().prepareIndex (index , type , id ).setSource ("{}" , XContentType .JSON ).setRouting (id ));
14091408 }
14101409 }
1411- final String [] indices = indicesSet .toArray (new String [indicesSet .size ()]);
14121410 Collections .shuffle (builders , random ());
14131411 final CopyOnWriteArrayList <Tuple <IndexRequestBuilder , Exception >> errors = new CopyOnWriteArrayList <>();
14141412 List <CountDownLatch > inFlightAsyncOperations = new ArrayList <>();
14151413 // If you are indexing just a few documents then frequently do it one at a time. If many then frequently in bulk.
1414+ final String [] indices = indicesAndTypes .keySet ().toArray (new String [0 ]);
14161415 if (builders .size () < FREQUENT_BULK_THRESHOLD ? frequently () : builders .size () < ALWAYS_BULK_THRESHOLD ? rarely () : false ) {
14171416 if (frequently ()) {
14181417 logger .info ("Index [{}] docs async: [{}] bulk: [{}]" , builders .size (), true , false );
@@ -1454,10 +1453,10 @@ public void indexRandom(boolean forceRefresh, boolean dummyDocuments, boolean ma
14541453 assertThat (actualErrors , emptyIterable ());
14551454 if (!bogusIds .isEmpty ()) {
14561455 // delete the bogus types again - it might trigger merges or at least holes in the segments and enforces deleted docs!
1457- for (Tuple < String , String > doc : bogusIds ) {
1458- assertEquals ("failed to delete a dummy doc [" + doc .v1 ( ) + "][" + doc .v2 ( ) + "]" ,
1456+ for (List < String > doc : bogusIds ) {
1457+ assertEquals ("failed to delete a dummy doc [" + doc .get ( 0 ) + "][" + doc .get ( 2 ) + "]" ,
14591458 DocWriteResponse .Result .DELETED ,
1460- client ().prepareDelete (doc .v1 ( ), RANDOM_BOGUS_TYPE , doc .v2 ( )).get ().getResult ());
1459+ client ().prepareDelete (doc .get ( 0 ), doc . get ( 1 ) , doc .get ( 2 )). setRouting ( doc . get ( 2 )).get ().getResult ());
14611460 }
14621461 }
14631462 if (forceRefresh ) {
0 commit comments