@@ -129,21 +129,16 @@ public ScorerSupplier scorerSupplier(LeafReaderContext context) throws IOExcepti
129
129
}
130
130
131
131
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 );
143
137
}
144
138
145
139
SortedNumericDocValues values = DocValues .getSortedNumeric (context .reader (), field );
146
140
final NumericDocValues singleton = DocValues .unwrapSingleton (values );
141
+ final DocValuesSkipper skipper = context .reader ().getDocValuesSkipper (field );
147
142
TwoPhaseIterator iterator ;
148
143
if (singleton != null ) {
149
144
if (skipper != null ) {
@@ -199,18 +194,30 @@ public float matchCost() {
199
194
200
195
@ Override
201
196
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 ();
208
202
}
209
- if (skipper .docCount () == context .reader ().maxDoc ()
210
- && skipper .minValue () >= lowerValue
211
- && skipper .maxValue () <= upperValue ) {
203
+ return cnt ;
204
+ }
212
205
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
+ }
214
221
}
215
222
return -1 ;
216
223
}
0 commit comments