Add settings to force milli to exhaustively compute the total number of hits

This commit is contained in:
ManyTheFish 2022-07-12 17:56:50 +02:00
parent fad0de4581
commit a396806343
5 changed files with 45 additions and 12 deletions

View File

@ -1,17 +1,22 @@
use roaring::RoaringBitmap; use roaring::RoaringBitmap;
use super::{Criterion, CriterionParameters, CriterionResult}; use super::{Criterion, CriterionParameters, CriterionResult};
use crate::search::criteria::{resolve_query_tree, Context};
use crate::search::query_tree::Operation; use crate::search::query_tree::Operation;
use crate::Result; use crate::Result;
pub struct Initial { pub struct Initial<'t> {
ctx: &'t dyn Context<'t>,
answer: Option<CriterionResult>, answer: Option<CriterionResult>,
exhaustive_number_hits: bool,
} }
impl Initial { impl<'t> Initial<'t> {
pub fn new( pub fn new(
ctx: &'t dyn Context<'t>,
query_tree: Option<Operation>, query_tree: Option<Operation>,
filtered_candidates: Option<RoaringBitmap>, filtered_candidates: Option<RoaringBitmap>,
exhaustive_number_hits: bool,
) -> Initial { ) -> Initial {
let answer = CriterionResult { let answer = CriterionResult {
query_tree, query_tree,
@ -19,13 +24,28 @@ impl Initial {
filtered_candidates, filtered_candidates,
bucket_candidates: None, bucket_candidates: None,
}; };
Initial { answer: Some(answer) } Initial { ctx, answer: Some(answer), exhaustive_number_hits }
} }
} }
impl Criterion for Initial { impl Criterion for Initial<'_> {
#[logging_timer::time("Initial::{}")] #[logging_timer::time("Initial::{}")]
fn next(&mut self, _: &mut CriterionParameters) -> Result<Option<CriterionResult>> { fn next(&mut self, params: &mut CriterionParameters) -> Result<Option<CriterionResult>> {
Ok(self.answer.take()) self.answer
.take()
.map(|mut answer| {
if self.exhaustive_number_hits && answer.query_tree.is_some() {
let candidates = resolve_query_tree(
self.ctx,
answer.query_tree.as_ref().unwrap(),
&mut params.wdcache,
)?;
answer.candidates = Some(candidates.clone());
answer.bucket_candidates = Some(candidates);
}
Ok(answer)
})
.transpose()
} }
} }

View File

