LUCENE-8607: MatchAllDocsQuery can skip counting hits

This commit is contained in:
Alan Woodward 2018-12-13 09:01:49 +00:00
parent 42f13731b3
commit fa025e1f78
3 changed files with 29 additions and 1 deletions

View File

@ -220,6 +220,9 @@ Optimizations
to early terminate the iterator if the minimum score is greater than the constant
score. (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 =======================
Build

View File

@ -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 {
@Override
public 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() {

View File

@ -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();
}
}