jimczi commented on a change in pull request #2256: URL: https://github.com/apache/lucene-solr/pull/2256#discussion_r572984156
########## File path: lucene/core/src/java/org/apache/lucene/index/StandardDirectoryReader.java ########## @@ -39,33 +40,47 @@ final IndexWriter writer; final SegmentInfos segmentInfos; + private final Comparator<LeafReader> leafSorter; private final boolean applyAllDeletes; private final boolean writeAllDeletes; - /** called only from static open() methods */ + /** package private constructor, called only from static open() methods. */ StandardDirectoryReader( Directory directory, LeafReader[] readers, IndexWriter writer, SegmentInfos sis, + Comparator<LeafReader> leafSorter, boolean applyAllDeletes, boolean writeAllDeletes) throws IOException { - super(directory, readers); + super(directory, sortLeaves(readers, leafSorter)); Review comment: I wonder if the leafSorter should be declared and executed in the base class `BaseCompositeReader` ? That would expose the feature explicitly to `MultiReader` and friends. ########## File path: lucene/core/src/java/org/apache/lucene/index/DirectoryReader.java ########## @@ -56,7 +57,24 @@ * @throws IOException if there is a low-level IO error */ public static DirectoryReader open(final Directory directory) throws IOException { - return StandardDirectoryReader.open(directory, null); + return StandardDirectoryReader.open(directory, null, null); + } + + /** + * Returns a IndexReader for the the index in the given Directory + * + * @param directory the index directory + * @param leafSorter a comparator for sorting leaf readers. Providing leafSorter is useful for + * indices on which it is expected to run many queries with particular sort criteria (e.g. for + * time-based indices this is usually a descending sort on timestamp). In this case {@code + * leafSorter} should sort leaves according to this sort criteria. Providing leafSorter allows + * to speed up this particular type of sort queries by early terminating while iterating + * though segments and segments' documents. Review comment: nit: s/though/through/ ########## File path: lucene/core/src/test/org/apache/lucene/index/TestIndexWriterReader.java ########## @@ -169,7 +176,7 @@ public void testUpdateDocument() throws Exception { // writer.close wrote a new commit assertFalse(r2.isCurrent()); - DirectoryReader r3 = DirectoryReader.open(dir1); + DirectoryReader r3 = open(dir1); Review comment: Can you keep the explicit version ? ########## File path: lucene/core/src/java/org/apache/lucene/index/IndexWriterConfig.java ########## @@ -478,6 +479,18 @@ public IndexWriterConfig setIndexSort(Sort sort) { return this; } + /** + * Set the comparator for sorting leaf readers. A DirectoryReader opened from a IndexWriter with + * this configuration will have its leaf readers sorted with the provided leaf sorter. + * + * @param leafSorter – a comparator for sorting leaf readers + * @return IndexWriterConfig with leafSorter set. + */ + public IndexWriterConfig setLeafSorter(Comparator<LeafReader> leafSorter) { Review comment: You added a specific unit test for this feature but we could also set a random value in `LuceneTestCase#newIndexWriterConfig` to improve the coverage. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org