Apache Solr open-source search software
LUCENE-8607: MatchAllDocsQuery can skip counting hits
Alan Woodwardfa025e1f7878
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex b4c7ca579ec..d6276aa2e63 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -220,6 +220,9 @@ Optimizationsto early terminate the iterator if the minimum score is greater than the constantscore. (Christophe Bismuth via Jim Ferenczi)+* LUCENE-8607: MatchAllDocsQuery can shortcut when total hit count is not+ required (Alan Woodward, Adrien Grand)+======================= Lucene 7.7.0 =======================Builddiff --git a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.javaindex 826494eeea1..80e8d3255bb 100644--- a/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java+++ b/lucene/core/src/java/org/apache/lucene/search/MatchAllDocsQuery.java@@ -24,7 +24,6 @@ import org.apache.lucene.util.Bits;/*** A query that matches all documents.- **/public final class MatchAllDocsQuery extends Query {@@ -47,6 +46,9 @@ public final class MatchAllDocsQuery extends Query {@Overridepublic BulkScorer bulkScorer(LeafReaderContext context) throws IOException {+ if (scoreMode == ScoreMode.TOP_SCORES) {+ return super.bulkScorer(context);+ }final float score = score();final int maxDoc = context.reader().maxDoc();return new BulkScorer() {diff --git a/lucene/core/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.javaindex ee02eed2279..a2e794e35ed 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java@@ -99,4 +99,27 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {iw.addDocument(doc);}+ public void testEarlyTermination() throws IOException {++ Directory dir = newDirectory();+ IndexWriter iw = new IndexWriter(dir, newIndexWriterConfig(analyzer).setMaxBufferedDocs(2).setMergePolicy(newLogMergePolicy()));+ for (int i = 0; i < 500; i++) {+ addDoc("doc" + i, iw);+ }+ IndexReader ir = DirectoryReader.open(iw);++ IndexSearcher is = newSearcher(ir);++ final int totalHitsThreshold = 200;+ TopScoreDocCollector c = TopScoreDocCollector.create(10, null, totalHitsThreshold);++ is.search(new MatchAllDocsQuery(), c);+ assertEquals(totalHitsThreshold, c.totalHits);++ iw.close();+ ir.close();+ dir.close();++ }+}