Skip to content

Apache Lucene open-source search software

Optimize IndexSearcher.count() to rewrite the query once (#16001)

David Smiley038e16787158
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f0bb778d385..b055ac1277c 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -353,6 +353,8 @@ Optimizations
* GITHUB#15732: Prevent writing vectors twice during merging HNSW graphs by allowing doing deferred work after calling
merge for vectors is finshed. (Ignacio Vera)
+* GITHUB#16001: IndexSearcher.count() was calling query.rewrite twice, a regression since v9.10 (David Smiley)
+
Bug Fixes
---------------------
* GITHUB#15754: Fix HTMLStripCharFilter to prevent tags from incorrectly consuming subsequent
diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
index 2d7bdc02ef7..91e77b6a30f 100644
--- a/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSearcher.java
@@ -493,28 +493,39 @@ public class IndexSearcher {
* possible.
*/
public int count(Query query) throws IOException {
- // Rewrite query before optimization check
+ // CSQ.rewrite may simplify the query -- don't need scores
query = rewrite(new ConstantScoreQuery(query));
- if (query instanceof ConstantScoreQuery csq) {
- query = csq.getQuery();
- }
- // Check if two clause disjunction optimization applies
- if (query instanceof BooleanQuery booleanQuery
- && this.reader.hasDeletions() == false
- && booleanQuery.isTwoClausePureDisjunctionWithTerms()) {
- Query[] queries = booleanQuery.rewriteTwoClauseDisjunctionWithTermsForCount(this);
- int countTerm1 = count(queries[0]);
- int countTerm2 = count(queries[1]);
- if (countTerm1 == 0 || countTerm2 == 0) {
- return Math.max(countTerm1, countTerm2);
- // Only apply optimization if the intersection is significantly smaller than the union
- } else if ((double) Math.min(countTerm1, countTerm2) / Math.max(countTerm1, countTerm2)
- < 0.1) {
- return countTerm1 + countTerm2 - count(queries[2]);
+ {
+ // Unwrap CSQ to check for optimizations on the inner query
+ Query innerQuery = query;
+ if (innerQuery instanceof ConstantScoreQuery csq) {
+ innerQuery = csq.getQuery();
+ }
+
+ // Check if two clause disjunction optimization applies
+ if (innerQuery instanceof BooleanQuery booleanQuery
+ && this.reader.hasDeletions() == false
+ && booleanQuery.isTwoClausePureDisjunctionWithTerms()) {
+ Query[] queries = booleanQuery.rewriteTwoClauseDisjunctionWithTermsForCount(this);
+ int countTerm1 = count(queries[0]);
+ int countTerm2 = count(queries[1]);
+ if (countTerm1 == 0 || countTerm2 == 0) {
+ return Math.max(countTerm1, countTerm2);
+ // Only apply optimization if the intersection is significantly smaller than the union
+ } else if ((double) Math.min(countTerm1, countTerm2) / Math.max(countTerm1, countTerm2)
+ < 0.1) {
+ return countTerm1 + countTerm2 - count(queries[2]);
+ }
}
}
- return search(new ConstantScoreQuery(query), new TotalHitCountCollectorManager(getSlices()));
+
+ // Use the already-rewritten query directly, avoiding a redundant rewrite in search(query,
+ // collector)
+ var collectorManager = new TotalHitCountCollectorManager(getSlices());
+ var firstCollector = collectorManager.newCollector();
+ final Weight weight = createWeight(query, firstCollector.scoreMode(), 1);
+ return search(weight, collectorManager, firstCollector);
}
/**
@@ -958,7 +969,7 @@ public class IndexSearcher {
/**
* Creates a {@link Weight} for the given query, potentially adding caching if possible and
- * configured.
+ * configured. The query is assumed to have been {@link #rewrite(Query)}-en already.
*
* @lucene.experimental
*/