@@ -148,6 +148,7 @@ public class TransportSearchAction extends HandledTransportAction<SearchRequest,
148148 Property .NodeScope
149149 );
150150
151+ // Marker to indicate this index's shards should be skipped in a search
151152 private static final OriginalIndices SKIPPED_INDICES = new OriginalIndices (Strings .EMPTY_ARRAY , IndicesOptions .strictExpandOpen ());
152153
153154 private final ThreadPool threadPool ;
@@ -594,7 +595,7 @@ public void onFailure(Exception e) {}
594595 );
595596 }
596597
597- static void adjustSearchType (SearchRequest searchRequest , boolean oneOrZeroValidShards ) {
598+ static void adjustSearchType (SearchRequest searchRequest , boolean oneOrZeroShardsToSearch ) {
598599 // if there's a kNN search, always use DFS_QUERY_THEN_FETCH
599600 if (searchRequest .hasKnnSearch ()) {
600601 searchRequest .searchType (DFS_QUERY_THEN_FETCH );
@@ -609,7 +610,7 @@ static void adjustSearchType(SearchRequest searchRequest, boolean oneOrZeroValid
609610 }
610611
611612 // optimize search type for cases where there is only one shard group to search on
612- if (oneOrZeroValidShards ) {
613+ if (oneOrZeroShardsToSearch ) {
613614 // if we only have one group, then we always want Q_T_F, no need for DFS, and no need to do THEN since we hit one shard
614615 searchRequest .searchType (QUERY_THEN_FETCH );
615616 }
@@ -1310,8 +1311,7 @@ private void executeSearch(
13101311
13111312 Map <String , Float > concreteIndexBoosts = resolveIndexBoosts (searchRequest , projectState .cluster ());
13121313
1313- boolean oneOrZeroValidShards = shardIterators .size () == 1 || allOrAllButOneSkipped (shardIterators );
1314- adjustSearchType (searchRequest , oneOrZeroValidShards );
1314+ adjustSearchType (searchRequest , oneOrZeroShardsToSearch (shardIterators ));
13151315
13161316 final DiscoveryNodes nodes = projectState .cluster ().nodes ();
13171317 BiFunction <String , String , Transport .Connection > connectionLookup = buildConnectionLookup (
@@ -1345,17 +1345,21 @@ private void executeSearch(
13451345 }
13461346
13471347 /**
1348- * Determines if all, or all but one, iterators are skipped .
1348+ * Determines if only one ( or zero) search shard iterators will be searched .
13491349 * (At this point, iterators may be marked as skipped due to index level blockers).
1350- * We expect skipped iteators to be unlikely, so returning fast after we see more
1350+ * We expect skipped iterators to be unlikely, so returning fast after we see more
13511351 * than one "not skipped" is an intended optimization.
13521352 *
13531353 * @param searchShardIterators all the shard iterators derived from indices being searched
1354- * @return true if all of them are already skipped, or only one is not skipped
1354+ * @return true if there are no more than one shard iterators, or if there are no more than
1355+ * one not marked to skip
13551356 */
1356- private boolean allOrAllButOneSkipped (List <SearchShardIterator > searchShardIterators ) {
1357- int notSkippedCount = 0 ;
1357+ private boolean oneOrZeroShardsToSearch (List <SearchShardIterator > searchShardIterators ) {
1358+ if (searchShardIterators .size () <= 1 ) {
1359+ return true ;
1360+ }
13581361
1362+ int notSkippedCount = 0 ;
13591363 for (SearchShardIterator searchShardIterator : searchShardIterators ) {
13601364 if (searchShardIterator .skip () == false ) {
13611365 notSkippedCount ++;
@@ -1364,7 +1368,6 @@ private boolean allOrAllButOneSkipped(List<SearchShardIterator> searchShardItera
13641368 }
13651369 }
13661370 }
1367-
13681371 return true ;
13691372 }
13701373
0 commit comments