Optimize words criterion

This commit is contained in:
many 2021-03-09 12:04:52 +01:00 committed by Kerollmops
parent c53be51460
commit 62a70c300d
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
7 changed files with 95 additions and 62 deletions

View File

@ -160,9 +160,21 @@ impl<'t> Criterion for AscDesc<'t> {
match self.parent.as_mut() { match self.parent.as_mut() {
Some(parent) => { Some(parent) => {
match parent.next(wdcache)? { match parent.next(wdcache)? {
Some(CriterionResult { query_tree, mut candidates, bucket_candidates }) => { Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_tree = query_tree; self.query_tree = query_tree;
let candidates = match (&self.query_tree, candidates) {
(_, Some(mut candidates)) => {
candidates.intersect_with(&self.faceted_candidates); candidates.intersect_with(&self.faceted_candidates);
candidates
},
(Some(qt), None) => {
let context = CriteriaBuilder::new(&self.rtxn, &self.index)?;
let mut candidates = resolve_query_tree(&context, qt, &mut HashMap::new(), wdcache)?;
candidates.intersect_with(&self.faceted_candidates);
candidates
},
(None, None) => take(&mut self.faceted_candidates),
};
self.candidates = facet_ordered( self.candidates = facet_ordered(
self.index, self.index,
self.rtxn, self.rtxn,
@ -183,7 +195,7 @@ impl<'t> Criterion for AscDesc<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree, query_tree,
candidates: RoaringBitmap::new(), candidates: Some(RoaringBitmap::new()),
bucket_candidates, bucket_candidates,
})); }));
}, },
@ -195,7 +207,7 @@ impl<'t> Criterion for AscDesc<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: self.query_tree.clone(), query_tree: self.query_tree.clone(),
candidates, candidates: Some(candidates),
bucket_candidates, bucket_candidates,
})); }));
}, },

View File

