2020-11-20 10:54:41 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::collections::{HashMap, HashSet};
|
|
|
|
use std::fmt;
|
2020-12-02 11:36:38 +01:00
|
|
|
use std::time::Instant;
|
2020-11-20 10:54:41 +01:00
|
|
|
|
2020-11-27 13:40:21 +01:00
|
|
|
use anyhow::{bail, Context};
|
2020-12-23 19:09:01 +01:00
|
|
|
use fst::{IntoStreamer, Streamer, Set};
|
2020-11-20 10:54:41 +01:00
|
|
|
use levenshtein_automata::DFA;
|
|
|
|
use levenshtein_automata::LevenshteinAutomatonBuilder as LevBuilder;
|
|
|
|
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;
|
2020-12-02 21:08:48 +01:00
|
|
|
use ordered_float::OrderedFloat;
|
2020-11-20 10:54:41 +01:00
|
|
|
use roaring::bitmap::RoaringBitmap;
|
|
|
|
|
2020-11-27 13:40:21 +01:00
|
|
|
use crate::facet::FacetType;
|
|
|
|
use crate::heed_codec::facet::{FacetLevelValueF64Codec, FacetLevelValueI64Codec};
|
2020-12-02 21:08:48 +01:00
|
|
|
use crate::heed_codec::facet::{FieldDocIdFacetF64Codec, FieldDocIdFacetI64Codec};
|
2020-11-20 10:54:41 +01:00
|
|
|
use crate::mdfs::Mdfs;
|
2020-12-23 19:09:01 +01:00
|
|
|
use crate::query_tokens::{query_tokens, QueryToken};
|
2021-02-17 17:34:22 +01:00
|
|
|
use crate::search::criteria::{Criterion, CriterionResult};
|
2021-02-17 10:29:28 +01:00
|
|
|
use crate::search::criteria::typo::Typo;
|
|
|
|
use crate::{Index, FieldId, DocumentId};
|
2020-11-20 10:54:41 +01:00
|
|
|
|
2020-12-28 19:08:53 +01:00
|
|
|
pub use self::facet::{FacetCondition, FacetDistribution, FacetNumberOperator, FacetStringOperator};
|
2020-11-28 14:52:50 +01:00
|
|
|
pub use self::facet::{FacetIter};
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Extracts the query words from the query string and returns the DFAs accordingly.
|
|
|
|
/// TODO introduce settings for the number of typos regarding the words lengths.
|
|
|
|
fn generate_query_dfas(query: &str) -> Vec<(String, bool, DFA)> {
|
|
|
|
let (lev0, lev1, lev2) = (&LEVDIST0, &LEVDIST1, &LEVDIST2);
|
|
|
|
|
2020-12-23 19:09:01 +01:00
|
|
|
let stop_words = Set::default();
|
|
|
|
let analyzer = Analyzer::new(AnalyzerConfig::default_with_stopwords(&stop_words));
|
|
|
|
let analyzed = analyzer.analyze(query);
|
|
|
|
let tokens = analyzed.tokens();
|
|
|
|
let words: Vec<_> = query_tokens(tokens).collect();
|
|
|
|
|
2020-11-20 10:54:41 +01:00
|
|
|
let ends_with_whitespace = query.chars().last().map_or(false, char::is_whitespace);
|
|
|
|
let number_of_words = words.len();
|
|
|
|
|
|
|
|
words.into_iter().enumerate().map(|(i, word)| {
|
|
|
|
let (word, quoted) = match word {
|
2020-12-23 19:09:01 +01:00
|
|
|
QueryToken::Free(token) => (token.text().to_string(), token.text().len() <= 3),
|
|
|
|
QueryToken::Quoted(token) => (token.text().to_string(), true),
|
2020-11-20 10:54:41 +01:00
|
|
|
};
|
|
|
|
let is_last = i + 1 == number_of_words;
|
|
|
|
let is_prefix = is_last && !ends_with_whitespace && !quoted;
|
|
|
|
let lev = match word.len() {
|
|
|
|
0..=4 => if quoted { lev0 } else { lev0 },
|
|
|
|
5..=8 => if quoted { lev0 } else { lev1 },
|
|
|
|
_ => if quoted { lev0 } else { lev2 },
|
|
|
|
};
|
|
|
|
|
|
|
|
let dfa = if is_prefix {
|
|
|
|
lev.build_prefix_dfa(&word)
|
|
|
|
} else {
|
|
|
|
lev.build_dfa(&word)
|
|
|
|
};
|
|
|
|
|
|
|
|
(word, is_prefix, dfa)
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Fetch the words from the given FST related to the given DFAs along with
|
|
|
|
/// the associated documents ids.
|
|
|
|
fn fetch_words_docids(
|
|
|
|
&self,
|
|
|
|
fst: &fst::Set<Cow<[u8]>>,
|
|
|
|
dfas: Vec<(String, bool, DFA)>,
|
|
|
|
) -> anyhow::Result<Vec<(HashMap<String, (u8, RoaringBitmap)>, RoaringBitmap)>>
|
|
|
|
{
|
|
|
|
// A Vec storing all the derived words from the original query words, associated
|
|
|
|
// with the distance from the original word and the docids where the words appears.
|
|
|
|
let mut derived_words = Vec::<(HashMap::<String, (u8, RoaringBitmap)>, RoaringBitmap)>::with_capacity(dfas.len());
|
|
|
|
|
|
|
|
for (_word, _is_prefix, dfa) in dfas {
|
|
|
|
|
|
|
|
let mut acc_derived_words = HashMap::new();
|
|
|
|
let mut unions_docids = RoaringBitmap::new();
|
|
|
|
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 docids = self.index.word_docids.get(self.rtxn, word)?.unwrap();
|
|
|
|
let distance = dfa.distance(state);
|
|
|
|
unions_docids.union_with(&docids);
|
|
|
|
acc_derived_words.insert(word.to_string(), (distance.to_u8(), docids));
|
|
|
|
}
|
|
|
|
derived_words.push((acc_derived_words, unions_docids));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(derived_words)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the set of docids that contains all of the query words.
|
|
|
|
fn compute_candidates(
|
|
|
|
derived_words: &[(HashMap<String, (u8, RoaringBitmap)>, RoaringBitmap)],
|
|
|
|
) -> RoaringBitmap
|
|
|
|
{
|
|
|
|
// We sort the derived words by inverse popularity, this way intersections are faster.
|
|
|
|
let mut derived_words: Vec<_> = derived_words.iter().collect();
|
|
|
|
derived_words.sort_unstable_by_key(|(_, docids)| docids.len());
|
|
|
|
|
|
|
|
// we do a union between all the docids of each of the derived words,
|
|
|
|
// we got N unions (the number of original query words), we then intersect them.
|
|
|
|
let mut candidates = RoaringBitmap::new();
|
|
|
|
|
|
|
|
for (i, (_, union_docids)) in derived_words.iter().enumerate() {
|
|
|
|
if i == 0 {
|
|
|
|
candidates = union_docids.clone();
|
|
|
|
} else {
|
|
|
|
candidates.intersect_with(&union_docids);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
candidates
|
|
|
|
}
|
|
|
|
|
2020-11-27 13:40:21 +01:00
|
|
|
fn facet_ordered(
|
|
|
|
&self,
|
|
|
|
field_id: FieldId,
|
|
|
|
facet_type: FacetType,
|
2020-11-28 14:52:50 +01:00
|
|
|
ascending: bool,
|
2020-12-17 13:57:07 +01:00
|
|
|
mut documents_ids: RoaringBitmap,
|
2020-11-27 13:40:21 +01:00
|
|
|
limit: usize,
|
|
|
|
) -> anyhow::Result<Vec<DocumentId>>
|
|
|
|
{
|
2020-12-17 13:57:07 +01:00
|
|
|
let mut output: Vec<_> = match facet_type {
|
2020-11-27 13:40:21 +01:00
|
|
|
FacetType::Float => {
|
2020-12-02 21:08:48 +01:00
|
|
|
if documents_ids.len() <= 1000 {
|
|
|
|
let db = self.index.field_id_docid_facet_values.remap_key_type::<FieldDocIdFacetF64Codec>();
|
|
|
|
let mut docids_values = Vec::with_capacity(documents_ids.len() as usize);
|
2020-12-17 13:57:07 +01:00
|
|
|
for docid in documents_ids.iter() {
|
2020-12-02 21:08:48 +01:00
|
|
|
let left = (field_id, docid, f64::MIN);
|
|
|
|
let right = (field_id, docid, f64::MAX);
|
|
|
|
let mut iter = db.range(self.rtxn, &(left..=right))?;
|
|
|
|
let entry = if ascending { iter.next() } else { iter.last() };
|
|
|
|
if let Some(((_, _, value), ())) = entry.transpose()? {
|
|
|
|
docids_values.push((docid, OrderedFloat(value)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
docids_values.sort_unstable_by_key(|(_, value)| *value);
|
2020-12-12 14:21:27 +01:00
|
|
|
let iter = docids_values.into_iter().map(|(id, _)| id);
|
|
|
|
if ascending {
|
2020-12-17 13:57:07 +01:00
|
|
|
iter.take(limit).collect()
|
2020-12-12 14:21:27 +01:00
|
|
|
} else {
|
2020-12-17 13:57:07 +01:00
|
|
|
iter.rev().take(limit).collect()
|
2020-12-12 14:21:27 +01:00
|
|
|
}
|
2020-11-28 14:52:50 +01:00
|
|
|
} else {
|
2020-12-02 21:08:48 +01:00
|
|
|
let facet_fn = if ascending {
|
2021-01-06 15:10:30 +01:00
|
|
|
FacetIter::<f64, FacetLevelValueF64Codec>::new_reducing
|
2020-12-02 21:08:48 +01:00
|
|
|
} else {
|
2021-01-06 15:10:30 +01:00
|
|
|
FacetIter::<f64, FacetLevelValueF64Codec>::new_reverse_reducing
|
2020-12-02 21:08:48 +01:00
|
|
|
};
|
2020-12-17 13:57:07 +01:00
|
|
|
let mut limit_tmp = limit;
|
|
|
|
let mut output = Vec::new();
|
|
|
|
for result in facet_fn(self.rtxn, self.index, field_id, documents_ids.clone())? {
|
2020-12-02 21:08:48 +01:00
|
|
|
let (_val, docids) = result?;
|
|
|
|
limit_tmp = limit_tmp.saturating_sub(docids.len() as usize);
|
|
|
|
output.push(docids);
|
|
|
|
if limit_tmp == 0 { break }
|
|
|
|
}
|
2020-12-17 13:57:07 +01:00
|
|
|
output.into_iter().flatten().take(limit).collect()
|
2020-11-28 14:52:50 +01:00
|
|
|
}
|
2020-11-27 13:40:21 +01:00
|
|
|
},
|
|
|
|
FacetType::Integer => {
|
2020-12-02 21:08:48 +01:00
|
|
|
if documents_ids.len() <= 1000 {
|
|
|
|
let db = self.index.field_id_docid_facet_values.remap_key_type::<FieldDocIdFacetI64Codec>();
|
|
|
|
let mut docids_values = Vec::with_capacity(documents_ids.len() as usize);
|
2020-12-17 13:57:07 +01:00
|
|
|
for docid in documents_ids.iter() {
|
2020-12-02 21:08:48 +01:00
|
|
|
let left = (field_id, docid, i64::MIN);
|
|
|
|
let right = (field_id, docid, i64::MAX);
|
|
|
|
let mut iter = db.range(self.rtxn, &(left..=right))?;
|
|
|
|
let entry = if ascending { iter.next() } else { iter.last() };
|
|
|
|
if let Some(((_, _, value), ())) = entry.transpose()? {
|
|
|
|
docids_values.push((docid, value));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
docids_values.sort_unstable_by_key(|(_, value)| *value);
|
2020-12-12 14:21:27 +01:00
|
|
|
let iter = docids_values.into_iter().map(|(id, _)| id);
|
|
|
|
if ascending {
|
2020-12-17 13:57:07 +01:00
|
|
|
iter.take(limit).collect()
|
2020-12-12 14:21:27 +01:00
|
|
|
} else {
|
2020-12-17 13:57:07 +01:00
|
|
|
iter.rev().take(limit).collect()
|
2020-12-12 14:21:27 +01:00
|
|
|
}
|
2020-11-28 14:52:50 +01:00
|
|
|
} else {
|
2020-12-02 21:08:48 +01:00
|
|
|
let facet_fn = if ascending {
|
2021-01-06 15:10:30 +01:00
|
|
|
FacetIter::<i64, FacetLevelValueI64Codec>::new_reducing
|
2020-12-02 21:08:48 +01:00
|
|
|
} else {
|
2021-01-06 15:10:30 +01:00
|
|
|
FacetIter::<i64, FacetLevelValueI64Codec>::new_reverse_reducing
|
2020-12-02 21:08:48 +01:00
|
|
|
};
|
2020-12-17 13:57:07 +01:00
|
|
|
let mut limit_tmp = limit;
|
|
|
|
let mut output = Vec::new();
|
|
|
|
for result in facet_fn(self.rtxn, self.index, field_id, documents_ids.clone())? {
|
2020-12-02 21:08:48 +01:00
|
|
|
let (_val, docids) = result?;
|
|
|
|
limit_tmp = limit_tmp.saturating_sub(docids.len() as usize);
|
|
|
|
output.push(docids);
|
|
|
|
if limit_tmp == 0 { break }
|
|
|
|
}
|
2020-12-17 13:57:07 +01:00
|
|
|
output.into_iter().flatten().take(limit).collect()
|
2020-11-28 14:52:50 +01:00
|
|
|
}
|
2020-11-27 13:40:21 +01:00
|
|
|
},
|
|
|
|
FacetType::String => bail!("criteria facet type must be a number"),
|
2020-12-17 13:57:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// if there isn't enough documents to return we try to complete that list
|
|
|
|
// with documents that are maybe not faceted under this field and therefore
|
|
|
|
// not returned by the previous facet iteration.
|
|
|
|
if output.len() < limit {
|
|
|
|
output.iter().for_each(|n| { documents_ids.remove(*n); });
|
|
|
|
let remaining = documents_ids.iter().take(limit - output.len());
|
|
|
|
output.extend(remaining);
|
2020-11-27 13:40:21 +01:00
|
|
|
}
|
2020-12-17 13:57:07 +01:00
|
|
|
|
|
|
|
Ok(output)
|
2020-11-27 13:40:21 +01:00
|
|
|
}
|
|
|
|
|
2020-11-20 10:54:41 +01:00
|
|
|
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) => {
|
|
|
|
let builder = QueryTreeBuilder::new(self.rtxn, self.index);
|
|
|
|
let stop_words = &Set::default();
|
|
|
|
let analyzer = Analyzer::new(AnalyzerConfig::default_with_stopwords(stop_words));
|
|
|
|
let result = analyzer.analyze(query);
|
|
|
|
let tokens = result.tokens();
|
|
|
|
builder.build(false, true, tokens)
|
|
|
|
},
|
|
|
|
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-17 10:29:28 +01:00
|
|
|
// We aretesting the typo criteria but there will be more of them soon.
|
2021-02-17 15:27:35 +01:00
|
|
|
let criteria_ctx = criteria::HeedContext::new(self.rtxn, self.index)?;
|
|
|
|
let mut criteria = Typo::initial(&criteria_ctx, query_tree, facet_candidates)?;
|
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-17 17:50:46 +01:00
|
|
|
let mut len = candidates.len() as usize;
|
|
|
|
let mut candidates = candidates.into_iter();
|
|
|
|
|
|
|
|
if let Some(docids) = bucket_candidates {
|
|
|
|
initial_candidates.union_with(&docids);
|
|
|
|
}
|
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 }
|
|
|
|
}
|
|
|
|
|
|
|
|
let found_words = HashSet::new();
|
2021-02-17 17:50:46 +01:00
|
|
|
Ok(SearchResult { found_words, candidates: initial_candidates, documents_ids })
|
2021-02-17 10:29:28 +01:00
|
|
|
|
|
|
|
// let order_by_facet = {
|
|
|
|
// let criteria = self.index.criteria(self.rtxn)?;
|
|
|
|
// let result = criteria.into_iter().flat_map(|criterion| {
|
|
|
|
// match criterion {
|
|
|
|
// Criterion::Asc(fid) => Some((fid, true)),
|
|
|
|
// Criterion::Desc(fid) => Some((fid, false)),
|
|
|
|
// _ => None
|
|
|
|
// }
|
|
|
|
// }).next();
|
|
|
|
// match result {
|
|
|
|
// Some((attr_name, is_ascending)) => {
|
|
|
|
// let field_id_map = self.index.fields_ids_map(self.rtxn)?;
|
|
|
|
// let fid = field_id_map.id(&attr_name).with_context(|| format!("unknown field: {:?}", attr_name))?;
|
|
|
|
// let faceted_fields = self.index.faceted_fields_ids(self.rtxn)?;
|
|
|
|
// let ftype = *faceted_fields.get(&fid)
|
|
|
|
// .with_context(|| format!("{:?} not found in the faceted fields.", attr_name))
|
|
|
|
// .expect("corrupted data: ");
|
|
|
|
// Some((fid, ftype, is_ascending))
|
|
|
|
// },
|
|
|
|
// None => None,
|
|
|
|
// }
|
|
|
|
// };
|
|
|
|
|
|
|
|
// let before = Instant::now();
|
|
|
|
// let (candidates, derived_words) = match (facet_candidates, derived_words) {
|
|
|
|
// (Some(mut facet_candidates), Some(derived_words)) => {
|
|
|
|
// let words_candidates = Self::compute_candidates(&derived_words);
|
|
|
|
// facet_candidates.intersect_with(&words_candidates);
|
|
|
|
// (facet_candidates, derived_words)
|
|
|
|
// },
|
|
|
|
// (None, Some(derived_words)) => {
|
|
|
|
// (Self::compute_candidates(&derived_words), derived_words)
|
|
|
|
// },
|
|
|
|
// (Some(facet_candidates), None) => {
|
|
|
|
// // If the query is not set or results in no DFAs but
|
|
|
|
// // there is some facet conditions we return a placeholder.
|
|
|
|
// let documents_ids = match order_by_facet {
|
|
|
|
// Some((fid, ftype, is_ascending)) => {
|
|
|
|
// self.facet_ordered(fid, ftype, is_ascending, facet_candidates.clone(), limit)?
|
|
|
|
// },
|
|
|
|
// None => facet_candidates.iter().take(limit).collect(),
|
|
|
|
// };
|
|
|
|
// return Ok(SearchResult {
|
|
|
|
// documents_ids,
|
|
|
|
// candidates: facet_candidates,
|
|
|
|
// ..Default::default()
|
|
|
|
// })
|
|
|
|
// },
|
|
|
|
// (None, None) => {
|
|
|
|
// // If the query is not set or results in no DFAs we return a placeholder.
|
|
|
|
// let all_docids = self.index.documents_ids(self.rtxn)?;
|
|
|
|
// let documents_ids = match order_by_facet {
|
|
|
|
// Some((fid, ftype, is_ascending)) => {
|
|
|
|
// self.facet_ordered(fid, ftype, is_ascending, all_docids.clone(), limit)?
|
|
|
|
// },
|
|
|
|
// None => all_docids.iter().take(limit).collect(),
|
|
|
|
// };
|
|
|
|
// return Ok(SearchResult { documents_ids, candidates: all_docids,..Default::default() })
|
|
|
|
// },
|
|
|
|
// };
|
|
|
|
|
|
|
|
// debug!("candidates: {:?} took {:.02?}", candidates, before.elapsed());
|
|
|
|
|
|
|
|
// // The mana depth first search is a revised DFS that explore
|
|
|
|
// // solutions in the order of their proximities.
|
|
|
|
// let mut mdfs = Mdfs::new(self.index, self.rtxn, &derived_words, candidates.clone());
|
|
|
|
// let mut documents = Vec::new();
|
|
|
|
|
|
|
|
// // We execute the Mdfs iterator until we find enough documents.
|
|
|
|
// while documents.iter().map(RoaringBitmap::len).sum::<u64>() < limit as u64 {
|
|
|
|
// match mdfs.next().transpose()? {
|
|
|
|
// Some((proximity, answer)) => {
|
|
|
|
// debug!("answer with a proximity of {}: {:?}", proximity, answer);
|
|
|
|
// documents.push(answer);
|
|
|
|
// },
|
|
|
|
// None => break,
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// let found_words = derived_words.into_iter().flat_map(|(w, _)| w).map(|(w, _)| w).collect();
|
|
|
|
// let documents_ids = match order_by_facet {
|
|
|
|
// Some((fid, ftype, order)) => {
|
|
|
|
// let mut ordered_documents = Vec::new();
|
|
|
|
// for documents_ids in documents {
|
|
|
|
// let docids = self.facet_ordered(fid, ftype, order, documents_ids, limit)?;
|
|
|
|
// ordered_documents.push(docids);
|
|
|
|
// if ordered_documents.iter().map(Vec::len).sum::<usize>() >= limit { break }
|
|
|
|
// }
|
|
|
|
// ordered_documents.into_iter().flatten().take(limit).collect()
|
|
|
|
// },
|
|
|
|
// None => documents.into_iter().flatten().take(limit).collect(),
|
|
|
|
// };
|
|
|
|
|
|
|
|
// Ok(SearchResult { found_words, 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 {
|
|
|
|
pub found_words: HashSet<String>,
|
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
|
|
|
|
|
|
|
pub fn word_typos(word: &str, is_prefix: bool, max_typo: u8, fst: &fst::Set<Cow<[u8]>>) -> anyhow::Result<Vec<(String, u8)>> {
|
|
|
|
let dfa = {
|
|
|
|
let lev = match max_typo {
|
|
|
|
0 => &LEVDIST0,
|
|
|
|
1 => &LEVDIST1,
|
|
|
|
_ => &LEVDIST2,
|
|
|
|
};
|
|
|
|
|
|
|
|
if is_prefix {
|
|
|
|
lev.build_prefix_dfa(&word)
|
|
|
|
} else {
|
|
|
|
lev.build_dfa(&word)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut derived_words = Vec::new();
|
|
|
|
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)
|
|
|
|
}
|