@ -232,13 +232,15 @@ impl<'t> CriteriaBuilder<'t> {
primitive_query: Option<Vec<PrimitiveQueryPart>>, primitive_query: Option<Vec<PrimitiveQueryPart>>,
filtered_candidates: Option<RoaringBitmap>, filtered_candidates: Option<RoaringBitmap>,
sort_criteria: Option<Vec<AscDescName>>, sort_criteria: Option<Vec<AscDescName>>,
exhaustive_number_hits: bool,
) -> Result<Final<'t>> { ) -> Result<Final<'t>> {
use crate::criterion::Criterion as Name; use crate::criterion::Criterion as Name;
let primitive_query = primitive_query.unwrap_or_default(); let primitive_query = primitive_query.unwrap_or_default();
let mut criterion = let mut criterion =
Box::new(Initial::new(query_tree, filtered_candidates)) as Box<dyn Criterion>; Box::new(Initial::new(self, query_tree, filtered_candidates, exhaustive_number_hits))
as Box<dyn Criterion>;
for name in self.index.criteria(&self.rtxn)? { for name in self.index.criteria(&self.rtxn)? {
criterion = match name { criterion = match name {
Name::Words => Box::new(Words::new(self, criterion)), Name::Words => Box::new(Words::new(self, criterion)),

View File

@ -368,7 +368,7 @@ mod test {
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(query_tree, facet_candidates); let parent = Initial::new(&context, query_tree, facet_candidates, false);
let criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let result = display_criteria(criteria, criterion_parameters); let result = display_criteria(criteria, criterion_parameters);
@ -405,7 +405,7 @@ mod test {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(Some(query_tree), facet_candidates); let parent = Initial::new(&context, Some(query_tree), facet_candidates, false);
let criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let result = display_criteria(criteria, criterion_parameters); let result = display_criteria(criteria, criterion_parameters);
@ -439,7 +439,7 @@ mod test {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(query_tree, Some(facet_candidates.clone())); let parent = Initial::new(&context, query_tree, Some(facet_candidates.clone()), false);
let criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let result = display_criteria(criteria, criterion_parameters); let result = display_criteria(criteria, criterion_parameters);
@ -476,7 +476,8 @@ mod test {
wdcache: &mut WordDerivationsCache::new(), wdcache: &mut WordDerivationsCache::new(),
excluded_candidates: &RoaringBitmap::new(), excluded_candidates: &RoaringBitmap::new(),
}; };
let parent = Initial::new(Some(query_tree), Some(facet_candidates.clone())); let parent =
Initial::new(&context, Some(query_tree), Some(facet_candidates.clone()), false);
let criteria = Typo::new(&context, Box::new(parent)); let criteria = Typo::new(&context, Box::new(parent));
let result = display_criteria(criteria, criterion_parameters); let result = display_criteria(criteria, criterion_parameters);

View File

@ -47,6 +47,7 @@ pub struct Search<'a> {
terms_matching_strategy: TermsMatchingStrategy, terms_matching_strategy: TermsMatchingStrategy,
authorize_typos: bool, authorize_typos: bool,
words_limit: usize, words_limit: usize,
exhaustive_number_hits: bool,
rtxn: &'a heed::RoTxn<'a>, rtxn: &'a heed::RoTxn<'a>,
index: &'a Index, index: &'a Index,
} }
@ -61,6 +62,7 @@ impl<'a> Search<'a> {
sort_criteria: None, sort_criteria: None,
terms_matching_strategy: TermsMatchingStrategy::default(), terms_matching_strategy: TermsMatchingStrategy::default(),
authorize_typos: true, authorize_typos: true,
exhaustive_number_hits: false,
words_limit: 10, words_limit: 10,
rtxn, rtxn,
index, index,
@ -107,6 +109,11 @@ impl<'a> Search<'a> {
self self
} }
pub fn exhaustive_number_hits(&mut self, exhaustive_number_hits: bool) -> &mut Search<'a> {
self.exhaustive_number_hits = exhaustive_number_hits;
self
}
fn is_typo_authorized(&self) -> Result<bool> { fn is_typo_authorized(&self) -> Result<bool> {
let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?; let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?;
// only authorize typos if both the index and the query allow it. // only authorize typos if both the index and the query allow it.
@ -189,6 +196,7 @@ impl<'a> Search<'a> {
primitive_query, primitive_query,
filtered_candidates, filtered_candidates,
self.sort_criteria.clone(), self.sort_criteria.clone(),
self.exhaustive_number_hits,
)?; )?;
match self.index.distinct_field(self.rtxn)? { match self.index.distinct_field(self.rtxn)? {
@ -262,6 +270,7 @@ impl fmt::Debug for Search<'_> {
terms_matching_strategy, terms_matching_strategy,
authorize_typos, authorize_typos,
words_limit, words_limit,
exhaustive_number_hits,
rtxn: _, rtxn: _,
index: _, index: _,
} = self; } = self;
@ -273,6 +282,7 @@ impl fmt::Debug for Search<'_> {
.field("sort_criteria", sort_criteria) .field("sort_criteria", sort_criteria)
.field("terms_matching_strategy", terms_matching_strategy) .field("terms_matching_strategy", terms_matching_strategy)
.field("authorize_typos", authorize_typos) .field("authorize_typos", authorize_typos)
.field("exhaustive_number_hits", exhaustive_number_hits)
.field("words_limit", words_limit) .field("words_limit", words_limit)
.finish() .finish()
} }

View File

@ -51,7 +51,7 @@ macro_rules! test_criterion {
}; };
} }
test_criterion!(none_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![], vec![]); test_criterion!(none_allow_typo, DISALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![], vec![]);
test_criterion!(none_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![], vec![]); test_criterion!(none_disallow_typo, DISALLOW_OPTIONAL_WORDS, DISALLOW_TYPOS, vec![], vec![]);
test_criterion!(words_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Words], vec![]); test_criterion!(words_allow_typo, ALLOW_OPTIONAL_WORDS, ALLOW_TYPOS, vec![Words], vec![]);
test_criterion!( test_criterion!(