You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by lu...@apache.org on 2024/10/25 07:21:18 UTC

(lucene) branch branch_10x updated: Check ahead if we can get the count (#13899)

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

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


The following commit(s) were added to refs/heads/branch_10x by this push:
     new 85fbe6e3279 Check ahead if we can get the count (#13899)
85fbe6e3279 is described below

commit 85fbe6e327963dc45b41ffadaf10393ad2d0a3af
Author: Lu Xugang <lu...@apache.org>
AuthorDate: Fri Oct 25 15:05:51 2024 +0800

    Check ahead if we can get the count (#13899)
    
    Currently, we traverse the BKD tree or perform a binary search using DocValues first, and then check whether the count can be obtained in the count() method of IndexSortSortedNumericDocValuesRangeQuery.
    
    we should consider providing a mechanism to perform this check beforehand, avoid unnecessary processing when dealing with a sparseRange
---
 lucene/CHANGES.txt                                 |  2 ++
 .../IndexSortSortedNumericDocValuesRangeQuery.java | 36 +++++++++++++++++++++-
 2 files changed, 37 insertions(+), 1 deletion(-)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 1358b3c588c..439dc3fe9d2 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -37,6 +37,8 @@ Optimizations
 * GITHUB#13941: Optimized computation of top-hits on disjunctive queries with
   many clauses. (Adrien Grand)
 
+* GITHUB#13899: Check ahead if we can get the count. (Lu Xugang)
+
 Bug Fixes
 ---------------------
 * GITHUB#13832: Fixed an issue where the DefaultPassageFormatter.format method did not format passages as intended
diff --git a/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java
index e8b341cdcfb..3b6ef588dfb 100644
--- a/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/IndexSortSortedNumericDocValuesRangeQuery.java
@@ -186,10 +186,44 @@ public class IndexSortSortedNumericDocValuesRangeQuery extends Query {
       @Override
       public int count(LeafReaderContext context) throws IOException {
         if (context.reader().hasDeletions() == false) {
-          IteratorAndCount itAndCount = getDocIdSetIteratorOrNull(context);
+          if (lowerValue > upperValue) {
+            return 0;
+          }
+          IteratorAndCount itAndCount = null;
+          LeafReader reader = context.reader();
+
+          // first use bkd optimization if possible
+          SortedNumericDocValues sortedNumericValues = DocValues.getSortedNumeric(reader, field);
+          NumericDocValues numericValues = DocValues.unwrapSingleton(sortedNumericValues);
+          PointValues pointValues = reader.getPointValues(field);
+          if (pointValues != null && pointValues.getDocCount() == reader.maxDoc()) {
+            itAndCount = getDocIdSetIteratorOrNullFromBkd(context, numericValues);
+          }
           if (itAndCount != null && itAndCount.count != -1) {
             return itAndCount.count;
           }
+
+          // use index sort optimization if possible
+          Sort indexSort = reader.getMetaData().sort();
+          if (indexSort != null
+              && indexSort.getSort().length > 0
+              && indexSort.getSort()[0].getField().equals(field)) {
+            final SortField sortField = indexSort.getSort()[0];
+            final SortField.Type sortFieldType = getSortFieldType(sortField);
+            // The index sort optimization is only supported for Type.INT and Type.LONG
+            if (sortFieldType == Type.INT || sortFieldType == Type.LONG) {
+              Object missingValue = sortField.getMissingValue();
+              final long missingLongValue = missingValue == null ? 0L : (long) missingValue;
+              // all documents have docValues or missing value falls outside the range
+              if ((pointValues != null && pointValues.getDocCount() == reader.maxDoc())
+                  || (missingLongValue < lowerValue || missingLongValue > upperValue)) {
+                itAndCount = getDocIdSetIterator(sortField, sortFieldType, context, numericValues);
+              }
+              if (itAndCount != null && itAndCount.count != -1) {
+                return itAndCount.count;
+              }
+            }
+          }
         }
         return fallbackWeight.count(context);
       }