@@ -129,21 +129,16 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
129129 }
130130
131131 int maxDoc = context .reader ().maxDoc ();
132- DocValuesSkipper skipper = context .reader ().getDocValuesSkipper (field );
133- if (skipper != null ) {
134- if (skipper .minValue () > upperValue || skipper .maxValue () < lowerValue ) {
135- return null ;
136- }
137- if (skipper .docCount () == maxDoc
138- && skipper .minValue () >= lowerValue
139- && skipper .maxValue () <= upperValue ) {
140-
141- return ConstantScoreScorerSupplier .matchAll (score (), scoreMode , maxDoc );
142- }
132+ int count = docCountIgnoringDeletes (context );
133+ if (count == 0 ) {
134+ return null ;
135+ } else if (count == maxDoc ) {
136+ return ConstantScoreScorerSupplier .matchAll (score (), scoreMode , maxDoc );
143137 }
144138
145139 SortedNumericDocValues values = DocValues .getSortedNumeric (context .reader (), field );
146140 final NumericDocValues singleton = DocValues .unwrapSingleton (values );
141+ final DocValuesSkipper skipper = context .reader ().getDocValuesSkipper (field );
147142 TwoPhaseIterator iterator ;
148143 if (singleton != null ) {
149144 if (skipper != null ) {
@@ -199,18 +194,30 @@ public float matchCost() {
199194
200195 @ Override
201196 public int count (LeafReaderContext context ) throws IOException {
202- DocValuesSkipper skipper = context .reader ().getDocValuesSkipper (field );
203- if (skipper == null ) {
204- return -1 ;
205- }
206- if (skipper .minValue () > upperValue || skipper .maxValue () < lowerValue ) {
207- return 0 ;
197+ int maxDoc = context .reader ().maxDoc ();
198+ int cnt = docCountIgnoringDeletes (context );
199+ if (cnt == maxDoc ) {
200+ // Return LeafReader#numDocs that accounts for deleted documents as well
201+ return context .reader ().numDocs ();
208202 }
209- if (skipper .docCount () == context .reader ().maxDoc ()
210- && skipper .minValue () >= lowerValue
211- && skipper .maxValue () <= upperValue ) {
203+ return cnt ;
204+ }
212205
213- return context .reader ().numDocs ();
206+ /* Returns
207+ * # docs within the query range ignoring any deleted documents
208+ * -1 if # docs cannot be determined efficiently
209+ */
210+ private int docCountIgnoringDeletes (LeafReaderContext context ) throws IOException {
211+ final DocValuesSkipper skipper = context .reader ().getDocValuesSkipper (field );
212+ if (skipper != null ) {
213+ if (skipper .minValue () > upperValue || skipper .maxValue () < lowerValue ) {
214+ return 0 ;
215+ }
216+ if (skipper .docCount () == context .reader ().maxDoc ()
217+ && skipper .minValue () >= lowerValue
218+ && skipper .maxValue () <= upperValue ) {
219+ return context .reader ().maxDoc ();
220+ }
214221 }
215222 return -1 ;
216223 }
0 commit comments