From 025835c5b2af5c2622c41504c19ea3f94aab6680 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 1 Mar 2021 14:03:12 +0100 Subject: [PATCH] Fix the criteria to avoid always returning a placeholder --- milli/src/search/criteria/asc_desc.rs | 7 +++++-- milli/src/search/criteria/fetcher.rs | 18 ++++++++++++------ milli/src/search/criteria/proximity.rs | 7 +++++-- milli/src/search/criteria/typo.rs | 7 +++++-- milli/src/search/criteria/words.rs | 8 ++++++-- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/milli/src/search/criteria/asc_desc.rs b/milli/src/search/criteria/asc_desc.rs index df80f3bb4..151b0a6a0 100644 --- a/milli/src/search/criteria/asc_desc.rs +++ b/milli/src/search/criteria/asc_desc.rs @@ -128,8 +128,11 @@ impl<'t> Criterion for AscDesc<'t> { loop { match (&mut self.query_tree, &mut self.candidates) { (_, Allowed(candidates)) if candidates.is_empty() => { - self.query_tree = None; - self.candidates = Candidates::default(); + return Ok(Some(CriterionResult { + query_tree: self.query_tree.take(), + candidates: take(&mut self.candidates).into_inner(), + bucket_candidates: take(&mut self.bucket_candidates), + })); }, (Some(qt), Allowed(candidates)) => { let bucket_candidates = match self.parent { diff --git a/milli/src/search/criteria/fetcher.rs b/milli/src/search/criteria/fetcher.rs index e21548e3f..f0cf16b90 100644 --- a/milli/src/search/criteria/fetcher.rs +++ b/milli/src/search/criteria/fetcher.rs @@ -54,20 +54,26 @@ impl<'t> Criterion for Fetcher<'t> { self.should_get_documents_ids, self.candidates, ); + let should_get_documents_ids = take(&mut self.should_get_documents_ids); match &mut self.candidates { - Allowed(candidates) => if candidates.is_empty() { - self.candidates = Candidates::default(); - } else { - self.should_get_documents_ids = false; + Allowed(candidates) => { let candidates = take(&mut self.candidates).into_inner(); + let candidates = match &self.query_tree { + Some(qt) if should_get_documents_ids => { + let mut docids = resolve_query_tree(self.ctx, &qt, &mut HashMap::new())?; + docids.intersect_with(&candidates); + docids + }, + _ => candidates, + }; + return Ok(Some(CriterionResult { - query_tree: self.query_tree.clone(), + query_tree: self.query_tree.take(), candidates: candidates.clone(), bucket_candidates: candidates, })); }, Forbidden(_) => { - let should_get_documents_ids = take(&mut self.should_get_documents_ids); match self.parent.as_mut() { Some(parent) => { match parent.next()? { diff --git a/milli/src/search/criteria/proximity.rs b/milli/src/search/criteria/proximity.rs index 352567d1a..553a191ec 100644 --- a/milli/src/search/criteria/proximity.rs +++ b/milli/src/search/criteria/proximity.rs @@ -64,8 +64,11 @@ impl<'t> Criterion for Proximity<'t> { match (&mut self.query_tree, &mut self.candidates) { (_, Allowed(candidates)) if candidates.is_empty() => { - self.query_tree = None; - self.candidates = Candidates::default(); + return Ok(Some(CriterionResult { + query_tree: self.query_tree.take().map(|(_, qt)| qt), + candidates: take(&mut self.candidates).into_inner(), + bucket_candidates: take(&mut self.bucket_candidates), + })); }, (Some((max_prox, query_tree)), Allowed(candidates)) => { if self.proximity as usize > *max_prox { diff --git a/milli/src/search/criteria/typo.rs b/milli/src/search/criteria/typo.rs index b82ebbf5b..5c8592c5e 100644 --- a/milli/src/search/criteria/typo.rs +++ b/milli/src/search/criteria/typo.rs @@ -64,8 +64,11 @@ impl<'t> Criterion for Typo<'t> { match (&mut self.query_tree, &mut self.candidates) { (_, Allowed(candidates)) if candidates.is_empty() => { - self.query_tree = None; - self.candidates = Candidates::default(); + return Ok(Some(CriterionResult { + query_tree: self.query_tree.take().map(|(_, qt)| qt), + candidates: take(&mut self.candidates).into_inner(), + bucket_candidates: take(&mut self.bucket_candidates), + })); }, (Some((max_typos, query_tree)), Allowed(candidates)) => { if self.number_typos as usize > *max_typos { diff --git a/milli/src/search/criteria/words.rs b/milli/src/search/criteria/words.rs index c8bb0abc4..bfb85579a 100644 --- a/milli/src/search/criteria/words.rs +++ b/milli/src/search/criteria/words.rs @@ -56,9 +56,13 @@ impl<'t> Criterion for Words<'t> { debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates); match (self.query_trees.pop(), &mut self.candidates) { - (_, Allowed(candidates)) if candidates.is_empty() => { + (query_tree, Allowed(candidates)) if candidates.is_empty() => { self.query_trees = Vec::new(); - self.candidates = Candidates::default(); + return Ok(Some(CriterionResult { + query_tree, + candidates: take(&mut self.candidates).into_inner(), + bucket_candidates: take(&mut self.bucket_candidates), + })); }, (Some(qt), Allowed(candidates)) => { let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache)?;