@ -8,12 +8,24 @@ use crate::search::query_tree::Operation;
use crate::search::WordDerivationsCache; use crate::search::WordDerivationsCache;
use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context}; use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context};
/// The result of a call to the parent criterion.
#[derive(Debug, Clone, PartialEq)]
pub struct FetcherResult {
/// The query tree that must be used by the children criterion to fetch candidates.
pub query_tree: Option<Operation>,
/// The candidates that this criterion is allowed to return subsets of.
pub candidates: RoaringBitmap,
/// Candidates that comes from the current bucket of the initial criterion.
pub bucket_candidates: RoaringBitmap,
}
pub struct Fetcher<'t> { pub struct Fetcher<'t> {
ctx: &'t dyn Context, ctx: &'t dyn Context,
query_tree: Option<Operation>, query_tree: Option<Operation>,
candidates: Candidates, candidates: Candidates,
parent: Option<Box<dyn Criterion + 't>>, parent: Option<Box<dyn Criterion + 't>>,
should_get_documents_ids: bool, should_get_documents_ids: bool,
wdcache: WordDerivationsCache,
} }
impl<'t> Fetcher<'t> { impl<'t> Fetcher<'t> {
@ -29,6 +41,7 @@ impl<'t> Fetcher<'t> {
candidates: candidates.map_or_else(Candidates::default, Candidates::Allowed), candidates: candidates.map_or_else(Candidates::default, Candidates::Allowed),
parent: None, parent: None,
should_get_documents_ids: true, should_get_documents_ids: true,
wdcache: WordDerivationsCache::new(),
} }
} }
@ -43,13 +56,12 @@ impl<'t> Fetcher<'t> {
candidates: Candidates::default(), candidates: Candidates::default(),
parent: Some(parent), parent: Some(parent),
should_get_documents_ids: true, should_get_documents_ids: true,
} wdcache: WordDerivationsCache::new(),
} }
} }
impl<'t> Criterion for Fetcher<'t> {
#[logging_timer::time("Fetcher::{}")] #[logging_timer::time("Fetcher::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> { pub fn next(&mut self) -> anyhow::Result<Option<FetcherResult>> {
use Candidates::{Allowed, Forbidden}; use Candidates::{Allowed, Forbidden};
loop { loop {
debug!("Fetcher iteration (should_get_documents_ids: {}) ({:?})", debug!("Fetcher iteration (should_get_documents_ids: {}) ({:?})",
@ -62,14 +74,14 @@ impl<'t> Criterion for Fetcher<'t> {
let candidates = take(&mut self.candidates).into_inner(); let candidates = take(&mut self.candidates).into_inner();
let candidates = match &self.query_tree { let candidates = match &self.query_tree {
Some(qt) if should_get_documents_ids => { Some(qt) if should_get_documents_ids => {
let mut docids = resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), wdcache)?; let mut docids = resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), &mut self.wdcache)?;
docids.intersect_with(&candidates); docids.intersect_with(&candidates);
docids docids
}, },
_ => candidates, _ => candidates,
}; };
return Ok(Some(CriterionResult { return Ok(Some(FetcherResult {
query_tree: self.query_tree.take(), query_tree: self.query_tree.take(),
candidates: candidates.clone(), candidates: candidates.clone(),
bucket_candidates: candidates, bucket_candidates: candidates,
@ -78,15 +90,23 @@ impl<'t> Criterion for Fetcher<'t> {
Forbidden(_) => { Forbidden(_) => {
match self.parent.as_mut() { match self.parent.as_mut() {
Some(parent) => { Some(parent) => {
match parent.next(wdcache)? { match parent.next(&mut self.wdcache)? {
Some(result) => return Ok(Some(result)), Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
let candidates = match (&query_tree, candidates) {
(_, Some(candidates)) => candidates,
(Some(qt), None) => resolve_query_tree(self.ctx, qt, &mut HashMap::new(), &mut self.wdcache)?,
(None, None) => RoaringBitmap::new(),
};
return Ok(Some(FetcherResult { query_tree, candidates, bucket_candidates }))
},
None => if should_get_documents_ids { None => if should_get_documents_ids {
let candidates = match &self.query_tree { let candidates = match &self.query_tree {
Some(qt) => resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), wdcache)?, Some(qt) => resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), &mut self.wdcache)?,
None => self.ctx.documents_ids()?, None => self.ctx.documents_ids()?,
}; };
return Ok(Some(CriterionResult { return Ok(Some(FetcherResult {
query_tree: self.query_tree.clone(), query_tree: self.query_tree.clone(),
candidates: candidates.clone(), candidates: candidates.clone(),
bucket_candidates: candidates, bucket_candidates: candidates,
@ -96,11 +116,11 @@ impl<'t> Criterion for Fetcher<'t> {
}, },
None => if should_get_documents_ids { None => if should_get_documents_ids {
let candidates = match &self.query_tree { let candidates = match &self.query_tree {
Some(qt) => resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), wdcache)?, Some(qt) => resolve_query_tree(self.ctx, &qt, &mut HashMap::new(), &mut self.wdcache)?,
None => self.ctx.documents_ids()?, None => self.ctx.documents_ids()?,
}; };
return Ok(Some(CriterionResult { return Ok(Some(FetcherResult {
query_tree: self.query_tree.clone(), query_tree: self.query_tree.clone(),
candidates: candidates.clone(), candidates: candidates.clone(),
bucket_candidates: candidates, bucket_candidates: candidates,

View File

@ -14,10 +14,10 @@ use self::asc_desc::AscDesc;
use self::proximity::Proximity; use self::proximity::Proximity;
use self::fetcher::Fetcher; use self::fetcher::Fetcher;
pub mod typo; mod typo;
pub mod words; mod words;
pub mod asc_desc; mod asc_desc;
pub mod proximity; mod proximity;
pub mod fetcher; pub mod fetcher;
pub trait Criterion { pub trait Criterion {
@ -28,11 +28,12 @@ pub trait Criterion {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub struct CriterionResult { pub struct CriterionResult {
/// The query tree that must be used by the children criterion to fetch candidates. /// The query tree that must be used by the children criterion to fetch candidates.
pub query_tree: Option<Operation>, query_tree: Option<Operation>,
/// The candidates that this criterion is allowed to return subsets of. /// The candidates that this criterion is allowed to return subsets of,
pub candidates: RoaringBitmap, /// if None, it is up to the child to compute the candidates itself.
candidates: Option<RoaringBitmap>,
/// Candidates that comes from the current bucket of the initial criterion. /// Candidates that comes from the current bucket of the initial criterion.
pub bucket_candidates: RoaringBitmap, bucket_candidates: RoaringBitmap,
} }
/// Either a set of candidates that defines the candidates /// Either a set of candidates that defines the candidates

View File

@ -9,7 +9,7 @@ use log::debug;
use crate::{DocumentId, Position, search::{query_tree::QueryKind, word_derivations}}; use crate::{DocumentId, Position, search::{query_tree::QueryKind, word_derivations}};
use crate::search::query_tree::{maximum_proximity, Operation, Query}; use crate::search::query_tree::{maximum_proximity, Operation, Query};
use crate::search::WordDerivationsCache; use crate::search::WordDerivationsCache;
use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids}; use super::{Candidates, Criterion, CriterionResult, Context, query_docids, query_pair_proximity_docids, resolve_query_tree};
pub struct Proximity<'t> { pub struct Proximity<'t> {
ctx: &'t dyn Context, ctx: &'t dyn Context,
@ -70,7 +70,7 @@ impl<'t> Criterion for Proximity<'t> {
(_, Allowed(candidates)) if candidates.is_empty() => { (_, Allowed(candidates)) if candidates.is_empty() => {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: self.query_tree.take().map(|(_, qt)| qt), query_tree: self.query_tree.take().map(|(_, qt)| qt),
candidates: take(&mut self.candidates).into_inner(), candidates: Some(take(&mut self.candidates).into_inner()),
bucket_candidates: take(&mut self.bucket_candidates), bucket_candidates: take(&mut self.bucket_candidates),
})); }));
}, },
@ -126,7 +126,7 @@ impl<'t> Criterion for Proximity<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(query_tree.clone()), query_tree: Some(query_tree.clone()),
candidates: new_candidates, candidates: Some(new_candidates),
bucket_candidates, bucket_candidates,
})); }));
} }
@ -155,7 +155,7 @@ impl<'t> Criterion for Proximity<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(query_tree.clone()), query_tree: Some(query_tree.clone()),
candidates: new_candidates, candidates: Some(new_candidates),
bucket_candidates, bucket_candidates,
})); }));
} }
@ -164,7 +164,7 @@ impl<'t> Criterion for Proximity<'t> {
let candidates = take(&mut self.candidates).into_inner(); let candidates = take(&mut self.candidates).into_inner();
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: None, query_tree: None,
candidates: candidates.clone(), candidates: Some(candidates.clone()),
bucket_candidates: candidates, bucket_candidates: candidates,
})); }));
}, },
@ -173,6 +173,12 @@ impl<'t> Criterion for Proximity<'t> {
Some(parent) => { Some(parent) => {
match parent.next(wdcache)? { match parent.next(wdcache)? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => { Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
let candidates = match (&query_tree, candidates) {
(_, Some(candidates)) => candidates,
(Some(qt), None) => resolve_query_tree(self.ctx, qt, &mut HashMap::new(), wdcache)?,
(None, None) => RoaringBitmap::new(),
};
self.query_tree = query_tree.map(|op| (maximum_proximity(&op), op)); self.query_tree = query_tree.map(|op| (maximum_proximity(&op), op));
self.proximity = 0; self.proximity = 0;
self.candidates = Candidates::Allowed(candidates); self.candidates = Candidates::Allowed(candidates);

View File

@ -63,7 +63,7 @@ impl<'t> Criterion for Typo<'t> {
(_, Allowed(candidates)) if candidates.is_empty() => { (_, Allowed(candidates)) if candidates.is_empty() => {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: self.query_tree.take().map(|(_, qt)| qt), query_tree: self.query_tree.take().map(|(_, qt)| qt),
candidates: take(&mut self.candidates).into_inner(), candidates: Some(take(&mut self.candidates).into_inner()),
bucket_candidates: take(&mut self.bucket_candidates), bucket_candidates: take(&mut self.bucket_candidates),
})); }));
}, },
@ -100,7 +100,7 @@ impl<'t> Criterion for Typo<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(new_query_tree), query_tree: Some(new_query_tree),
candidates: new_candidates, candidates: Some(new_candidates),
bucket_candidates, bucket_candidates,
})); }));
} }
@ -138,7 +138,7 @@ impl<'t> Criterion for Typo<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(new_query_tree), query_tree: Some(new_query_tree),
candidates: new_candidates, candidates: Some(new_candidates),
bucket_candidates, bucket_candidates,
})); }));
} }
@ -147,7 +147,7 @@ impl<'t> Criterion for Typo<'t> {
let candidates = take(&mut self.candidates).into_inner(); let candidates = take(&mut self.candidates).into_inner();
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: None, query_tree: None,
candidates: candidates.clone(), candidates: Some(candidates.clone()),
bucket_candidates: candidates, bucket_candidates: candidates,
})); }));
}, },
@ -158,7 +158,7 @@ impl<'t> Criterion for Typo<'t> {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => { Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_tree = query_tree.map(|op| (maximum_typo(&op), op)); self.query_tree = query_tree.map(|op| (maximum_typo(&op), op));
self.number_typos = 0; self.number_typos = 0;
self.candidates = Candidates::Allowed(candidates); self.candidates = candidates.map_or_else(Candidates::default, Candidates::Allowed);
self.bucket_candidates.union_with(&bucket_candidates); self.bucket_candidates.union_with(&bucket_candidates);
}, },
None => return Ok(None), None => return Ok(None),
@ -394,7 +394,7 @@ mod test {
Operation::Query(Query { prefix: false, kind: QueryKind::exact("world".to_string()) }), Operation::Query(Query { prefix: false, kind: QueryKind::exact("world".to_string()) }),
]), ]),
])), ])),
candidates: candidates_1.clone(), candidates: Some(candidates_1.clone()),
bucket_candidates: candidates_1, bucket_candidates: candidates_1,
}; };
@ -416,7 +416,7 @@ mod test {
]), ]),
]), ]),
])), ])),
candidates: candidates_2.clone(), candidates: Some(candidates_2.clone()),
bucket_candidates: candidates_2, bucket_candidates: candidates_2,
}; };
@ -434,7 +434,7 @@ mod test {
let expected = CriterionResult { let expected = CriterionResult {
query_tree: None, query_tree: None,
candidates: facet_candidates.clone(), candidates: Some(facet_candidates.clone()),
bucket_candidates: facet_candidates, bucket_candidates: facet_candidates,
}; };
@ -472,7 +472,7 @@ mod test {
Operation::Query(Query { prefix: false, kind: QueryKind::exact("world".to_string()) }), Operation::Query(Query { prefix: false, kind: QueryKind::exact("world".to_string()) }),
]), ]),
])), ])),
candidates: &candidates_1 & &facet_candidates, candidates: Some(&candidates_1 & &facet_candidates),
bucket_candidates: candidates_1 & &facet_candidates, bucket_candidates: candidates_1 & &facet_candidates,
}; };
@ -494,7 +494,7 @@ mod test {
]), ]),
]), ]),
])), ])),
candidates: &candidates_2 & &facet_candidates, candidates: Some(&candidates_2 & &facet_candidates),
bucket_candidates: candidates_2 & &facet_candidates, bucket_candidates: candidates_2 & &facet_candidates,
}; };

