Skip to content

Commit f50725e

Browse files
authored
Remove TopDocsCollectorContext#shouldRescore method (elastic#94895)
QueryPhase#executeInternal returns a boolean that tells whether we should rescore. That one is returned by searchWithCollector which in turn is returned by TopDocsCollectorContext#shouldRescore which returns false by default and the only subclass that overrides it returns whether the search context had a rescorer configured. We can simplify this by pulling out the logic that we use to make the decision on whether to execute the rescore phase or not, and remove the shouldRescore method and return value that gets carried around which adds complexity.
1 parent c787e38 commit f50725e

File tree

4 files changed

+12
-33
lines changed

4 files changed

+12
-33
lines changed

server/src/main/java/org/elasticsearch/search/query/QueryPhase.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ public static void execute(SearchContext searchContext) throws QueryPhaseExecuti
7777
// request, preProcess is called on the DFS phase, this is why we pre-process them
7878
// here to make sure it happens during the QUERY phase
7979
AggregationPhase.preProcess(searchContext);
80-
boolean rescore = executeInternal(searchContext);
80+
executeInternal(searchContext);
8181

82-
if (rescore) { // only if we do a regular search
83-
RescorePhase.execute(searchContext);
84-
}
82+
RescorePhase.execute(searchContext);
8583
SuggestPhase.execute(searchContext);
8684
AggregationPhase.execute(searchContext);
8785

@@ -93,9 +91,8 @@ public static void execute(SearchContext searchContext) throws QueryPhaseExecuti
9391
/**
9492
* In a package-private method so that it can be tested without having to
9593
* wire everything (mapperService, etc.)
96-
* @return whether the rescoring phase should be executed
9794
*/
98-
static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExecutionException {
95+
static void executeInternal(SearchContext searchContext) throws QueryPhaseExecutionException {
9996
final ContextIndexSearcher searcher = searchContext.searcher();
10097
final IndexReader reader = searcher.getIndexReader();
10198
QuerySearchResult queryResult = searchContext.queryResult();
@@ -176,7 +173,7 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
176173
}
177174

178175
try {
179-
boolean shouldRescore = searchWithCollector(searchContext, searcher, query, collectors, hasFilterCollector, timeoutSet);
176+
searchWithCollector(searchContext, searcher, query, collectors, hasFilterCollector, timeoutSet);
180177
ExecutorService executor = searchContext.indexShard().getThreadPool().executor(ThreadPool.Names.SEARCH);
181178
assert executor instanceof EWMATrackingEsThreadPoolExecutor
182179
|| (executor instanceof EsThreadPoolExecutor == false /* in case thread pool is mocked out in tests */)
@@ -185,7 +182,6 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
185182
queryResult.nodeQueueSize(rExecutor.getCurrentQueueSize());
186183
queryResult.serviceTimeEWMA((long) rExecutor.getTaskExecutionEWMA());
187184
}
188-
return shouldRescore;
189185
} finally {
190186
// Search phase has finished, no longer need to check for timeout
191187
// otherwise aggregation phase might get cancelled.
@@ -198,7 +194,7 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
198194
}
199195
}
200196

201-
private static boolean searchWithCollector(
197+
private static void searchWithCollector(
202198
SearchContext searchContext,
203199
ContextIndexSearcher searcher,
204200
Query query,
@@ -238,7 +234,6 @@ private static boolean searchWithCollector(
238234
for (QueryCollectorContext ctx : collectors) {
239235
ctx.postProcess(queryResult);
240236
}
241-
return topDocsFactory.shouldRescore();
242237
}
243238

244239
/**

server/src/main/java/org/elasticsearch/search/query/TopDocsCollectorContext.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,6 @@ abstract class TopDocsCollectorContext extends QueryCollectorContext {
7474
this.numHits = numHits;
7575
}
7676

77-
/**
78-
* Returns the number of top docs to retrieve
79-
*/
80-
final int numHits() {
81-
return numHits;
82-
}
83-
84-
/**
85-
* Returns true if the top docs should be re-scored after initial search
86-
*/
87-
boolean shouldRescore() {
88-
return false;
89-
}
90-
9177
static class EmptyTopDocsCollectorContext extends TopDocsCollectorContext {
9278
private final Sort sort;
9379
private final Collector collector;
@@ -188,7 +174,7 @@ void postProcess(QuerySearchResult result) throws IOException {
188174
}
189175
}
190176

191-
abstract static class SimpleTopDocsCollectorContext extends TopDocsCollectorContext {
177+
static class SimpleTopDocsCollectorContext extends TopDocsCollectorContext {
192178

193179
private static TopDocsCollector<?> createCollector(
194180
@Nullable SortAndFormats sortAndFormats,
@@ -479,12 +465,7 @@ static TopDocsCollectorContext createTopDocsCollectorContext(SearchContext searc
479465
searchContext.trackScores(),
480466
searchContext.trackTotalHitsUpTo(),
481467
hasFilterCollector
482-
) {
483-
@Override
484-
boolean shouldRescore() {
485-
return rescore;
486-
}
487-
};
468+
);
488469
}
489470
}
490471

server/src/main/java/org/elasticsearch/search/rescore/RescorePhase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
public class RescorePhase {
2323

2424
public static void execute(SearchContext context) {
25+
if (context.size() == 0 || context.collapse() != null || context.rescore() == null || context.rescore().isEmpty()) {
26+
return;
27+
}
28+
2529
TopDocs topDocs = context.queryResult().topDocs().topDocs;
2630
if (topDocs.scoreDocs.length == 0) {
2731
return;

server/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ private void countTestCase(Query query, IndexReader reader, boolean shouldCollec
116116
context.parsedQuery(new ParsedQuery(query));
117117
context.setSize(0);
118118
context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
119-
final boolean rescore = QueryPhase.executeInternal(context);
120-
assertFalse(rescore);
119+
QueryPhase.executeInternal(context);
121120

122121
ContextIndexSearcher countSearcher = shouldCollectCount
123122
? newContextSearcher(reader)

0 commit comments

Comments
 (0)