2020-11-20 17:54:41 +08:00
|
|
|
use std::borrow::Cow;
|
2021-03-05 18:02:24 +08:00
|
|
|
use std::collections::hash_map::{HashMap, Entry};
|
2020-11-20 17:54:41 +08:00
|
|
|
use std::fmt;
|
2021-03-05 18:02:24 +08:00
|
|
|
use std::str::Utf8Error;
|
2020-12-02 18:36:38 +08:00
|
|
|
use std::time::Instant;
|
2020-11-20 17:54:41 +08:00
|
|
|
|
2021-03-30 01:15:47 +08:00
|
|
|
use fst::{IntoStreamer, Streamer};
|
2021-02-25 00:44:35 +08:00
|
|
|
use levenshtein_automata::{DFA, LevenshteinAutomatonBuilder as LevBuilder};
|
2020-11-20 17:54:41 +08:00
|
|
|
use log::debug;
|
2020-12-24 02:09:01 +08:00
|
|
|
use meilisearch_tokenizer::{AnalyzerConfig, Analyzer};
|
2020-11-20 17:54:41 +08:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use roaring::bitmap::RoaringBitmap;
|
|
|
|
|
2021-03-09 19:04:52 +08:00
|
|
|
use crate::search::criteria::fetcher::FetcherResult;
|
2021-02-19 22:45:15 +08:00
|
|
|
use crate::{Index, DocumentId};
|
2020-11-20 17:54:41 +08:00
|
|
|
|
2021-02-19 22:45:15 +08:00
|
|
|
pub use self::facet::FacetIter;
|
2021-02-25 00:44:35 +08:00
|
|
|
pub use self::facet::{FacetCondition, FacetDistribution, FacetNumberOperator, FacetStringOperator};
|
|
|
|
pub use self::query_tree::MatchingWords;
|
2021-02-17 17:29:28 +08:00
|
|
|
use self::query_tree::QueryTreeBuilder;
|
2020-11-20 17:54:41 +08:00
|
|
|
|
|
|
|
// Building these factories is not free.
|
|
|
|
static LEVDIST0: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(0, true));
|
|
|
|
static LEVDIST1: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(1, true));
|
|
|
|
static LEVDIST2: Lazy<LevBuilder> = Lazy::new(|| LevBuilder::new(2, true));
|
|
|
|
|
|
|
|
mod facet;
|
2021-03-03 19:03:31 +08:00
|
|
|
mod query_tree;
|
2021-02-17 17:29:28 +08:00
|
|
|
mod criteria;
|
2020-11-20 17:54:41 +08:00
|
|
|
|
|
|
|
pub struct Search<'a> {
|
|
|
|
query: Option<String>,
|
|
|
|
facet_condition: Option<FacetCondition>,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
2021-03-10 18:16:30 +08:00
|
|
|
optional_words: bool,
|
|
|
|
authorize_typos: bool,
|
2020-11-20 17:54:41 +08:00
|
|
|
rtxn: &'a heed::RoTxn<'a>,
|
|
|
|
index: &'a Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Search<'a> {
|
|
|
|
pub fn new(rtxn: &'a heed::RoTxn, index: &'a Index) -> Search<'a> {
|
2021-03-10 18:16:30 +08:00
|
|
|
Search {
|
|
|
|
query: None,
|
|
|
|
facet_condition: None,
|
|
|
|
offset: 0,
|
|
|
|
limit: 20,
|
|
|
|
optional_words: true,
|
|
|
|
authorize_typos: true,
|
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2020-11-20 17:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn query(&mut self, query: impl Into<String>) -> &mut Search<'a> {
|
|
|
|
self.query = Some(query.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn offset(&mut self, offset: usize) -> &mut Search<'a> {
|
|
|
|
self.offset = offset;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn limit(&mut self, limit: usize) -> &mut Search<'a> {
|
|
|
|
self.limit = limit;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-03-10 18:16:30 +08:00
|
|
|
pub fn optional_words(&mut self, value: bool) -> &mut Search<'a> {
|
|
|
|
self.optional_words = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn authorize_typos(&mut self, value: bool) -> &mut Search<'a> {
|
|
|
|
self.authorize_typos = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:54:41 +08:00
|
|
|
pub fn facet_condition(&mut self, condition: FacetCondition) -> &mut Search<'a> {
|
|
|
|
self.facet_condition = Some(condition);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn execute(&self) -> anyhow::Result<SearchResult> {
|
2021-02-17 17:29:28 +08:00
|
|
|
// We create the query tree by spliting the query into tokens.
|
|
|
|
let before = Instant::now();
|
|
|
|
let query_tree = match self.query.as_ref() {
|
|
|
|
Some(query) => {
|
2021-03-10 18:16:30 +08:00
|
|
|
let mut builder = QueryTreeBuilder::new(self.rtxn, self.index);
|
|
|
|
builder.optional_words(self.optional_words);
|
|
|
|
builder.authorize_typos(self.authorize_typos);
|
2021-03-30 01:15:47 +08:00
|
|
|
let analyzer = Analyzer::<Vec<u8>>::new(AnalyzerConfig::default());
|
2021-02-17 17:29:28 +08:00
|
|
|
let result = analyzer.analyze(query);
|
|
|
|
let tokens = result.tokens();
|
2021-03-03 19:12:35 +08:00
|
|
|
builder.build(tokens)?
|
2021-02-17 17:29:28 +08:00
|
|
|
},
|
|
|
|
None => None,
|
2020-11-20 17:54:41 +08:00
|
|
|
};
|
|
|
|
|
2021-02-17 17:29:28 +08:00
|
|
|
debug!("query tree: {:?} took {:.02?}", query_tree, before.elapsed());
|
|
|
|
|
2020-11-20 17:54:41 +08:00
|
|
|
// We create the original candidates with the facet conditions results.
|
2020-12-02 18:36:38 +08:00
|
|
|
let before = Instant::now();
|
2020-11-20 19:59:29 +08:00
|
|
|
let facet_candidates = match &self.facet_condition {
|
2020-11-21 20:09:49 +08:00
|
|
|
Some(condition) => Some(condition.evaluate(self.rtxn, self.index)?),
|
2020-11-20 17:54:41 +08:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2020-12-02 18:36:38 +08:00
|
|
|
debug!("facet candidates: {:?} took {:.02?}", facet_candidates, before.elapsed());
|
|
|
|
|
2021-02-25 00:44:35 +08:00
|
|
|
let matching_words = match query_tree.as_ref() {
|
|
|
|
Some(query_tree) => MatchingWords::from_query_tree(&query_tree),
|
|
|
|
None => MatchingWords::default(),
|
|
|
|
};
|
|
|
|
|
2021-03-02 18:58:32 +08:00
|
|
|
let criteria_builder = criteria::CriteriaBuilder::new(self.rtxn, self.index)?;
|
|
|
|
let mut criteria = criteria_builder.build(query_tree, facet_candidates)?;
|
2021-02-23 00:17:01 +08:00
|
|
|
|
2021-02-17 17:29:28 +08:00
|
|
|
let mut offset = self.offset;
|
|
|
|
let mut limit = self.limit;
|
|
|
|
let mut documents_ids = Vec::new();
|
2021-02-18 00:50:46 +08:00
|
|
|
let mut initial_candidates = RoaringBitmap::new();
|
2021-03-09 19:04:52 +08:00
|
|
|
while let Some(FetcherResult { candidates, bucket_candidates, .. }) = criteria.next()? {
|
2021-02-17 17:29:28 +08:00
|
|
|
|
2021-02-24 22:37:37 +08:00
|
|
|
debug!("Number of candidates found {}", candidates.len());
|
|
|
|
|
2021-02-18 00:50:46 +08:00
|
|
|
let mut len = candidates.len() as usize;
|
|
|
|
let mut candidates = candidates.into_iter();
|
|
|
|
|
2021-02-25 23:14:38 +08:00
|
|
|
initial_candidates.union_with(&bucket_candidates);
|
2020-11-20 17:54:41 +08:00
|
|
|
|
2021-02-17 17:29:28 +08:00
|
|
|
if offset != 0 {
|
2021-03-16 03:23:50 +08:00
|
|
|
candidates.by_ref().take(offset).for_each(drop);
|
2021-02-17 17:29:28 +08:00
|
|
|
offset = offset.saturating_sub(len.min(offset));
|
|
|
|
len = len.saturating_sub(len.min(offset));
|
2020-11-20 17:54:41 +08:00
|
|
|
}
|
|
|
|
|
2021-02-17 17:29:28 +08:00
|
|
|
if len != 0 {
|
2021-02-18 00:50:46 +08:00
|
|
|
documents_ids.extend(candidates.take(limit));
|
2021-02-17 17:29:28 +08:00
|
|
|
limit = limit.saturating_sub(len.min(limit));
|
|
|
|
}
|
2020-11-27 21:52:53 +08:00
|
|
|
|
2021-02-17 17:29:28 +08:00
|
|
|
if limit == 0 { break }
|
|
|
|
}
|
|
|
|
|
2021-02-25 00:44:35 +08:00
|
|
|
Ok(SearchResult { matching_words, candidates: initial_candidates, documents_ids })
|
2020-11-20 17:54:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Search<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-03-10 18:16:30 +08:00
|
|
|
let Search {
|
|
|
|
query,
|
|
|
|
facet_condition,
|
|
|
|
offset,
|
|
|
|
limit,
|
|
|
|
optional_words,
|
|
|
|
authorize_typos,
|
|
|
|
rtxn: _,
|
|
|
|
index: _,
|
|
|
|
} = self;
|
2020-11-20 17:54:41 +08:00
|
|
|
f.debug_struct("Search")
|
2020-11-22 22:40:11 +08:00
|
|
|
.field("query", query)
|
|
|
|
.field("facet_condition", facet_condition)
|
|
|
|
.field("offset", offset)
|
|
|
|
.field("limit", limit)
|
2021-03-10 18:16:30 +08:00
|
|
|
.field("optional_words", optional_words)
|
|
|
|
.field("authorize_typos", authorize_typos)
|
2020-11-20 17:54:41 +08:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SearchResult {
|
2021-02-25 00:44:35 +08:00
|
|
|
pub matching_words: MatchingWords,
|
2020-12-29 02:08:53 +08:00
|
|
|
pub candidates: RoaringBitmap,
|
2020-11-20 17:54:41 +08:00
|
|
|
// TODO those documents ids should be associated with their criteria scores.
|
|
|
|
pub documents_ids: Vec<DocumentId>,
|
|
|
|
}
|
2021-03-03 19:03:31 +08:00
|
|
|
|
2021-03-05 18:02:24 +08:00
|
|
|
pub type WordDerivationsCache = HashMap<(String, bool, u8), Vec<(String, u8)>>;
|
|
|
|
|
|
|
|
pub fn word_derivations<'c>(
|
2021-02-25 00:44:35 +08:00
|
|
|
word: &str,
|
|
|
|
is_prefix: bool,
|
|
|
|
max_typo: u8,
|
|
|
|
fst: &fst::Set<Cow<[u8]>>,
|
2021-03-05 18:02:24 +08:00
|
|
|
cache: &'c mut WordDerivationsCache,
|
|
|
|
) -> Result<&'c [(String, u8)], Utf8Error>
|
2021-02-25 00:44:35 +08:00
|
|
|
{
|
2021-03-05 18:02:24 +08:00
|
|
|
match cache.entry((word.to_string(), is_prefix, max_typo)) {
|
|
|
|
Entry::Occupied(entry) => Ok(entry.into_mut()),
|
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
let mut derived_words = Vec::new();
|
|
|
|
let dfa = build_dfa(word, max_typo, is_prefix);
|
|
|
|
let mut stream = fst.search_with_state(&dfa).into_stream();
|
|
|
|
|
|
|
|
while let Some((word, state)) = stream.next() {
|
|
|
|
let word = std::str::from_utf8(word)?;
|
|
|
|
let distance = dfa.distance(state);
|
|
|
|
derived_words.push((word.to_string(), distance.to_u8()));
|
|
|
|
}
|
2021-03-03 19:03:31 +08:00
|
|
|
|
2021-03-05 18:02:24 +08:00
|
|
|
Ok(entry.insert(derived_words))
|
|
|
|
},
|
|
|
|
}
|
2021-03-03 19:03:31 +08:00
|
|
|
}
|
2021-02-25 00:44:35 +08:00
|
|
|
|
|
|
|
pub fn build_dfa(word: &str, typos: u8, is_prefix: bool) -> DFA {
|
|
|
|
let lev = match typos {
|
|
|
|
0 => &LEVDIST0,
|
|
|
|
1 => &LEVDIST1,
|
|
|
|
_ => &LEVDIST2,
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_prefix {
|
|
|
|
lev.build_prefix_dfa(word)
|
|
|
|
} else {
|
|
|
|
lev.build_dfa(word)
|
|
|
|
}
|
|
|
|
}
|