2020-11-20 10:54:41 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::fmt;
|
2020-12-02 11:36:38 +01:00
|
|
|
use std::time::Instant;
|
2020-11-20 10:54:41 +01:00
|
|
|
|
2020-12-23 19:09:01 +01:00
|
|
|
use fst::{IntoStreamer, Streamer, Set};
|
2021-02-24 17:44:35 +01:00
|
|
|
use levenshtein_automata::{DFA, LevenshteinAutomatonBuilder as LevBuilder};
|
2020-11-20 10:54:41 +01:00
|
|
|
use log::debug;
|
2020-12-23 19:09:01 +01:00
|
|
|
use meilisearch_tokenizer::{AnalyzerConfig, Analyzer};
|
2020-11-20 10:54:41 +01:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use roaring::bitmap::RoaringBitmap;
|
|
|
|
|
2021-02-17 17:34:22 +01:00
|
|
|
use crate::search::criteria::{Criterion, CriterionResult};
|
2021-02-25 16:34:29 +01:00
|
|
|
use crate::search::criteria::{typo::Typo, words::Words, proximity::Proximity, fetcher::Fetcher};
|
2021-02-19 15:45:15 +01:00
|
|
|
use crate::{Index, DocumentId};
|
2020-11-20 10:54:41 +01:00
|
|
|
|
2021-02-19 15:45:15 +01:00
|
|
|
pub use self::facet::FacetIter;
|
2021-02-24 17:44:35 +01:00
|
|
|
pub use self::facet::{FacetCondition, FacetDistribution, FacetNumberOperator, FacetStringOperator};
|
|
|
|
pub use self::query_tree::MatchingWords;
|
2021-02-17 10:29:28 +01:00
|
|
|
use self::query_tree::QueryTreeBuilder;
|
2020-11-20 10:54:41 +01: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 12:03:31 +01:00
|
|
|
mod query_tree;
|
2021-02-17 10:29:28 +01:00
|
|
|
mod criteria;
|
2020-11-20 10:54:41 +01:00
|
|
|
|
|
|
|
pub struct Search<'a> {
|
|
|
|
query: Option<String>,
|
|
|
|
facet_condition: Option<FacetCondition>,
|
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
|
|
|
rtxn: &'a heed::RoTxn<'a>,
|
|
|
|
index: &'a Index,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Search<'a> {
|
|
|
|
pub fn new(rtxn: &'a heed::RoTxn, index: &'a Index) -> Search<'a> {
|
|
|
|
Search { query: None, facet_condition: None, offset: 0, limit: 20, rtxn, index }
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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 10:29:28 +01: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-03 12:12:35 +01:00
|
|
|
let builder = QueryTreeBuilder::new(self.rtxn, self.index);
|
2021-02-17 10:29:28 +01:00
|
|
|
let stop_words = &Set::default();
|
|
|
|
let analyzer = Analyzer::new(AnalyzerConfig::default_with_stopwords(stop_words));
|
|
|
|
let result = analyzer.analyze(query);
|
|
|
|
let tokens = result.tokens();
|
2021-03-03 12:12:35 +01:00
|
|
|
builder.build(tokens)?
|
2021-02-17 10:29:28 +01:00
|
|
|
},
|
|
|
|
None => None,
|
2020-11-20 10:54:41 +01:00
|
|
|
};
|
|
|
|
|
2021-02-17 10:29:28 +01:00
|
|
|
debug!("query tree: {:?} took {:.02?}", query_tree, before.elapsed());
|
|
|
|
|
2020-11-20 10:54:41 +01:00
|
|
|
// We create the original candidates with the facet conditions results.
|
2020-12-02 11:36:38 +01:00
|
|
|
let before = Instant::now();
|
2020-11-20 12:59:29 +01:00
|
|
|
let facet_candidates = match &self.facet_condition {
|
2020-11-21 13:09:49 +01:00
|
|
|
Some(condition) => Some(condition.evaluate(self.rtxn, self.index)?),
|
2020-11-20 10:54:41 +01:00
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2020-12-02 11:36:38 +01:00
|
|
|
debug!("facet candidates: {:?} took {:.02?}", facet_candidates, before.elapsed());
|
|
|
|
|
2021-02-24 17:44:35 +01:00
|
|
|
let matching_words = match query_tree.as_ref() {
|
|
|
|
Some(query_tree) => MatchingWords::from_query_tree(&query_tree),
|
|
|
|
None => MatchingWords::default(),
|
|
|
|
};
|
|
|
|
|
2021-02-17 15:27:35 +01:00
|
|
|
let criteria_ctx = criteria::HeedContext::new(self.rtxn, self.index)?;
|
2021-02-18 17:22:43 +01:00
|
|
|
let typo_criterion = Typo::initial(&criteria_ctx, query_tree, facet_candidates)?;
|
2021-02-19 15:45:15 +01:00
|
|
|
let words_criterion = Words::new(&criteria_ctx, Box::new(typo_criterion))?;
|
2021-02-22 17:17:01 +01:00
|
|
|
let proximity_criterion = Proximity::new(&criteria_ctx, Box::new(words_criterion))?;
|
2021-02-25 16:34:29 +01:00
|
|
|
let fetcher_criterion = Fetcher::new(&criteria_ctx, Box::new(proximity_criterion));
|
|
|
|
let mut criteria = fetcher_criterion;
|
2021-02-22 17:17:01 +01:00
|
|
|
|
|
|
|
// // We sort in descending order on a specific field *by hand*, don't do that at home.
|
|
|
|
// let attr_name = "released-timestamp";
|
|
|
|
// let fid = self.index.fields_ids_map(self.rtxn)?.id(attr_name).unwrap();
|
|
|
|
// let ftype = *self.index.faceted_fields(self.rtxn)?.get(attr_name).unwrap();
|
|
|
|
// let desc_criterion = AscDesc::desc(self.index, self.rtxn, Box::new(words_criterion), fid, ftype)?;
|
2020-11-27 13:40:21 +01:00
|
|
|
|
2021-02-17 10:29:28 +01:00
|
|
|
let mut offset = self.offset;
|
|
|
|
let mut limit = self.limit;
|
|
|
|
let mut documents_ids = Vec::new();
|
2021-02-17 17:50:46 +01:00
|
|
|
let mut initial_candidates = RoaringBitmap::new();
|
|
|
|
while let Some(CriterionResult { candidates, bucket_candidates, .. }) = criteria.next()? {
|
2021-02-17 10:29:28 +01:00
|
|
|
|
2021-02-24 15:37:37 +01:00
|
|
|
debug!("Number of candidates found {}", candidates.len());
|
|
|
|
|
2021-02-17 17:50:46 +01:00
|
|
|
let mut len = candidates.len() as usize;
|
|
|
|
let mut candidates = candidates.into_iter();
|
|
|
|
|
2021-02-25 16:14:38 +01:00
|
|
|
initial_candidates.union_with(&bucket_candidates);
|
2020-11-20 10:54:41 +01:00
|
|
|
|
2021-02-17 10:29:28 +01:00
|
|
|
if offset != 0 {
|
2021-02-17 17:50:46 +01:00
|
|
|
candidates.by_ref().skip(offset).for_each(drop);
|
2021-02-17 10:29:28 +01:00
|
|
|
offset = offset.saturating_sub(len.min(offset));
|
|
|
|
len = len.saturating_sub(len.min(offset));
|
2020-11-20 10:54:41 +01:00
|
|
|
}
|
|
|
|
|
2021-02-17 10:29:28 +01:00
|
|
|
if len != 0 {
|
2021-02-17 17:50:46 +01:00
|
|
|
documents_ids.extend(candidates.take(limit));
|
2021-02-17 10:29:28 +01:00
|
|
|
limit = limit.saturating_sub(len.min(limit));
|
|
|
|
}
|
2020-11-27 14:52:53 +01:00
|
|
|
|
2021-02-17 10:29:28 +01:00
|
|
|
if limit == 0 { break }
|
|
|
|
}
|
|
|
|
|
2021-02-24 17:44:35 +01:00
|
|
|
Ok(SearchResult { matching_words, candidates: initial_candidates, documents_ids })
|
2020-11-20 10:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Search<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2020-11-22 15:40:11 +01:00
|
|
|
let Search { query, facet_condition, offset, limit, rtxn: _, index: _ } = self;
|
2020-11-20 10:54:41 +01:00
|
|
|
f.debug_struct("Search")
|
2020-11-22 15:40:11 +01:00
|
|
|
.field("query", query)
|
|
|
|
.field("facet_condition", facet_condition)
|
|
|
|
.field("offset", offset)
|
|
|
|
.field("limit", limit)
|
2020-11-20 10:54:41 +01:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct SearchResult {
|
2021-02-24 17:44:35 +01:00
|
|
|
pub matching_words: MatchingWords,
|
2020-12-28 19:08:53 +01:00
|
|
|
pub candidates: RoaringBitmap,
|
2020-11-20 10:54:41 +01:00
|
|
|
// TODO those documents ids should be associated with their criteria scores.
|
|
|
|
pub documents_ids: Vec<DocumentId>,
|
|
|
|
}
|
2021-03-03 12:03:31 +01:00
|
|
|
|
2021-02-24 17:44:35 +01:00
|
|
|
pub fn word_derivations(
|
|
|
|
word: &str,
|
|
|
|
is_prefix: bool,
|
|
|
|
max_typo: u8,
|
|
|
|
fst: &fst::Set<Cow<[u8]>>,
|
|
|
|
) -> anyhow::Result<Vec<(String, u8)>>
|
|
|
|
{
|
2021-03-03 12:03:31 +01:00
|
|
|
let mut derived_words = Vec::new();
|
2021-02-24 17:44:35 +01:00
|
|
|
let dfa = build_dfa(word, max_typo, is_prefix);
|
2021-03-03 12:03:31 +01:00
|
|
|
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()));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(derived_words)
|
|
|
|
}
|
2021-02-24 17:44:35 +01: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)
|
|
|
|
}
|
|
|
|
}
|