mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-23 21:20:24 +01:00
Introduce the Words criterion
This commit is contained in:
parent
2d068bd45b
commit
1e47f9b3ff
@ -8,6 +8,7 @@ use roaring::RoaringBitmap;
|
||||
use super::query_tree::{Operation, Query, QueryKind};
|
||||
|
||||
pub mod typo;
|
||||
pub mod words;
|
||||
|
||||
pub trait Criterion {
|
||||
fn next(&mut self) -> anyhow::Result<Option<CriterionResult>>;
|
||||
|
@ -13,7 +13,7 @@ pub struct Typo<'t> {
|
||||
number_typos: u8,
|
||||
candidates: Candidates,
|
||||
bucket_candidates: RoaringBitmap,
|
||||
parent: Option<Box<dyn Criterion>>,
|
||||
parent: Option<Box<dyn Criterion + 't>>,
|
||||
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
|
||||
typo_cache: HashMap<(String, bool, u8), Vec<(String, u8)>>,
|
||||
}
|
||||
@ -39,7 +39,7 @@ impl<'t> Typo<'t> {
|
||||
|
||||
pub fn new(
|
||||
ctx: &'t dyn Context,
|
||||
parent: Box<dyn Criterion>,
|
||||
parent: Box<dyn Criterion + 't>,
|
||||
) -> anyhow::Result<Self> where Self: Sized
|
||||
{
|
||||
Ok(Typo {
|
||||
|
109
milli/src/search/criteria/words.rs
Normal file
109
milli/src/search/criteria/words.rs
Normal file
@ -0,0 +1,109 @@
|
||||
use std::collections::HashMap;
|
||||
use std::mem::take;
|
||||
|
||||
use roaring::RoaringBitmap;
|
||||
|
||||
use crate::search::query_tree::Operation;
|
||||
use super::{Candidates, Criterion, CriterionResult, Context};
|
||||
|
||||
pub struct Words<'t> {
|
||||
ctx: &'t dyn Context,
|
||||
query_trees: Vec<Operation>,
|
||||
candidates: Candidates,
|
||||
bucket_candidates: RoaringBitmap,
|
||||
parent: Option<Box<dyn Criterion + 't>>,
|
||||
candidates_cache: HashMap<(Operation, u8), RoaringBitmap>,
|
||||
}
|
||||
|
||||
impl<'t> Words<'t> {
|
||||
pub fn initial(
|
||||
ctx: &'t dyn Context,
|
||||
query_tree: Option<Operation>,
|
||||
candidates: Option<RoaringBitmap>,
|
||||
) -> anyhow::Result<Self> where Self: Sized
|
||||
{
|
||||
Ok(Words {
|
||||
ctx,
|
||||
query_trees: query_tree.map(explode_query_tree).unwrap_or_default(),
|
||||
candidates: candidates.map_or_else(Candidates::default, Candidates::Allowed),
|
||||
bucket_candidates: RoaringBitmap::new(),
|
||||
parent: None,
|
||||
candidates_cache: HashMap::default(),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn new(
|
||||
ctx: &'t dyn Context,
|
||||
parent: Box<dyn Criterion + 't>,
|
||||
) -> anyhow::Result<Self> where Self: Sized
|
||||
{
|
||||
Ok(Words {
|
||||
ctx,
|
||||
query_trees: Vec::default(),
|
||||
candidates: Candidates::default(),
|
||||
bucket_candidates: RoaringBitmap::new(),
|
||||
parent: Some(parent),
|
||||
candidates_cache: HashMap::default(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<'t> Criterion for Words<'t> {
|
||||
fn next(&mut self) -> anyhow::Result<Option<CriterionResult>> {
|
||||
use Candidates::{Allowed, Forbidden};
|
||||
|
||||
loop {
|
||||
match (self.query_trees.pop(), &mut self.candidates) {
|
||||
(_, Allowed(candidates)) if candidates.is_empty() => {
|
||||
self.query_trees = Vec::new();
|
||||
self.candidates = Candidates::default();
|
||||
},
|
||||
(Some(qt), Allowed(candidates)) => {
|
||||
let bucket_candidates = match self.parent {
|
||||
Some(_) => take(&mut self.bucket_candidates),
|
||||
None => candidates.clone(),
|
||||
};
|
||||
|
||||
return Ok(Some(CriterionResult {
|
||||
query_tree: Some(qt),
|
||||
candidates: candidates.clone(),
|
||||
bucket_candidates,
|
||||
}));
|
||||
},
|
||||
(Some(_qt), Forbidden(candidates)) => {
|
||||
todo!()
|
||||
},
|
||||
(None, Allowed(_)) => {
|
||||
let candidates = take(&mut self.candidates).into_inner();
|
||||
return Ok(Some(CriterionResult {
|
||||
query_tree: None,
|
||||
candidates: candidates.clone(),
|
||||
bucket_candidates: candidates,
|
||||
}));
|
||||
},
|
||||
(None, Forbidden(_)) => {
|
||||
match self.parent.as_mut() {
|
||||
Some(parent) => {
|
||||
match parent.next()? {
|
||||
Some(CriterionResult { query_tree, candidates, bucket_candidates }) => {
|
||||
self.query_trees = query_tree.map(explode_query_tree).unwrap_or_default();
|
||||
self.candidates = Candidates::Allowed(candidates);
|
||||
self.bucket_candidates = bucket_candidates;
|
||||
},
|
||||
None => return Ok(None),
|
||||
}
|
||||
},
|
||||
None => return Ok(None),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn explode_query_tree(query_tree: Operation) -> Vec<Operation> {
|
||||
match query_tree {
|
||||
Operation::Or(true, ops) => ops,
|
||||
otherwise => vec![otherwise],
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user