Skip to content

Commit 814ec2f

Browse files
committed
CR comment
1 parent 83350f6 commit 814ec2f

File tree

3 files changed

+47
-40
lines changed

3 files changed

+47
-40
lines changed

src/query/boolean_query/boolean_weight.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
174174
per_occur_scorers.remove(&Occur::Must).unwrap_or_default();
175175
let must_special_scorer_counts = remove_and_count_all_and_empty_scorers(&mut must_scorers);
176176

177-
if must_special_scorer_counts.empty_count > 0 {
177+
if must_special_scorer_counts.num_empty_scorers > 0 {
178178
return Ok(SpecializedScorer::Other(Box::new(EmptyScorer)));
179179
}
180180

@@ -188,14 +188,14 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
188188
let exclude_special_scorer_counts =
189189
remove_and_count_all_and_empty_scorers(&mut exclude_scorers);
190190

191-
if exclude_special_scorer_counts.all_count > 0 {
191+
if exclude_special_scorer_counts.num_all_scorers > 0 {
192192
// We exclude all documents at one point.
193193
return Ok(SpecializedScorer::Other(Box::new(EmptyScorer)));
194194
}
195195

196196
let minimum_number_should_match = self
197197
.minimum_number_should_match
198-
.saturating_sub(should_special_scorer_counts.all_count);
198+
.saturating_sub(should_special_scorer_counts.num_all_scorers);
199199

200200
let should_scorers: ShouldScorersCombinationMethod = {
201201
let num_of_should_scorers = should_scorers.len();
@@ -244,10 +244,17 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
244244
))
245245
};
246246

