Skip to content

Apache Lucene open-source search software

Skipper-based numeric pruning should work from the start of a segment (#15757)

Alan Woodwarda54503278f9e
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 0f8daea1aee..25be4990fbd 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -221,6 +221,8 @@ New Features
Improvements
---------------------
+* GITHUB#15757: Better skipper-based numeric sort pruning from the start of segments (Alan Woodward)
+
* GITHUB#15753: Add Case Insensitive makeChar and makeString to Automata. (John Wagster)
Optimizations
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.java
index 5b62de7345b..92e82b9db1b 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
@@ -343,8 +343,7 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato
// helps to be conservative about increasing the sampling interval
private int tryUpdateFailCount = 0;
- PointsCompetitiveDISIBuilder(PointValues pointValues, NumericLeafComparator comparator)
- throws IOException {
+ PointsCompetitiveDISIBuilder(PointValues pointValues, NumericLeafComparator comparator) {
super(comparator);
LeafReaderContext context = comparator.context;
FieldInfo info = context.reader().getFieldInfos().fieldInfo(field);
@@ -368,7 +367,6 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato
+ bytesCount);
}
this.pointValues = pointValues;
- postInitializeCompetitiveIterator();
}
@Override
@@ -389,30 +387,6 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato
return pointValues.getDocCount() != maxDoc;
}
- /**
- * If queue is full and global min/max point values are not competitive with bottom then set an
- * empty iterator as competitive iterator.
- *
- * @throws IOException i/o exception while fetching min and max values from point values
- */
- void postInitializeCompetitiveIterator() throws IOException {
- if (queueFull && hitsThresholdReached) {
- // if some documents have missing points, then check that missing values prohibits
- // optimization
- if (hasMissingDocs() && isMissingValueCompetitive()) {
- return;
- }
- long bottom = leafComparator.bottomAsComparableLong();
- long minValue = sortableBytesToLong(pointValues.getMinPackedValue());
- long maxValue = sortableBytesToLong(pointValues.getMaxPackedValue());
- if (reverse == false && bottom < minValue) {
- updateCompetitiveIterator(DocIdSetIterator.empty());
- } else if (reverse && bottom > maxValue) {
- updateCompetitiveIterator(DocIdSetIterator.empty());
- }
- }
- }
-
@Override
protected void doUpdateCompetitiveIterator() throws IOException {
DocIdSetBuilder result = new DocIdSetBuilder(maxDoc);
@@ -531,32 +505,16 @@ public abstract class NumericComparator<T extends Number> extends FieldComparato
DocValuesSkipper skipper, NumericLeafComparator leafComparator) {
super(leafComparator);
this.skipper = skipper;
- postInitializeCompetitiveIterator();
}
@Override
- protected boolean hasMissingDocs() {
- return skipper.docCount() != maxDoc;
+ void setScorer(Scorable scorer) throws IOException {
+ updateCompetitiveIterator();
}
- /**
- * If queue is full and global min/max skipper are not competitive with bottom then set an empty
- * iterator as competitive iterator.
- */
- void postInitializeCompetitiveIterator() {
- if (queueFull && hitsThresholdReached) {
- // if some documents have missing doc values, check that missing values prohibits
- // optimization
- if (hasMissingDocs() && isMissingValueCompetitive()) {
- return;
- }
- long bottom = leafComparator.bottomAsComparableLong();
- if (reverse == false && bottom < skipper.minValue()) {
- updateCompetitiveIterator(DocIdSetIterator.empty());
- } else if (reverse && bottom > skipper.maxValue()) {
- updateCompetitiveIterator(DocIdSetIterator.empty());
- }
- }
+ @Override
+ protected boolean hasMissingDocs() {
+ return skipper.docCount() != maxDoc;
}
@Override
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 492c6fd5e02..2cf00565023 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSortOptimization.java
@@ -25,6 +25,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
+import org.apache.lucene.codecs.Codec;
+import org.apache.lucene.codecs.lucene90.Lucene90DocValuesFormat;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.FloatDocValuesField;
@@ -992,6 +994,76 @@ public class TestSortOptimization extends LuceneTestCase {
dir.close();
}
+ public void testPointsPrunesOnSegmentEntry() throws IOException {
+ testPrunesOnSegmentEntry(
+ TestUtil.getDefaultCodec(),
+ 2048,
+ (field, value) ->
+ List.of(new LongPoint(field, value), new NumericDocValuesField(field, value)));
+ }
+
+ public void testDVSkipperPrunesOnSegmentEntry() throws IOException {
+ testPrunesOnSegmentEntry(
+ TestUtil.alwaysDocValuesFormat(new Lucene90DocValuesFormat(16)),
+ 16,
+ (field, value) -> List.of(NumericDocValuesField.indexedField(field, value)));
+ }
+
+ /**
+ * Test that CompetitiveDISIBuilder enables pruning from the very start of a subsequent segment.
+ */
+ private void testPrunesOnSegmentEntry(
+ Codec codec, int blockSize, BiFunction<String, Integer, List<IndexableField>> fieldsBuilder)
+ throws IOException {
+ final Directory dir = newDirectory();
+ IndexWriterConfig config =
+ new IndexWriterConfig().setCodec(codec).setMergePolicy(NoMergePolicy.INSTANCE);
+ final IndexWriter writer = new IndexWriter(dir, config);
+
+ final int numHits = 5;
+ final int docsInFirstSegment = numHits * 2;
+ for (int i = 0; i < docsInFirstSegment; i++) {
+ final Document doc = new Document();
+ fieldsBuilder.apply("my_field", i).forEach(doc::add);
+ writer.addDocument(doc);
+ }
+ writer.flush();
+
+ // Many non-competitive values followed by one block of competitive values, so that pruning
+ // must be established on segment entry to skip the non-competitive docs.
+ final int nonCompetitiveBlocks = 20;
+ final int docsInNonCompetitiveBlocks = nonCompetitiveBlocks * blockSize;
+ for (int i = 0; i < docsInNonCompetitiveBlocks; i++) {
+ final Document doc = new Document();
+ fieldsBuilder.apply("my_field", 1000 + i).forEach(doc::add);
+ writer.addDocument(doc);
+ }
+ for (int i = 0; i < blockSize; i++) {
+ final Document doc = new Document();
+ fieldsBuilder.apply("my_field", i).forEach(doc::add);
+ writer.addDocument(doc);
+ }
+ writer.flush();
+
+ final DirectoryReader reader = DirectoryReader.open(writer);
+ writer.close();
+ assertEquals(2, reader.leaves().size());
+
+ final SortField sortField = new SortField("my_field", SortField.Type.LONG);
+ IndexSearcher searcher = new IndexSearcher(reader);
+ TopFieldDocs topDocs =
+ searcher.search(
+ MatchAllDocsQuery.INSTANCE,
+ new TopFieldCollectorManager(new Sort(sortField), numHits, 1));
+
+ assertEquals(numHits, topDocs.scoreDocs.length);
+ final int totalDocs = docsInFirstSegment + docsInNonCompetitiveBlocks + blockSize;
+ assertNonCompetitiveHitsAreSkipped(topDocs.totalHits.value(), totalDocs);
+
+ reader.close();
+ dir.close();
+ }
+
private void assertNonCompetitiveHitsAreSkipped(long collectedHits, long numDocs) {
if (collectedHits >= numDocs) {
fail(
diff --git a/lucene/core/src/test/org/apache/lucene/search/comparators/TestNumericComparator.java b/lucene/core/src/test/org/apache/lucene/search/comparators/TestNumericComparator.java
index 2a536c3037b..5ea3e5c47e6 100644
--- a/lucene/core/src/test/org/apache/lucene/search/comparators/TestNumericComparator.java
+++ b/lucene/core/src/test/org/apache/lucene/search/comparators/TestNumericComparator.java
@@ -215,6 +215,7 @@ public class TestNumericComparator extends LuceneTestCase {
comparator1.hitsThresholdReached = true;
comparator1.bottom = bottom;
var leafComparator = comparator1.getLeafComparator(leafContext);
+ leafComparator.setScorer(null);
assertEquals(DocIdSetIterator.NO_MORE_DOCS, leafComparator.competitiveIterator().nextDoc());
}
@@ -229,6 +230,7 @@ public class TestNumericComparator extends LuceneTestCase {
comparator1.hitsThresholdReached = true;
comparator1.bottom = bottom;
var leafComparator = comparator1.getLeafComparator(leafContext);
+ leafComparator.setScorer(null);
assertEquals(DocIdSetIterator.NO_MORE_DOCS, leafComparator.competitiveIterator().nextDoc());
}
@@ -243,6 +245,7 @@ public class TestNumericComparator extends LuceneTestCase {
comparator1.hitsThresholdReached = true;
comparator1.bottom = bottom;
var leafComparator = comparator1.getLeafComparator(leafContext);
+ leafComparator.setScorer(null);
assertEquals(DocIdSetIterator.NO_MORE_DOCS, leafComparator.competitiveIterator().nextDoc());
}
@@ -257,6 +260,7 @@ public class TestNumericComparator extends LuceneTestCase {
comparator1.hitsThresholdReached = true;
comparator1.bottom = bottom;
var leafComparator = comparator1.getLeafComparator(leafContext);
+ leafComparator.setScorer(null);
assertEquals(DocIdSetIterator.NO_MORE_DOCS, leafComparator.competitiveIterator().nextDoc());
}
}