2626import org .elasticsearch .action .admin .cluster .node .stats .NodeStats ;
2727import org .elasticsearch .action .admin .cluster .node .stats .NodesStatsResponse ;
2828import org .elasticsearch .action .admin .indices .stats .IndexStats ;
29+ import org .elasticsearch .action .get .MultiGetRequest ;
2930import org .elasticsearch .action .index .IndexRequest ;
3031import org .elasticsearch .action .index .IndexResponse ;
3132import org .elasticsearch .action .search .SearchRequest ;
109110import java .util .concurrent .atomic .AtomicInteger ;
110111import java .util .concurrent .atomic .AtomicLong ;
111112import java .util .concurrent .atomic .AtomicReference ;
113+ import java .util .function .IntToLongFunction ;
112114import java .util .function .Predicate ;
113115import java .util .stream .Stream ;
114116
@@ -688,7 +690,23 @@ private static ShardRouting getInitializingShardRouting(ShardRouting existingSha
688690 return shardRouting ;
689691 }
690692
691- public void testAutomaticRefresh () throws InterruptedException {
693+ public void testAutomaticRefreshSearch () throws InterruptedException {
694+ runTestAutomaticRefresh (numDocs -> client ().prepareSearch ("test" ).get ().getHits ().getTotalHits ().value );
695+ }
696+
697+ public void testAutomaticRefreshMultiGet () throws InterruptedException {
698+ runTestAutomaticRefresh (
699+ numDocs -> {
700+ final MultiGetRequest request = new MultiGetRequest ();
701+ request .realtime (false );
702+ for (int i = 0 ; i < numDocs ; i ++) {
703+ request .add ("test" , "" + i );
704+ }
705+ return Arrays .stream (client ().multiGet (request ).actionGet ().getResponses ()).filter (r -> r .getResponse ().isExists ()).count ();
706+ });
707+ }
708+
709+ private void runTestAutomaticRefresh (final IntToLongFunction count ) throws InterruptedException {
692710 TimeValue randomTimeValue = randomFrom (random (), null , TimeValue .ZERO , TimeValue .timeValueMillis (randomIntBetween (0 , 1000 )));
693711 Settings .Builder builder = Settings .builder ();
694712 if (randomTimeValue != null ) {
@@ -721,31 +739,31 @@ public void testAutomaticRefresh() throws InterruptedException {
721739 ensureNoPendingScheduledRefresh (indexService .getThreadPool ());
722740 }
723741 }
742+
724743 CountDownLatch started = new CountDownLatch (1 );
725744 Thread t = new Thread (() -> {
726- SearchResponse searchResponse ;
727745 started .countDown ();
728746 do {
729- searchResponse = client (). prepareSearch (). get ();
730- } while (searchResponse . getHits (). getTotalHits (). value != totalNumDocs .get ());
747+
748+ } while (count . applyAsLong ( totalNumDocs . get ()) != totalNumDocs .get ());
731749 });
732750 t .start ();
733751 started .await ();
734- assertHitCount ( client (). prepareSearch () .get (), 1 );
752+ assertThat ( count . applyAsLong ( totalNumDocs .get ()), equalTo ( 1L ) );
735753 for (int i = 1 ; i < numDocs ; i ++) {
736754 client ().prepareIndex ("test" , "test" , "" + i ).setSource ("{\" foo\" : \" bar\" }" , XContentType .JSON )
737755 .execute (new ActionListener <IndexResponse >() {
738- @ Override
739- public void onResponse (IndexResponse indexResponse ) {
740- indexingDone .countDown ();
741- }
742-
743- @ Override
744- public void onFailure (Exception e ) {
745- indexingDone .countDown ();
746- throw new AssertionError (e );
747- }
748- });
756+ @ Override
757+ public void onResponse (IndexResponse indexResponse ) {
758+ indexingDone .countDown ();
759+ }
760+
761+ @ Override
762+ public void onFailure (Exception e ) {
763+ indexingDone .countDown ();
764+ throw new AssertionError (e );
765+ }
766+ });
749767 }
750768 indexingDone .await ();
751769 t .join ();
@@ -757,7 +775,6 @@ public void testPendingRefreshWithIntervalChange() throws Exception {
757775 IndexService indexService = createIndex ("test" , builder .build ());
758776 assertFalse (indexService .getIndexSettings ().isExplicitRefresh ());
759777 ensureGreen ();
760- assertNoSearchHits (client ().prepareSearch ().get ());
761778 client ().prepareIndex ("test" , "test" , "0" ).setSource ("{\" foo\" : \" bar\" }" , XContentType .JSON ).get ();
762779 IndexShard shard = indexService .getShard (0 );
763780 assertFalse (shard .scheduledRefresh ());
0 commit comments