Skip to content

Commit 283f8ac

Browse files
authored
Remove shortcutTotalHitCount optimization (#89047)
Our TopDocsCollectorContext has an optimization to try and avoid counting total hit count for queries like match all docs, term query and field exists query, relying on the statistics from each segment instead. This optimization has been recently streamlined in lucene through the introduction of Weight#count and now leveraged directly by TotalHitCountCollector in lucene with https://issues.apache.org/jira/browse/LUCENE-10620 , later complemented by #88396 within Elasticsearch. With this, we can remove this internal optimization and instead leverage the default lucene behaviour which covers more queries and will be possibly expanded in the future as well. Closes #81034
1 parent c08c16e commit 283f8ac

File tree

4 files changed

+79
-304
lines changed

4 files changed

+79
-304
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,21 +129,15 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
129129
}
130130

131131
final LinkedList<QueryCollectorContext> collectors = new LinkedList<>();
132-
// whether the chain contains a collector that filters documents
133-
boolean hasFilterCollector = false;
134132
if (searchContext.terminateAfter() != SearchContext.DEFAULT_TERMINATE_AFTER) {
135133
// add terminate_after before the filter collectors
136134
// it will only be applied on documents accepted by these filter collectors
137135
collectors.add(createEarlyTerminationCollectorContext(searchContext.terminateAfter()));
138-
// this collector can filter documents during the collection
139-
hasFilterCollector = true;
140136
}
141137
if (searchContext.parsedPostFilter() != null) {
142138
// add post filters before aggregations
143139
// it will only be applied to top hits
144140
collectors.add(createFilteredCollectorContext(searcher, searchContext.parsedPostFilter().query()));
145-
// this collector can filter documents during the collection
146-
hasFilterCollector = true;
147141
}
148142
if (searchContext.queryCollectors().isEmpty() == false) {
149143
// plug in additional collectors, like aggregations
@@ -152,8 +146,6 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
152146
if (searchContext.minimumScore() != null) {
153147
// apply the minimum score after multi collector so we filter aggs as well
154148
collectors.add(createMinScoreCollectorContext(searchContext.minimumScore()));
155-
// this collector can filter documents during the collection
156-
hasFilterCollector = true;
157149
}
158150

159151
boolean timeoutSet = scrollContext == null
@@ -176,7 +168,7 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
176168
}
177169

178170
try {
179-
boolean shouldRescore = searchWithCollector(searchContext, searcher, query, collectors, hasFilterCollector, timeoutSet);
171+
boolean shouldRescore = searchWithCollector(searchContext, searcher, query, collectors, timeoutSet);
180172
ExecutorService executor = searchContext.indexShard().getThreadPool().executor(ThreadPool.Names.SEARCH);
181173
assert executor instanceof EWMATrackingEsThreadPoolExecutor
182174
|| (executor instanceof EsThreadPoolExecutor == false /* in case thread pool is mocked out in tests */)
@@ -203,11 +195,10 @@ private static boolean searchWithCollector(
203195
ContextIndexSearcher searcher,
204196
Query query,
205197
LinkedList<QueryCollectorContext> collectors,
206-
boolean hasFilterCollector,
207198
boolean timeoutSet
208199
) throws IOException {
209200
// create the top docs collector last when the other collectors are known
210-
final TopDocsCollectorContext topDocsFactory = createTopDocsCollectorContext(searchContext, hasFilterCollector);
201+
final TopDocsCollectorContext topDocsFactory = createTopDocsCollectorContext(searchContext);
211202
// add the top docs collector, the first collector context in the chain
212203
collectors.addFirst(topDocsFactory);
213204

0 commit comments

Comments
 (0)