Revert "LUCENE-10385: Implement Weight#count on IndexSortSortedNumeri… (#745)
In LUCENE-10458 we identified a bug in the logic. We're reverting on the 9.1
branch to avoid holding up the release.
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 19fd1a5..2c4ec5f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -114,9 +114,6 @@
based on TotalHitCountCollector that allows users to parallelize counting the
number of hits. (Luca Cavanna, Adrien Grand)
-* LUCENE-10385: Implement Weight#count on IndexSortSortedNumericDocValuesRangeQuery
- to speed up computing the number of hits when possible. (Luca Cavanna, Adrien Grand)
-
* LUCENE-10403: Add ArrayUtil#grow(T[]). (Greg Miller)
* LUCENE-10414: Add fn:fuzzyTerm interval function to flexible query parser (Dawid Weiss,
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/IndexSortSortedNumericDocValuesRangeQuery.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/IndexSortSortedNumericDocValuesRangeQuery.java
index 1dc80af..6401e8b 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/IndexSortSortedNumericDocValuesRangeQuery.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/IndexSortSortedNumericDocValuesRangeQuery.java
@@ -163,19 +163,29 @@
@Override
public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOException {
final Weight weight = this;
- DocIdSetIterator disi = getDocIdSetIteratorOrNull(context);
- if (disi != null) {
- return new ScorerSupplier() {
- @Override
- public Scorer get(long leadCost) throws IOException {
- return new ConstantScoreScorer(weight, score(), scoreMode, disi);
- }
+ SortedNumericDocValues sortedNumericValues =
+ DocValues.getSortedNumeric(context.reader(), field);
+ NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues);
- @Override
- public long cost() {
- return disi.cost();
- }
- };
+ if (numericValues != null) {
+ Sort indexSort = context.reader().getMetaData().getSort();
+ if (indexSort != null
+ && indexSort.getSort().length > 0
+ && indexSort.getSort()[0].getField().equals(field)) {
+ SortField sortField = indexSort.getSort()[0];
+ DocIdSetIterator disi = getDocIdSetIterator(sortField, context, numericValues);
+ return new ScorerSupplier() {
+ @Override
+ public Scorer get(long leadCost) throws IOException {
+ return new ConstantScoreScorer(weight, score(), scoreMode, disi);
+ }
+
+ @Override
+ public long cost() {
+ return disi.cost();
+ }
+ };
+ }
}
return fallbackWeight.scorerSupplier(context);
}
@@ -195,36 +205,9 @@
// if the fallback query is cacheable.
return fallbackWeight.isCacheable(ctx);
}
-
- @Override
- public int count(LeafReaderContext context) throws IOException {
- BoundedDocSetIdIterator disi = getDocIdSetIteratorOrNull(context);
- if (disi != null) {
- return disi.lastDoc - disi.firstDoc;
- }
- return fallbackWeight.count(context);
- }
};
}
- private BoundedDocSetIdIterator getDocIdSetIteratorOrNull(LeafReaderContext context)
- throws IOException {
- SortedNumericDocValues sortedNumericValues =
- DocValues.getSortedNumeric(context.reader(), field);
- NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues);
- if (numericValues != null) {
- Sort indexSort = context.reader().getMetaData().getSort();
- if (indexSort != null
- && indexSort.getSort().length > 0
- && indexSort.getSort()[0].getField().equals(field)) {
-
- SortField sortField = indexSort.getSort()[0];
- return getDocIdSetIterator(sortField, context, numericValues);
- }
- }
- return null;
- }
-
/**
* Computes the document IDs that lie within the range [lowerValue, upperValue] by performing
* binary search on the field's doc values.
@@ -237,7 +220,7 @@
* {@link DocIdSetIterator} makes sure to wrap the original docvalues to skip over documents with
* no value.
*/
- private BoundedDocSetIdIterator getDocIdSetIterator(
+ private DocIdSetIterator getDocIdSetIterator(
SortField sortField, LeafReaderContext context, DocIdSetIterator delegate)
throws IOException {
long lower = sortField.getReverse() ? upperValue : lowerValue;
diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestIndexSortSortedNumericDocValuesRangeQuery.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestIndexSortSortedNumericDocValuesRangeQuery.java
index 2217374..5f6f726 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestIndexSortSortedNumericDocValuesRangeQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestIndexSortSortedNumericDocValuesRangeQuery.java
@@ -459,56 +459,6 @@
reader.close();
}
- public void testCount() throws IOException {
- Directory dir = newDirectory();
- IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
- Sort indexSort = new Sort(new SortedNumericSortField("field", SortField.Type.LONG));
- iwc.setIndexSort(indexSort);
- RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
- Document doc = new Document();
- doc.add(new SortedNumericDocValuesField("field", 10));
- writer.addDocument(doc);
- IndexReader reader = writer.getReader();
- IndexSearcher searcher = newSearcher(reader);
-
- Query fallbackQuery = LongPoint.newRangeQuery("field", 1, 42);
- Query query = new IndexSortSortedNumericDocValuesRangeQuery("field", 1, 42, fallbackQuery);
- Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE, 1.0f);
- for (LeafReaderContext context : searcher.getLeafContexts()) {
- assertEquals(1, weight.count(context));
- }
-
- writer.close();
- reader.close();
- dir.close();
- }
-
- public void testFallbackCount() throws IOException {
- Directory dir = newDirectory();
- IndexWriterConfig iwc = new IndexWriterConfig(new MockAnalyzer(random()));
- Sort indexSort = new Sort(new SortedNumericSortField("field", SortField.Type.LONG));
- iwc.setIndexSort(indexSort);
- RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
- Document doc = new Document();
- doc.add(new SortedNumericDocValuesField("field", 10));
- writer.addDocument(doc);
- IndexReader reader = writer.getReader();
- IndexSearcher searcher = newSearcher(reader);
-
- // we use an unrealistic query that exposes its own Weight#count
- Query fallbackQuery = new MatchNoDocsQuery();
- // the index is not sorted on this field, the fallback query is used
- Query query = new IndexSortSortedNumericDocValuesRangeQuery("another", 1, 42, fallbackQuery);
- Weight weight = query.createWeight(searcher, ScoreMode.COMPLETE, 1.0f);
- for (LeafReaderContext context : searcher.getLeafContexts()) {
- assertEquals(0, weight.count(context));
- }
-
- writer.close();
- reader.close();
- dir.close();
- }
-
private Document createDocument(String field, long value) {
Document doc = new Document();
doc.add(new SortedNumericDocValuesField(field, value));