1
+ use core:: num;
1
2
use std:: collections:: HashMap ;
2
3
3
4
use crate :: docset:: COLLECT_BLOCK_BUFFER_LEN ;
42
43
fn scorer_union < TScoreCombiner > (
43
44
scorers : Vec < Box < dyn Scorer > > ,
44
45
score_combiner_fn : impl Fn ( ) -> TScoreCombiner ,
46
+ num_docs : u32 ,
45
47
) -> SpecializedScorer
46
48
where
47
49
TScoreCombiner : ScoreCombiner ,
@@ -68,23 +70,27 @@ where
68
70
return SpecializedScorer :: Other ( Box :: new ( BufferedUnionScorer :: build (
69
71
scorers,
70
72
score_combiner_fn,
73
+ num_docs,
71
74
) ) ) ;
72
75
}
73
76
}
74
77
}
75
78
SpecializedScorer :: Other ( Box :: new ( BufferedUnionScorer :: build (
76
79
scorers,
77
80
score_combiner_fn,
81
+ num_docs,
78
82
) ) )
79
83
}
80
84
81
85
fn into_box_scorer < TScoreCombiner : ScoreCombiner > (
82
86
scorer : SpecializedScorer ,
83
87
score_combiner_fn : impl Fn ( ) -> TScoreCombiner ,
88
+ num_docs : u32 ,
84
89
) -> Box < dyn Scorer > {
85
90
match scorer {
86
91
SpecializedScorer :: TermUnion ( term_scorers) => {
87
- let union_scorer = BufferedUnionScorer :: build ( term_scorers, score_combiner_fn) ;
92
+ let union_scorer =
93
+ BufferedUnionScorer :: build ( term_scorers, score_combiner_fn, num_docs) ;
88
94
Box :: new ( union_scorer)
89
95
}
90
96
SpecializedScorer :: Other ( scorer) => scorer,
@@ -151,6 +157,7 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
151
157
boost : Score ,
152
158
score_combiner_fn : impl Fn ( ) -> TComplexScoreCombiner ,
153
159
) -> crate :: Result < SpecializedScorer > {
160
+ let num_docs = reader. num_docs ( ) ;
154
161
let mut per_occur_scorers = self . per_occur_scorers ( reader, boost) ?;
155
162
// Indicate how should clauses are combined with other clauses.
156
163
enum CombinationMethod {
@@ -167,11 +174,16 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
167
174
return Ok ( SpecializedScorer :: Other ( Box :: new ( EmptyScorer ) ) ) ;
168
175
}
169
176
match self . minimum_number_should_match {
170
- 0 => CombinationMethod :: Optional ( scorer_union ( should_scorers, & score_combiner_fn) ) ,
171
- 1 => {
172
- let scorer_union = scorer_union ( should_scorers, & score_combiner_fn) ;
173
- CombinationMethod :: Required ( scorer_union)
174
- }
177
+ 0 => CombinationMethod :: Optional ( scorer_union (
178
+ should_scorers,
179
+ & score_combiner_fn,
180
+ num_docs,
181
+ ) ) ,
182
+ 1 => CombinationMethod :: Required ( scorer_union (
183
+ should_scorers,
184
+ & score_combiner_fn,
185
+ num_docs,
186
+ ) ) ,
175
187
n if num_of_should_scorers == n => {
176
188
// When num_of_should_scorers equals the number of should clauses,
177
189
// they are no different from must clauses.
@@ -200,30 +212,30 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
200
212
} ;
201
213
let exclude_scorer_opt: Option < Box < dyn Scorer > > = per_occur_scorers
202
214
. remove ( & Occur :: MustNot )
203
- . map ( |scorers| scorer_union ( scorers, DoNothingCombiner :: default) )
215
+ . map ( |scorers| scorer_union ( scorers, DoNothingCombiner :: default, num_docs ) )
204
216
. map ( |specialized_scorer : SpecializedScorer | {
205
- into_box_scorer ( specialized_scorer, DoNothingCombiner :: default)
217
+ into_box_scorer ( specialized_scorer, DoNothingCombiner :: default, num_docs )
206
218
} ) ;
207
219
let positive_scorer = match ( should_opt, must_scorers) {
208
220
( CombinationMethod :: Ignored , Some ( must_scorers) ) => {
209
- SpecializedScorer :: Other ( intersect_scorers ( must_scorers) )
221
+ SpecializedScorer :: Other ( intersect_scorers ( must_scorers, num_docs ) )
210
222
}
211
223
( CombinationMethod :: Optional ( should_scorer) , Some ( must_scorers) ) => {
212
- let must_scorer = intersect_scorers ( must_scorers) ;
224
+ let must_scorer = intersect_scorers ( must_scorers, num_docs ) ;
213
225
if self . scoring_enabled {
214
226
SpecializedScorer :: Other ( Box :: new (
215
227
RequiredOptionalScorer :: < _ , _ , TScoreCombiner > :: new (
216
228
must_scorer,
217
- into_box_scorer ( should_scorer, & score_combiner_fn) ,
229
+ into_box_scorer ( should_scorer, & score_combiner_fn, num_docs ) ,
218
230
) ,
219
231
) )
220
232
} else {
221
233
SpecializedScorer :: Other ( must_scorer)
222
234
}
223
235
}
224
236
( CombinationMethod :: Required ( should_scorer) , Some ( mut must_scorers) ) => {
225
- must_scorers. push ( into_box_scorer ( should_scorer, & score_combiner_fn) ) ;
226
- SpecializedScorer :: Other ( intersect_scorers ( must_scorers) )
237
+ must_scorers. push ( into_box_scorer ( should_scorer, & score_combiner_fn, num_docs ) ) ;
238
+ SpecializedScorer :: Other ( intersect_scorers ( must_scorers, num_docs ) )
227
239
}
228
240
( CombinationMethod :: Ignored , None ) => {
229
241
return Ok ( SpecializedScorer :: Other ( Box :: new ( EmptyScorer ) ) )
@@ -233,7 +245,8 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
233
245
( CombinationMethod :: Optional ( should_scorer) , None ) => should_scorer,
234
246
} ;
235
247
if let Some ( exclude_scorer) = exclude_scorer_opt {
236
- let positive_scorer_boxed = into_box_scorer ( positive_scorer, & score_combiner_fn) ;
248
+ let positive_scorer_boxed =
249
+ into_box_scorer ( positive_scorer, & score_combiner_fn, num_docs) ;
237
250
Ok ( SpecializedScorer :: Other ( Box :: new ( Exclude :: new (
238
251
positive_scorer_boxed,
239
252
exclude_scorer,
@@ -246,6 +259,7 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
246
259
247
260
impl < TScoreCombiner : ScoreCombiner + Sync > Weight for BooleanWeight < TScoreCombiner > {
248
261
fn scorer ( & self , reader : & SegmentReader , boost : Score ) -> crate :: Result < Box < dyn Scorer > > {
262
+ let num_docs = reader. num_docs ( ) ;
249
263
if self . weights . is_empty ( ) {
250
264
Ok ( Box :: new ( EmptyScorer ) )
251
265
} else if self . weights . len ( ) == 1 {
@@ -258,12 +272,12 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
258
272
} else if self . scoring_enabled {
259
273
self . complex_scorer ( reader, boost, & self . score_combiner_fn )
260
274
. map ( |specialized_scorer| {
261
- into_box_scorer ( specialized_scorer, & self . score_combiner_fn )
275
+ into_box_scorer ( specialized_scorer, & self . score_combiner_fn , num_docs )
262
276
} )
263
277
} else {
264
278
self . complex_scorer ( reader, boost, DoNothingCombiner :: default)
265
279
. map ( |specialized_scorer| {
266
- into_box_scorer ( specialized_scorer, DoNothingCombiner :: default)
280
+ into_box_scorer ( specialized_scorer, DoNothingCombiner :: default, num_docs )
267
281
} )
268
282
}
269
283
}
@@ -296,8 +310,11 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
296
310
let scorer = self . complex_scorer ( reader, 1.0 , & self . score_combiner_fn ) ?;
297
311
match scorer {
298
312
SpecializedScorer :: TermUnion ( term_scorers) => {
299
- let mut union_scorer =
300
- BufferedUnionScorer :: build ( term_scorers, & self . score_combiner_fn ) ;
313
+ let mut union_scorer = BufferedUnionScorer :: build (
314
+ term_scorers,
315
+ & self . score_combiner_fn ,
316
+ reader. num_docs ( ) ,
317
+ ) ;
301
318
for_each_scorer ( & mut union_scorer, callback) ;
302
319
}
303
320
SpecializedScorer :: Other ( mut scorer) => {
@@ -317,8 +334,11 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
317
334
318
335
match scorer {
319
336
SpecializedScorer :: TermUnion ( term_scorers) => {
320
- let mut union_scorer =
321
- BufferedUnionScorer :: build ( term_scorers, & self . score_combiner_fn ) ;
337
+ let mut union_scorer = BufferedUnionScorer :: build (
338
+ term_scorers,
339
+ & self . score_combiner_fn ,
340
+ reader. num_docs ( ) ,
341
+ ) ;
322
342
for_each_docset_buffered ( & mut union_scorer, & mut buffer, callback) ;
323
343
}
324
344
SpecializedScorer :: Other ( mut scorer) => {
0 commit comments