Skip to content

Commit 5b2028b

Browse files
missing optimization
1 parent c432cd8 commit 5b2028b

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

server/src/main/java/org/elasticsearch/action/search/SearchQueryThenFetchAsyncAction.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public class SearchQueryThenFetchAsyncAction<Result extends SearchPhaseResult> e
126126
executor,
127127
nodeIdToConnection,
128128
shardsIts,
129-
aliasFilter,
129+
removeEmptyAliasFilters(aliasFilter), // TODO: we should do this at build time for this map, but BwC is tricky for now
130+
// this is an important optimization for batched execution though because it keeps
131+
// the transport request size in check by avoiding redundant index name/uuid strings
130132
concreteIndexBoosts,
131133
timeProvider,
132134
clusterState,
@@ -152,6 +154,10 @@ public final void start() {
152154
return;
153155
}
154156
try {
157+
if (shardsIts.isEmpty()) {
158+
executeNextPhase(NAME, this::getNextPhase);
159+
return;
160+
}
155161
run();
156162
} catch (Exception e) {
157163
if (logger.isDebugEnabled()) {
@@ -267,8 +273,8 @@ public static class NodeQueryResponse extends TransportResponse {
267273
SearchPhaseController.TopDocsStats topDocsStats
268274
) {
269275
this.results = results;
270-
for (int i = 0; i < results.length; i++) {
271-
if (results[i] instanceof RefCounted r) {
276+
for (Object result : results) {
277+
if (result instanceof RefCounted r) {
272278
r.incRef();
273279
}
274280
}
@@ -479,10 +485,6 @@ private static ShardSearchRequest rewriteShardSearchRequest(
479485
}
480486

481487
private void run() {
482-
if (shardsIts.size() == 0) {
483-
executeNextPhase(NAME, this::getNextPhase);
484-
return;
485-
}
486488
// TODO: stupid but we kinda need to fill all of these in with the current logic, do something nicer before merging
487489
final Map<SearchShardIterator, Integer> shardIndexMap = Maps.newHashMapWithExpectedSize(shardIterators.length);
488490
for (int i = 0; i < shardIterators.length; i++) {
@@ -594,6 +596,16 @@ public void handleException(TransportException e) {
594596
});
595597
}
596598

599+
private static Map<String, AliasFilter> removeEmptyAliasFilters(Map<String, AliasFilter> aliasFilters) {
600+
Map<String, AliasFilter> aliasFilterNoEmpty = new HashMap<>();
601+
aliasFilters.forEach((idx, filter) -> {
602+
if (AliasFilter.EMPTY.equals(filter) == false) {
603+
aliasFilterNoEmpty.put(idx, filter);
604+
}
605+
});
606+
return Map.copyOf(aliasFilterNoEmpty);
607+
}
608+
597609
private void onNodeQueryFailure(Exception e, NodeQueryRequest request, String nodeId) {
598610
for (ShardToQuery shard : request.shards) {
599611
int idx = shard.shardIndex;

0 commit comments

Comments
 (0)