Skip to content

Commit 853b755

Browse files
fix test
1 parent 4a132d2 commit 853b755

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
import org.elasticsearch.search.CanMatchShardResponse;
2323
import org.elasticsearch.search.SearchService;
2424
import org.elasticsearch.search.SearchShardTarget;
25+
import org.elasticsearch.search.builder.SearchSourceBuilder;
2526
import org.elasticsearch.search.internal.AliasFilter;
2627
import org.elasticsearch.search.internal.ShardSearchRequest;
2728
import org.elasticsearch.search.sort.FieldSortBuilder;
2829
import org.elasticsearch.search.sort.MinAndMax;
29-
import org.elasticsearch.search.sort.SortOrder;
3030
import org.elasticsearch.threadpool.ThreadPool;
3131
import org.elasticsearch.transport.Transport;
3232

@@ -474,21 +474,23 @@ private synchronized List<SearchShardIterator> getIterator(List<SearchShardItera
474474
if (shouldSortShards(minAndMaxes) == false) {
475475
return shardsIts;
476476
}
477-
int[] indexTranslation = sortShards(shardsIts, minAndMaxes, FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()).order());
477+
int[] indexTranslation = sortShards(shardsIts, minAndMaxes, request.source());
478478
List<SearchShardIterator> list = new ArrayList<>(indexTranslation.length);
479479
for (int in : indexTranslation) {
480480
list.add(shardsIts.get(in));
481481
}
482482
return list;
483483
}
484484

485-
public static <T extends Comparable<T>> int[] sortShards(List<T> shardsIts, MinAndMax<?>[] minAndMaxes, SortOrder order) {
485+
public static <T extends Comparable<T>> int[] sortShards(List<T> shardsIts, MinAndMax<?>[] minAndMaxes, SearchSourceBuilder source) {
486486
int bound = shardsIts.size();
487487
List<Integer> toSort = new ArrayList<>(bound);
488488
for (int i = 0; i < bound; i++) {
489489
toSort.add(i);
490490
}
491-
Comparator<? super MinAndMax<?>> keyComparator = forciblyCast(MinAndMax.getComparator(order));
491+
Comparator<? super MinAndMax<?>> keyComparator = forciblyCast(
492+
MinAndMax.getComparator(FieldSortBuilder.getPrimaryFieldSortOrNull(source).order())
493+
);
492494
toSort.sort((idx1, idx2) -> {
493495
int res = keyComparator.compare(minAndMaxes[idx1], minAndMaxes[idx2]);
494496
if (res != 0) {

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import org.elasticsearch.search.internal.ShardSearchContextId;
4949
import org.elasticsearch.search.internal.ShardSearchRequest;
5050
import org.elasticsearch.search.query.QuerySearchResult;
51-
import org.elasticsearch.search.sort.FieldSortBuilder;
5251
import org.elasticsearch.search.sort.MinAndMax;
5352
import org.elasticsearch.tasks.CancellableTask;
5453
import org.elasticsearch.tasks.Task;
@@ -480,11 +479,7 @@ protected void doRun() {
480479
}
481480

482481
try {
483-
final int[] indexes = CanMatchPreFilterSearchPhase.sortShards(
484-
shards,
485-
minAndMax,
486-
FieldSortBuilder.getPrimaryFieldSortOrNull(request.source()).order()
487-
);
482+
final int[] indexes = CanMatchPreFilterSearchPhase.sortShards(shards, minAndMax, request.source());
488483
final ShardToQuery[] orig = shards.toArray(new ShardToQuery[0]);
489484
for (int i = 0; i < indexes.length; i++) {
490485
shards.set(i, orig[indexes[i]]);
@@ -652,31 +647,29 @@ static void registerNodeSearchAction(
652647
NodeQueryRequest::new,
653648
(request, channel, task) -> {
654649
final SearchRequest searchRequest = request.searchRequest;
655-
final IntUnaryOperator shards;
656-
final ShardSearchRequest[] shardSearchRequests;
650+
ShardSearchRequest[] shardSearchRequests = null;
651+
IntUnaryOperator shards = IntUnaryOperator.identity();
657652
final int shardCount = request.shards.size();
658653
if (shardCount > 1 && hasPrimaryFieldSort(searchRequest.source())) {
659-
shardSearchRequests = new ShardSearchRequest[shardCount];
660-
@SuppressWarnings("rawtypes")
661-
final MinAndMax[] minAndMax = new MinAndMax[shardCount];
662-
for (int i = 0; i < minAndMax.length; i++) {
663-
ShardSearchRequest r = buildShardSearchRequestForLocal(request, request.shards.get(i));
664-
shardSearchRequests[i] = r;
665-
var canMatch = searchService.canMatch(r);
666-
if (canMatch.canMatch()) {
667-
r.setRunCanMatchInQueryPhase(false);
668-
minAndMax[i] = canMatch.estimatedMinAndMax();
654+
try {
655+
shardSearchRequests = new ShardSearchRequest[shardCount];
656+
@SuppressWarnings("rawtypes")
657+
final MinAndMax[] minAndMax = new MinAndMax[shardCount];
658+
for (int i = 0; i < minAndMax.length; i++) {
659+
ShardSearchRequest r = buildShardSearchRequestForLocal(request, request.shards.get(i));
660+
shardSearchRequests[i] = r;
661+
var canMatch = searchService.canMatch(r);
662+
if (canMatch.canMatch()) {
663+
r.setRunCanMatchInQueryPhase(false);
664+
minAndMax[i] = canMatch.estimatedMinAndMax();
665+
}
669666
}
667+
int[] indexes = CanMatchPreFilterSearchPhase.sortShards(request.shards, minAndMax, searchRequest.source());
668+
shards = pos -> indexes[pos];
669+
} catch (Exception e) {
670+
// TODO: ignored for now but we'll be guaranteed to fail the query phase at this point, fix things to fail here
671+
// already
670672
}
671-
int[] indexes = CanMatchPreFilterSearchPhase.sortShards(
672-
request.shards,
673-
minAndMax,
674-
FieldSortBuilder.getPrimaryFieldSortOrNull(searchRequest.source()).order()
675-
);
676-
shards = pos -> indexes[pos];
677-
} else {
678-
shardSearchRequests = null;
679-
shards = IntUnaryOperator.identity();
680673
}
681674
final CancellableTask cancellableTask = (CancellableTask) task;
682675
int workers = Math.min(searchRequest.getMaxConcurrentShardRequests(), Math.min(shardCount, searchPoolMax));

0 commit comments

Comments
 (0)