Apache Lucene open-source search software
Fix searchafter high latency when after value is out of range for segment (#12334)
Chaitanya Goheld44be240256a
diff --git a/lucene/core/src/java/org/apache/lucene/search/comparators/NumericComparator.java b/lucene/core/src/java/org/apache/lucene/search/comparators/NumericComparator.javaindex 0ac859d40f1..ea75530fb2b 100644--- a/lucene/core/src/java/org/apache/lucene/search/comparators/NumericComparator.java+++ b/lucene/core/src/java/org/apache/lucene/search/comparators/NumericComparator.java@@ -91,8 +91,8 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato// if skipping functionality should be enabled on this segmentprivate final boolean enableSkipping;private final int maxDoc;- private final byte[] minValueAsBytes;- private final byte[] maxValueAsBytes;+ private byte[] minValueAsBytes;+ private byte[] maxValueAsBytes;private DocIdSetIterator competitiveIterator;private long iteratorCost = -1;@@ -128,16 +128,10 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato}this.enableSkipping = true; // skipping is enabled when points are availablethis.maxDoc = context.reader().maxDoc();- this.maxValueAsBytes =- reverse == false ? new byte[bytesCount] : topValueSet ? new byte[bytesCount] : null;- this.minValueAsBytes =- reverse ? new byte[bytesCount] : topValueSet ? new byte[bytesCount] : null;this.competitiveIterator = DocIdSetIterator.all(maxDoc);} else {this.enableSkipping = false;this.maxDoc = 0;- this.maxValueAsBytes = null;- this.minValueAsBytes = null;}}@@ -191,7 +185,9 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato// update its iterator to include possibly only docs that are "stronger" than the current bottom// entryprivate void updateCompetitiveIterator() throws IOException {- if (enableSkipping == false || hitsThresholdReached == false || queueFull == false) return;+ if (enableSkipping == false+ || hitsThresholdReached == false+ || (queueFull == false && topValueSet == false)) return;// if some documents have missing points, check that missing values prohibits optimizationif ((pointValues.getDocCount() < maxDoc) && isMissingValueCompetitive()) {return; // we can't filter out documents, as documents with missing values are competitive@@ -204,13 +200,21 @@ public abstract class NumericComparator<T extends Number> extends FieldComparatoreturn;}if (reverse == false) {- encodeBottom(maxValueAsBytes);+ if (queueFull) { // bottom is avilable only when queue is full+ maxValueAsBytes = maxValueAsBytes == null ? new byte[bytesCount] : maxValueAsBytes;+ encodeBottom(maxValueAsBytes);+ }if (topValueSet) {+ minValueAsBytes = minValueAsBytes == null ? new byte[bytesCount] : minValueAsBytes;encodeTop(minValueAsBytes);}} else {- encodeBottom(minValueAsBytes);+ if (queueFull) { // bottom is avilable only when queue is full+ minValueAsBytes = minValueAsBytes == null ? new byte[bytesCount] : minValueAsBytes;+ encodeBottom(minValueAsBytes);+ }if (topValueSet) {+ maxValueAsBytes = maxValueAsBytes == null ? new byte[bytesCount] : maxValueAsBytes;encodeTop(maxValueAsBytes);}}