kkewwei commented on code in PR #16857:
URL: https://github.com/apache/lucene/pull/16857#discussion_r4389213156
##########
lucene/core/src/java/org/apache/lucene/search/FieldValueHitQueue.java:
##########
@@ -166,6 +167,76 @@ public int[] getReverseMul() {
return lessThan.reverseMul;
}
+ /**
+ * Returns a single {@link LeafFieldComparator} over all the sort fields. If
the segment's index
+ * sort starts with the search sort, the comparator terminates collection
once the queue is full
+ * of competitive hits and the hits threshold has been reached.
+ *
+ * @param paging whether a top value has been set on the comparators
+ */
+ LeafFieldComparator getLeafComparator(LeafReaderContext context, boolean
paging)
+ throws IOException {
+ LeafFieldComparator[] leafComparators = getComparators(context);
+ int[] reverseMuls = getReverseMul();
+ LeafFieldComparator comparator;
+ int reverseMul;
+ if (leafComparators.length == 1) {
+ comparator = leafComparators[0];
+ reverseMul = reverseMuls[0];
+ } else {
+ comparator = new MultiLeafFieldComparator(leafComparators, reverseMuls);
+ reverseMul = 1;
+ }
+ if (canEarlyTerminate(fields, context.reader())) {
+ comparator = new IndexSortedLeafFieldComparator(comparator, reverseMul,
paging);
+ }
+ return comparator;
+ }
+
+ /**
+ * Whether docs in the segment are visited in the order of the search sort:
the first search sort
+ * is {@code _doc}, or the search sort is a prefix of the segment's index
sort. Index sort fields
+ * that have no effect in this segment are ignored.
+ */
+ static boolean canEarlyTerminate(SortField[] searchSort, LeafReader reader) {
+ if (canEarlyTerminate(new Sort(searchSort), reader.getMetaData().sort())) {
+ return true;
+ }
+ // leading index sort fields may be no-ops in this segment, in which case
docs are ordered by
+ // the remaining ones
+ int primary = Sort.primarySortFieldIndex(reader);
+ return primary > 0 && isPrefix(searchSort,
reader.getMetaData().sort().getSort(), primary);
+ }
+
+ /**
+ * Whether the first search sort is {@code _doc}, or the search sort is a
prefix of the index
+ * sort, which may be null.
+ */
+ static boolean canEarlyTerminate(Sort searchSort, Sort indexSort) {
+ SortField[] searchFields = searchSort.getSort();
+ if (SortField.FIELD_DOC.equals(unwrap(searchFields[0]))) {
+ return true;
+ }
+ return indexSort != null && isPrefix(searchFields, indexSort.getSort(), 0);
Review Comment:
We should not early terminate when `leafMetaData.hasBlocks()` is true; we
encountered such a case in #16893.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]