Skip to content

Apache Lucene open-source search software

LUCENE-10126 Add extra test on _doc sort (#326)

Mayya Sharipova88b264a368b6
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java b/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
index e7697a7dddf..65e28eedc3e 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
@@ -40,6 +40,7 @@ import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LuceneTestCase;
public class TestSortOptimization extends LuceneTestCase {
@@ -483,6 +484,42 @@ public class TestSortOptimization extends LuceneTestCase {
dir.close();
}
+ public void testDocSortOptimizationWithAfterCollectsAllDocs() throws IOException {
+ final Directory dir = newDirectory();
+ final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig());
+ final int numDocs = atLeast(TEST_NIGHTLY ? 50_000 : 5_000);
+ final boolean multipleSegments = random().nextBoolean();
+ final int numDocsInSegment = numDocs / 10 + random().nextInt(numDocs / 10);
+
+ for (int i = 1; i <= numDocs; ++i) {
+ final Document doc = new Document();
+ writer.addDocument(doc);
+ if (multipleSegments && (i % numDocsInSegment == 0)) {
+ writer.flush();
+ }
+ }
+ writer.flush();
+
+ IndexReader reader = DirectoryReader.open(writer);
+ IndexSearcher searcher = newSearcher(reader);
+ int visitedHits = 0;
+ ScoreDoc after = null;
+ while (visitedHits < numDocs) {
+ int batch = 1 + random().nextInt(500);
+ Query query = new MatchAllDocsQuery();
+ TopDocs topDocs = searcher.searchAfter(after, query, batch, new Sort(FIELD_DOC));
+ int expectedHits = Math.min(numDocs - visitedHits, batch);
+ assertEquals(expectedHits, topDocs.scoreDocs.length);
+ after = topDocs.scoreDocs[expectedHits - 1];
+ for (int i = 0; i < topDocs.scoreDocs.length; i++) {
+ assertEquals(visitedHits, topDocs.scoreDocs[i].doc);
+ visitedHits++;
+ }
+ }
+ assertEquals(visitedHits, numDocs);
+ IOUtils.close(writer, reader, dir);
+ }
+
public void testDocSortOptimization() throws IOException {
final Directory dir = newDirectory();
final IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig());