Skip to content

Commit

Permalink
Remove shortcutTotalHitCount optimization (elastic#89047)
Browse files Browse the repository at this point in the history
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 elastic#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 elastic#81034
  • Loading branch information
javanna authored Feb 6, 2023
1 parent c08c16e commit 283f8ac
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,15 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
}

final LinkedList<QueryCollectorContext> collectors = new LinkedList<>();
// whether the chain contains a collector that filters documents
boolean hasFilterCollector = false;
if (searchContext.terminateAfter() != SearchContext.DEFAULT_TERMINATE_AFTER) {
// add terminate_after before the filter collectors
// it will only be applied on documents accepted by these filter collectors
collectors.add(createEarlyTerminationCollectorContext(searchContext.terminateAfter()));
// this collector can filter documents during the collection
hasFilterCollector = true;
}
if (searchContext.parsedPostFilter() != null) {
// add post filters before aggregations
// it will only be applied to top hits
collectors.add(createFilteredCollectorContext(searcher, searchContext.parsedPostFilter().query()));
// this collector can filter documents during the collection
hasFilterCollector = true;
}
if (searchContext.queryCollectors().isEmpty() == false) {
// plug in additional collectors, like aggregations
Expand All @@ -152,8 +146,6 @@ static boolean executeInternal(SearchContext searchContext) throws QueryPhaseExe
if (searchContext.minimumScore() != null) {
// apply the minimum score after multi collector so we filter aggs as well
collectors.add(createMinScoreCollectorContext(searchContext.minimumScore()));
// this collector can filter documents during the collection
hasFilterCollector = true;
}

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

try {
boolean shouldRescore = searchWithCollector(searchContext, searcher, query, collectors, hasFilterCollector, timeoutSet);
boolean shouldRescore = searchWithCollector(searchContext, searcher, query, collectors, timeoutSet);
ExecutorService executor = searchContext.indexShard().getThreadPool().executor(ThreadPool.Names.SEARCH);
assert executor instanceof EWMATrackingEsThreadPoolExecutor
|| (executor instanceof EsThreadPoolExecutor == false /* in case thread pool is mocked out in tests */)
Expand All @@ -203,11 +195,10 @@ private static boolean searchWithCollector(
ContextIndexSearcher searcher,
Query query,
LinkedList<QueryCollectorContext> collectors,
boolean hasFilterCollector,
boolean timeoutSet
) throws IOException {
// create the top docs collector last when the other collectors are known
final TopDocsCollectorContext topDocsFactory = createTopDocsCollectorContext(searchContext, hasFilterCollector);
final TopDocsCollectorContext topDocsFactory = createTopDocsCollectorContext(searchContext);
// add the top docs collector, the first collector context in the chain
collectors.addFirst(topDocsFactory);

Expand Down
Loading

0 comments on commit 283f8ac

Please sign in to comment.