View File

@ -6,12 +6,12 @@ use roaring::RoaringBitmap;
use crate::search::query_tree::Operation; use crate::search::query_tree::Operation;
use crate::search::WordDerivationsCache; use crate::search::WordDerivationsCache;
use super::{resolve_query_tree, Candidates, Criterion, CriterionResult, Context}; use super::{resolve_query_tree, Criterion, CriterionResult, Context};
pub struct Words<'t> { pub struct Words<'t> {
ctx: &'t dyn Context, ctx: &'t dyn Context,
query_trees: Vec<Operation>, query_trees: Vec<Operation>,
candidates: Candidates, candidates: Option<RoaringBitmap>,
bucket_candidates: RoaringBitmap, bucket_candidates: RoaringBitmap,
parent: Option<Box<dyn Criterion + 't>>, parent: Option<Box<dyn Criterion + 't>>,
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>, candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
@ -27,7 +27,7 @@ impl<'t> Words<'t> {
Words { Words {
ctx, ctx,
query_trees: query_tree.map(explode_query_tree).unwrap_or_default(), query_trees: query_tree.map(explode_query_tree).unwrap_or_default(),
candidates: candidates.map_or_else(Candidates::default, Candidates::Allowed), candidates,
bucket_candidates: RoaringBitmap::new(), bucket_candidates: RoaringBitmap::new(),
parent: None, parent: None,
candidates_cache: HashMap::default(), candidates_cache: HashMap::default(),
@ -38,7 +38,7 @@ impl<'t> Words<'t> {
Words { Words {
ctx, ctx,
query_trees: Vec::default(), query_trees: Vec::default(),
candidates: Candidates::default(), candidates: None,
bucket_candidates: RoaringBitmap::new(), bucket_candidates: RoaringBitmap::new(),
parent: Some(parent), parent: Some(parent),
candidates_cache: HashMap::default(), candidates_cache: HashMap::default(),
@ -49,20 +49,19 @@ impl<'t> Words<'t> {
impl<'t> Criterion for Words<'t> { impl<'t> Criterion for Words<'t> {
#[logging_timer::time("Words::{}")] #[logging_timer::time("Words::{}")]
fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> { fn next(&mut self, wdcache: &mut WordDerivationsCache) -> anyhow::Result<Option<CriterionResult>> {
use Candidates::{Allowed, Forbidden};
loop { loop {
debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates); debug!("Words at iteration {} ({:?})", self.query_trees.len(), self.candidates);
match (self.query_trees.pop(), &mut self.candidates) { match (self.query_trees.pop(), &mut self.candidates) {
(query_tree, Allowed(candidates)) if candidates.is_empty() => { (query_tree, Some(candidates)) if candidates.is_empty() => {
self.query_trees = Vec::new(); self.query_trees = Vec::new();
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree, query_tree,
candidates: take(&mut self.candidates).into_inner(), candidates: self.candidates.take(),
bucket_candidates: take(&mut self.bucket_candidates), bucket_candidates: take(&mut self.bucket_candidates),
})); }));
}, },
(Some(qt), Allowed(candidates)) => { (Some(qt), Some(candidates)) => {
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?; let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?;
found_candidates.intersect_with(&candidates); found_candidates.intersect_with(&candidates);
candidates.difference_with(&found_candidates); candidates.difference_with(&found_candidates);
@ -74,41 +73,37 @@ impl<'t> Criterion for Words<'t> {
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(qt), query_tree: Some(qt),
candidates: found_candidates, candidates: Some(found_candidates),
bucket_candidates, bucket_candidates,
})); }));
}, },
(Some(qt), Forbidden(candidates)) => { (Some(qt), None) => {
let mut found_candidates = resolve_query_tree(self.ctx, &qt, &mut self.candidates_cache, wdcache)?;
found_candidates.difference_with(&candidates);
candidates.union_with(&found_candidates);
let bucket_candidates = match self.parent { let bucket_candidates = match self.parent {
Some(_) => take(&mut self.bucket_candidates), Some(_) => take(&mut self.bucket_candidates),
None => found_candidates.clone(), None => RoaringBitmap::new(),
}; };
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: Some(qt), query_tree: Some(qt),
candidates: found_candidates, candidates: None,
bucket_candidates, bucket_candidates,
})); }));
}, },
(None, Allowed(_)) => { (None, Some(_)) => {
let candidates = take(&mut self.candidates).into_inner(); let candidates = self.candidates.take();
return Ok(Some(CriterionResult { return Ok(Some(CriterionResult {
query_tree: None, query_tree: None,
candidates: candidates.clone(), candidates: candidates.clone(),
bucket_candidates: candidates, bucket_candidates: candidates.unwrap_or_default(),
})); }));
}, },
(None, Forbidden(_)) => { (None, None) => {
match self.parent.as_mut() { match self.parent.as_mut() {
Some(parent) => { Some(parent) => {
match parent.next(wdcache)? { match parent.next(wdcache)? {
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => { Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default(); self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
self.candidates = Candidates::Allowed(candidates); self.candidates = candidates;
self.bucket_candidates.union_with(&bucket_candidates); self.bucket_candidates.union_with(&bucket_candidates);
}, },
None => return Ok(None), None => return Ok(None),

View File

@ -11,7 +11,7 @@ use meilisearch_tokenizer::{AnalyzerConfig, Analyzer};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use roaring::bitmap::RoaringBitmap; use roaring::bitmap::RoaringBitmap;
use crate::search::criteria::{Criterion, CriterionResult}; use crate::search::criteria::fetcher::FetcherResult;
use crate::{Index, DocumentId}; use crate::{Index, DocumentId};
pub use self::facet::FacetIter; pub use self::facet::FacetIter;
@ -99,9 +99,8 @@ impl<'a> Search<'a> {
let mut offset = self.offset; let mut offset = self.offset;
let mut limit = self.limit; let mut limit = self.limit;
let mut documents_ids = Vec::new(); let mut documents_ids = Vec::new();
let mut words_derivations_cache = WordDerivationsCache::new();
let mut initial_candidates = RoaringBitmap::new(); let mut initial_candidates = RoaringBitmap::new();
while let Some(CriterionResult { candidates, bucket_candidates, .. }) = criteria.next(&mut words_derivations_cache)? { while let Some(FetcherResult { candidates, bucket_candidates, .. }) = criteria.next()? {
debug!("Number of candidates found {}", candidates.len()); debug!("Number of candidates found {}", candidates.len());