247-
let positive_scorer = match (should_scorers, must_scorers) {
247+
let include_scorer = match (should_scorers, must_scorers) {
248248
(ShouldScorersCombinationMethod::Ignored, must_scorers) => {
249249
let boxed_scorer: Box<dyn Scorer> = if must_scorers.is_empty() {
250-
if must_special_scorer_counts.all_count + should_special_scorer_counts.all_count
250+
// We do not have any should scorers, nor all scorers.
251+
// There are still two cases here.
252+
//
253+
// If this follows the removal of some AllScorers in the should/must clauses, then
254+
// we match all documents.
255+
//
256+
// Otherwise, it is really just an EmptyScorer.
257+
if must_special_scorer_counts.num_all_scorers + should_special_scorer_counts.num_all_scorers
251258
> 0
252259
{
253260
Box::new(AllScorer::new(reader.max_doc()))
@@ -260,7 +267,7 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
260267
SpecializedScorer::Other(boxed_scorer)
261268
}
262269
(ShouldScorersCombinationMethod::Optional(should_scorer), must_scorers) => {
263-
if must_scorers.is_empty() && must_special_scorer_counts.all_count == 0 {
270+
if must_scorers.is_empty() && must_special_scorer_counts.num_all_scorers == 0 {
264271
// Optional options are promoted to required if no must scorers exists.
265272
should_scorer
266273
} else {
@@ -289,22 +296,22 @@ impl<TScoreCombiner: ScoreCombiner> BooleanWeight<TScoreCombiner> {
289296
}
290297
};
291298
if let Some(exclude_scorer) = exclude_scorer_opt {
292-
let positive_scorer_boxed =
293-
into_box_scorer(positive_scorer, &score_combiner_fn, num_docs);
299+
let include_scorer_boxed =
300+
into_box_scorer(include_scorer, &score_combiner_fn, num_docs);
294301
Ok(SpecializedScorer::Other(Box::new(Exclude::new(
295-
positive_scorer_boxed,
302+
include_scorer_boxed,
296303
exclude_scorer,
297304
))))
298305
} else {
299-
Ok(positive_scorer)
306+
Ok(include_scorer)
300307
}
301308
}
302309
}
303310

304311
#[derive(Default, Copy, Clone, Debug)]
305312
struct AllAndEmptyScorerCounts {
306-
all_count: usize,
307-
empty_count: usize,
313+
num_all_scorers: usize,
314+
num_empty_scorers: usize,
308315
}
309316

310317
fn remove_and_count_all_and_empty_scorers(
@@ -313,10 +320,10 @@ fn remove_and_count_all_and_empty_scorers(
313320
let mut counts = AllAndEmptyScorerCounts::default();
314321
scorers.retain(|scorer| {
315322
if scorer.is::<AllScorer>() {
316-
counts.all_count += 1;
323+
counts.num_all_scorers += 1;
317324
false
318325
} else if scorer.is::<EmptyScorer>() {
319-
counts.empty_count += 1;
326+
counts.num_empty_scorers += 1;
320327
false
321328
} else {
322329
true
@@ -361,7 +368,7 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
361368

362369
let mut explanation = Explanation::new("BooleanClause. sum of ...", scorer.score());
363370
for (occur, subweight) in &self.weights {
364-
if is_positive_occur(*occur) {
371+
if is_include_occur(*occur) {
365372
if let Ok(child_explanation) = subweight.explain(reader, doc) {
366373
explanation.add_detail(child_explanation);
367374
}
@@ -445,7 +452,7 @@ impl<TScoreCombiner: ScoreCombiner + Sync> Weight for BooleanWeight<TScoreCombin
445452
}
446453
}
447454

448-
fn is_positive_occur(occur: Occur) -> bool {
455+
fn is_include_occur(occur: Occur) -> bool {
449456
match occur {
450457
Occur::Must | Occur::Should => true,
451458
Occur::MustNot => false,

src/query/boolean_query/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,50 +323,50 @@ mod tests {
323323
index_writer.add_document(doc!(text_field=>"hello happy"))?;
324324
index_writer.commit()?;
325325
let searcher = index.reader()?.searcher();
326-
let term_a: Box<dyn Query> = Box::new(TermQuery::new(
326+
let term_match_all: Box<dyn Query> = Box::new(TermQuery::new(
327327
Term::from_field_text(text_field, "hello"),
328328
IndexRecordOption::Basic,
329329
));
330-
let term_b: Box<dyn Query> = Box::new(TermQuery::new(
330+
let term_match_some: Box<dyn Query> = Box::new(TermQuery::new(
331331
Term::from_field_text(text_field, "happy"),
332332
IndexRecordOption::Basic,
333333
));
334-
let term_c: Box<dyn Query> = Box::new(TermQuery::new(
334+
let term_match_none: Box<dyn Query> = Box::new(TermQuery::new(
335335
Term::from_field_text(text_field, "tax"),
336336
IndexRecordOption::Basic,
337337
));
338338
{
339339
let query = BooleanQuery::from(vec![
340-
(Occur::Must, term_a.box_clone()),
341-
(Occur::Must, term_b.box_clone()),
340+
(Occur::Must, term_match_all.box_clone()),
341+
(Occur::Must, term_match_some.box_clone()),
342342
]);
343343
let weight = query.weight(EnableScoring::disabled_from_searcher(&searcher))?;
344344
let scorer = weight.scorer(searcher.segment_reader(0u32), 1.0f32)?;
345345
assert!(scorer.is::<TermScorer>());
346346
}
347347
{
348348
let query = BooleanQuery::from(vec![
349-
(Occur::Must, term_a.box_clone()),
350-
(Occur::Must, term_b.box_clone()),
351-
(Occur::Must, term_c.box_clone()),
349+
(Occur::Must, term_match_all.box_clone()),
350+
(Occur::Must, term_match_some.box_clone()),
351+
(Occur::Must, term_match_none.box_clone()),
352352
]);
353353
let weight = query.weight(EnableScoring::disabled_from_searcher(&searcher))?;
354354
let scorer = weight.scorer(searcher.segment_reader(0u32), 1.0f32)?;
355355
assert!(scorer.is::<EmptyScorer>());
356356
}
357357
{
358358
let query = BooleanQuery::from(vec![
359-
(Occur::Should, term_a.box_clone()),
360-
(Occur::Should, term_c.box_clone()),
359+
(Occur::Should, term_match_all.box_clone()),
360+
(Occur::Should, term_match_none.box_clone()),
361361
]);
362362
let weight = query.weight(EnableScoring::disabled_from_searcher(&searcher))?;
363363
let scorer = weight.scorer(searcher.segment_reader(0u32), 1.0f32)?;
364364
assert!(scorer.is::<AllScorer>());
365365
}
366366
{
367367
let query = BooleanQuery::from(vec![
368-
(Occur::Should, term_b.box_clone()),
369-
(Occur::Should, term_c.box_clone()),
368+
(Occur::Should, term_match_some.box_clone()),
369+
(Occur::Should, term_match_none.box_clone()),
370370
]);
371371
let weight = query.weight(EnableScoring::disabled_from_searcher(&searcher))?;
372372
let scorer = weight.scorer(searcher.segment_reader(0u32), 1.0f32)?;

src/query/term_query/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,14 @@ mod tests {
473473
.unwrap()
474474
};
475475
// Should be an allscorer
476-
let hello_scorer = get_scorer_for_term("hello");
476+
let match_all_scorer = get_scorer_for_term("hello");
477477
// Should be a term scorer
478-
let happy_scorer = get_scorer_for_term("happy");
478+
let match_some_scorer = get_scorer_for_term("happy");
479479
// Should be an empty scorer
480-
let tax_scorer = get_scorer_for_term("tax");
481-
assert!(hello_scorer.is::<AllScorer>());
482-
assert!(happy_scorer.is::<TermScorer>());
483-
assert!(tax_scorer.is::<EmptyScorer>());
480+
let empty_scorer = get_scorer_for_term("tax");
481+
assert!(match_all_scorer.is::<AllScorer>());
482+
assert!(match_some_scorer.is::<TermScorer>());
483+
assert!(empty_scorer.is::<EmptyScorer>());
484484
}
485485

486486
#[test]
@@ -512,13 +512,13 @@ mod tests {
512512
.unwrap()
513513
};
514514
// Should be an allscorer
515-
let hello_scorer = get_scorer_for_term("hello");
515+
let match_all_scorer = get_scorer_for_term("hello");
516516
// Should be a term scorer
517-
let happy_scorer = get_scorer_for_term("happy");
517+
let one_scorer = get_scorer_for_term("happy");
518518
// Should be an empty scorer
519-
let tax_scorer = get_scorer_for_term("tax");
520-
assert!(hello_scorer.is::<TermScorer>());
521-
assert!(happy_scorer.is::<TermScorer>());
522-
assert!(tax_scorer.is::<EmptyScorer>());
519+
let empty_scorer = get_scorer_for_term("tax");
520+
assert!(match_all_scorer.is::<TermScorer>());
521+
assert!(one_scorer.is::<TermScorer>());
522+
assert!(empty_scorer.is::<EmptyScorer>());
523523
}
524524
}

0 commit comments

Comments
 (0)