Skip to content

Apache Lucene open-source search software

Enable collectors to take advantage of pre-aggregated data. (#14401)

Adrien Grand4bda52cf5bd9
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 342c1d108c5..e58df600b69 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -52,7 +52,8 @@ Other
API Changes
---------------------
-(No changes)
+* GITHUB#14401: Added LeafCollector#collectRange to enable collector to take
+ advantage of pre-aggregated data to speed up faceting. (Adrien Grand)
New Features
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/search/DenseConjunctionBulkScorer.java b/lucene/core/src/java/org/apache/lucene/search/DenseConjunctionBulkScorer.java
index f3a08842e52..57ad593916c 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DenseConjunctionBulkScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DenseConjunctionBulkScorer.java
@@ -191,7 +191,7 @@ final class DenseConjunctionBulkScorer extends BulkScorer {
if (acceptDocs == null && windowApproximations.isEmpty()) {
// We have a large range of doc IDs that all match.
- collector.collect(new RangeDocIdStream(min, minDocIDRunEnd));
+ collector.collectRange(min, minDocIDRunEnd);
return minDocIDRunEnd;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/LeafCollector.java b/lucene/core/src/java/org/apache/lucene/search/LeafCollector.java
index 2cc9d5c2e9d..f8765c18372 100644
--- a/lucene/core/src/java/org/apache/lucene/search/LeafCollector.java
+++ b/lucene/core/src/java/org/apache/lucene/search/LeafCollector.java
@@ -17,6 +17,7 @@
package org.apache.lucene.search;
import java.io.IOException;
+import org.apache.lucene.index.DocValuesSkipper;
import org.apache.lucene.index.StoredFields;
/**
@@ -83,6 +84,22 @@ public interface LeafCollector {
*/
void collect(int doc) throws IOException;
+ /**
+ * Collect a range of doc IDs, between {@code min} inclusive and {@code max} exclusive. {@code
+ * max} is guaranteed to be greater than {@code min}.
+ *
+ * <p>Extending this method is typically useful to take advantage of pre-aggregated data exposed
+ * in a {@link DocValuesSkipper}.
+ *
+ * <p>The default implementation calls {@link #collect(DocIdStream)} on a {@link DocIdStream} that
+ * matches the given range.
+ *
+ * @see #collect(int)
+ */
+ default void collectRange(int min, int max) throws IOException {
+ collect(new RangeDocIdStream(min, max));
+ }
+
/**
* Bulk-collect doc IDs.
*
diff --git a/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java b/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
index 0acba295559..fc128880fc1 100644
--- a/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
+++ b/lucene/core/src/java/org/apache/lucene/search/RangeDocIdStream.java
@@ -24,8 +24,8 @@ final class RangeDocIdStream extends DocIdStream {
private final int max;
RangeDocIdStream(int min, int max) {
- if (max < min) {
- throw new IllegalArgumentException("min = " + min + " > max = " + max);
+ if (min >= max) {
+ throw new IllegalArgumentException("min = " + min + " >= max = " + max);
}
this.upTo = min;
this.max = max;
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
index ca05ef76f87..de8a545283a 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/search/AssertingLeafCollector.java
@@ -50,6 +50,16 @@ class AssertingLeafCollector extends FilterLeafCollector {
in.collect(new AssertingDocIdStream(stream));
}
+ @Override
+ public void collectRange(int min, int max) throws IOException {
+ assert min > lastCollected;
+ assert max > min;
+ assert min >= this.min : "Out of range: " + min + " < " + this.min;
+ assert max <= this.max : "Out of range: " + (max - 1) + " >= " + this.max;
+ in.collectRange(min, max);
+ lastCollected = max - 1;
+ }
+
@Override
public void collect(int doc) throws IOException {
assert doc > lastCollected : "Out of order : " + lastCollected + " " + doc;