You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ja...@apache.org on 2025/08/27 18:38:21 UTC

(lucene) 03/03: Reusing count() for minor refactor in SortedNumericDocValuesRangeQuery (#15123)

This is an automated email from the ASF dual-hosted git repository.

jainankitk pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/lucene.git

commit fa70614506233a8ac24865e90436dca62310125d
Author: Ankit Jain <ja...@gmail.com>
AuthorDate: Wed Aug 27 10:16:25 2025 -0700

    Reusing count() for minor refactor in SortedNumericDocValuesRangeQuery (#15123)
    
    ---------
    Signed-off-by: Ankit Jain <ja...@gmail.com>
---
 .../document/SortedNumericDocValuesRangeQuery.java | 49 ++++++++++++----------
 1 file changed, 28 insertions(+), 21 deletions(-)

diff --git a/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java b/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java
index e72c0f81061..dd36d6ddb32 100644
--- a/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/document/SortedNumericDocValuesRangeQuery.java
@@ -129,21 +129,16 @@ final class SortedNumericDocValuesRangeQuery extends Query {
         }
 
         int maxDoc = context.reader().maxDoc();
-        DocValuesSkipper skipper = context.reader().getDocValuesSkipper(field);
-        if (skipper != null) {
-          if (skipper.minValue() > upperValue || skipper.maxValue() < lowerValue) {
-            return null;
-          }
-          if (skipper.docCount() == maxDoc
-              && skipper.minValue() >= lowerValue
-              && skipper.maxValue() <= upperValue) {
-
-            return ConstantScoreScorerSupplier.matchAll(score(), scoreMode, maxDoc);
-          }
+        int count = docCountIgnoringDeletes(context);
+        if (count == 0) {
+          return null;
+        } else if (count == maxDoc) {
+          return ConstantScoreScorerSupplier.matchAll(score(), scoreMode, maxDoc);
         }
 
         SortedNumericDocValues values = DocValues.getSortedNumeric(context.reader(), field);
         final NumericDocValues singleton = DocValues.unwrapSingleton(values);
+        final DocValuesSkipper skipper = context.reader().getDocValuesSkipper(field);
         TwoPhaseIterator iterator;
         if (singleton != null) {
           if (skipper != null) {
@@ -199,18 +194,30 @@ final class SortedNumericDocValuesRangeQuery extends Query {
 
       @Override
       public int count(LeafReaderContext context) throws IOException {
-        DocValuesSkipper skipper = context.reader().getDocValuesSkipper(field);
-        if (skipper == null) {
-          return -1;
-        }
-        if (skipper.minValue() > upperValue || skipper.maxValue() < lowerValue) {
-          return 0;
+        int maxDoc = context.reader().maxDoc();
+        int cnt = docCountIgnoringDeletes(context);
+        if (cnt == maxDoc) {
+          // Return LeafReader#numDocs that accounts for deleted documents as well
+          return context.reader().numDocs();
         }
-        if (skipper.docCount() == context.reader().maxDoc()
-            && skipper.minValue() >= lowerValue
-            && skipper.maxValue() <= upperValue) {
+        return cnt;
+      }
 
-          return context.reader().numDocs();
+      /* Returns
+       * # docs within the query range ignoring any deleted documents
+       * -1 if # docs cannot be determined efficiently
+       */
+      private int docCountIgnoringDeletes(LeafReaderContext context) throws IOException {
+        final DocValuesSkipper skipper = context.reader().getDocValuesSkipper(field);
+        if (skipper != null) {
+          if (skipper.minValue() > upperValue || skipper.maxValue() < lowerValue) {
+            return 0;
+          }
+          if (skipper.docCount() == context.reader().maxDoc()
+              && skipper.minValue() >= lowerValue
+              && skipper.maxValue() <= upperValue) {
+            return context.reader().maxDoc();
+          }
         }
         return -1;
       }