2023-03-30 16:10:10 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use levenshtein_automata::{LevenshteinAutomatonBuilder as LevBuilder, DFA};
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use roaring::bitmap::RoaringBitmap;
|
|
|
|
|
2022-09-08 13:28:17 +02:00
|
|
|
pub use self::facet::{FacetDistribution, Filter, DEFAULT_VALUES_PER_FACET};
|
2022-04-12 16:31:58 +02:00
|
|
|
pub use self::matches::{
|
|
|
|
FormatOptions, MatchBounds, Matcher, MatcherBuilder, MatchingWord, MatchingWords,
|
|
|
|
};
|
2023-03-23 09:35:53 +01:00
|
|
|
use crate::{
|
|
|
|
execute_search, AscDesc, DefaultSearchLogger, DocumentId, Index, Result, SearchContext,
|
|
|
|
};
|
2021-06-15 11:51:32 +02:00
|
|
|
|
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));
|
|
|
|
|
2022-08-31 13:03:36 +02:00
|
|
|
pub mod facet;
|
2022-03-15 17:28:57 +01:00
|
|
|
mod fst_utils;
|
2022-03-22 15:22:14 +01:00
|
|
|
mod matches;
|
2023-02-21 09:40:41 +01:00
|
|
|
pub mod new;
|
2020-11-20 10:54:41 +01:00
|
|
|
|
|
|
|
pub struct Search<'a> {
|
|
|
|
query: Option<String>,
|
2021-10-22 01:59:38 +02:00
|
|
|
// this should be linked to the String in the query
|
2021-10-22 14:33:18 +02:00
|
|
|
filter: Option<Filter<'a>>,
|
2020-11-20 10:54:41 +01:00
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
2021-08-23 11:37:18 +02:00
|
|
|
sort_criteria: Option<Vec<AscDesc>>,
|
2022-08-22 17:37:36 +02:00
|
|
|
terms_matching_strategy: TermsMatchingStrategy,
|
2021-03-10 11:16:30 +01:00
|
|
|
authorize_typos: bool,
|
2021-04-13 19:10:58 +02:00
|
|
|
words_limit: usize,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits: bool,
|
2020-11-20 10:54:41 +01: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 11:16:30 +01:00
|
|
|
Search {
|
|
|
|
query: None,
|
2021-06-01 15:25:17 +02:00
|
|
|
filter: None,
|
2021-03-10 11:16:30 +01:00
|
|
|
offset: 0,
|
|
|
|
limit: 20,
|
2021-08-23 11:37:18 +02:00
|
|
|
sort_criteria: None,
|
2022-08-22 17:37:36 +02:00
|
|
|
terms_matching_strategy: TermsMatchingStrategy::default(),
|
2021-03-10 11:16:30 +01:00
|
|
|
authorize_typos: true,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits: false,
|
2021-04-13 19:10:58 +02:00
|
|
|
words_limit: 10,
|
2021-03-10 11:16:30 +01:00
|
|
|
rtxn,
|
|
|
|
index,
|
|
|
|
}
|
2020-11-20 10:54:41 +01: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-08-23 11:37:18 +02:00
|
|
|
pub fn sort_criteria(&mut self, criteria: Vec<AscDesc>) -> &mut Search<'a> {
|
|
|
|
self.sort_criteria = Some(criteria);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-22 17:37:36 +02:00
|
|
|
pub fn terms_matching_strategy(&mut self, value: TermsMatchingStrategy) -> &mut Search<'a> {
|
|
|
|
self.terms_matching_strategy = value;
|
2021-03-10 11:16:30 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn authorize_typos(&mut self, value: bool) -> &mut Search<'a> {
|
|
|
|
self.authorize_typos = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-13 19:10:58 +02:00
|
|
|
pub fn words_limit(&mut self, value: usize) -> &mut Search<'a> {
|
|
|
|
self.words_limit = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-22 14:33:18 +02:00
|
|
|
pub fn filter(&mut self, condition: Filter<'a>) -> &mut Search<'a> {
|
2021-06-01 15:25:17 +02:00
|
|
|
self.filter = Some(condition);
|
2020-11-20 10:54:41 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-10-17 14:41:57 +02:00
|
|
|
/// Force the search to exhastivelly compute the number of candidates,
|
|
|
|
/// this will increase the search time but allows finite pagination.
|
2022-07-12 17:56:50 +02:00
|
|
|
pub fn exhaustive_number_hits(&mut self, exhaustive_number_hits: bool) -> &mut Search<'a> {
|
|
|
|
self.exhaustive_number_hits = exhaustive_number_hits;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:41:18 +01:00
|
|
|
// TODO!
|
|
|
|
fn _is_typo_authorized(&self) -> Result<bool> {
|
2022-03-31 09:54:49 +02:00
|
|
|
let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?;
|
|
|
|
// only authorize typos if both the index and the query allow it.
|
|
|
|
Ok(self.authorize_typos && index_authorizes_typos)
|
|
|
|
}
|
|
|
|
|
2021-06-14 16:46:19 +02:00
|
|
|
pub fn execute(&self) -> Result<SearchResult> {
|
2023-03-23 09:35:53 +01:00
|
|
|
let mut ctx = SearchContext::new(self.index, self.rtxn);
|
|
|
|
execute_search(
|
|
|
|
&mut ctx,
|
|
|
|
&self.query,
|
|
|
|
self.terms_matching_strategy,
|
2023-03-30 16:10:10 +02:00
|
|
|
self.exhaustive_number_hits,
|
2023-03-23 09:35:53 +01:00
|
|
|
&self.filter,
|
2023-03-28 12:40:52 +02:00
|
|
|
&self.sort_criteria,
|
2023-03-23 09:35:53 +01:00
|
|
|
self.offset,
|
|
|
|
self.limit,
|
|
|
|
Some(self.words_limit),
|
|
|
|
&mut DefaultSearchLogger,
|
|
|
|
&mut DefaultSearchLogger,
|
|
|
|
)
|
2020-11-20 10:54:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for Search<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2021-03-10 11:16:30 +01:00
|
|
|
let Search {
|
|
|
|
query,
|
2021-06-01 15:25:17 +02:00
|
|
|
filter,
|
2021-03-10 11:16:30 +01:00
|
|
|
offset,
|
|
|
|
limit,
|
2021-08-23 11:37:18 +02:00
|
|
|
sort_criteria,
|
2022-08-22 17:37:36 +02:00
|
|
|
terms_matching_strategy,
|
2021-03-10 11:16:30 +01:00
|
|
|
authorize_typos,
|
2021-04-13 19:10:58 +02:00
|
|
|
words_limit,
|
2022-07-12 17:56:50 +02:00
|
|
|
exhaustive_number_hits,
|
2021-03-10 11:16:30 +01:00
|
|
|
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)
|
2021-06-01 15:25:17 +02:00
|
|
|
.field("filter", filter)
|
2020-11-22 15:40:11 +01:00
|
|
|
.field("offset", offset)
|
|
|
|
.field("limit", limit)
|
2021-08-23 11:37:18 +02:00
|
|
|
.field("sort_criteria", sort_criteria)
|
2022-08-22 17:37:36 +02:00
|
|
|
.field("terms_matching_strategy", terms_matching_strategy)
|
2021-03-10 11:16:30 +01:00
|
|
|
.field("authorize_typos", authorize_typos)
|
2022-07-12 17:56:50 +02:00
|
|
|
.field("exhaustive_number_hits", exhaustive_number_hits)
|
2021-04-13 19:10:58 +02:00
|
|
|
.field("words_limit", words_limit)
|
2020-11-20 10:54:41 +01:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-06 17:50:47 +01:00
|
|
|
#[derive(Default, Debug)]
|
2020-11-20 10:54:41 +01:00
|
|
|
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
|
|
|
|
2022-08-18 17:36:08 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
pub enum TermsMatchingStrategy {
|
|
|
|
// remove last word first
|
|
|
|
Last,
|
|
|
|
// all words are mandatory
|
|
|
|
All,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for TermsMatchingStrategy {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Last
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 18:35:11 +01:00
|
|
|
fn get_first(s: &str) -> &str {
|
|
|
|
match s.chars().next() {
|
|
|
|
Some(c) => &s[..c.len_utf8()],
|
2022-01-25 10:06:27 +01:00
|
|
|
None => panic!("unexpected empty query"),
|
2022-01-20 18:35:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 09:54:49 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
2022-03-31 14:06:23 +02:00
|
|
|
use crate::index::tests::TempIndex;
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-02-20 15:33:10 +01:00
|
|
|
#[cfg(feature = "default")]
|
2023-02-01 18:39:54 +01:00
|
|
|
#[test]
|
|
|
|
fn test_kanji_language_detection() {
|
|
|
|
let index = TempIndex::new();
|
|
|
|
|
|
|
|
index
|
|
|
|
.add_documents(documents!([
|
|
|
|
{ "id": 0, "title": "The quick (\"brown\") fox can't jump 32.3 feet, right? Brr, it's 29.3°F!" },
|
|
|
|
{ "id": 1, "title": "東京のお寿司。" },
|
|
|
|
{ "id": 2, "title": "הַשּׁוּעָל הַמָּהִיר (״הַחוּם״) לֹא יָכוֹל לִקְפֹּץ 9.94 מֶטְרִים, נָכוֹן? ברר, 1.5°C- בַּחוּץ!" }
|
|
|
|
]))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let txn = index.write_txn().unwrap();
|
|
|
|
let mut search = Search::new(&txn, &index);
|
|
|
|
|
|
|
|
search.query("東京");
|
|
|
|
let SearchResult { documents_ids, .. } = search.execute().unwrap();
|
|
|
|
|
|
|
|
assert_eq!(documents_ids, vec![1]);
|
|
|
|
}
|
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// #[test]
|
|
|
|
// fn test_is_authorized_typos() {
|
|
|
|
// let index = TempIndex::new();
|
|
|
|
// let mut txn = index.write_txn().unwrap();
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// let mut search = Search::new(&txn, &index);
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// // default is authorized
|
|
|
|
// assert!(search.is_typo_authorized().unwrap());
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// search.authorize_typos(false);
|
|
|
|
// assert!(!search.is_typo_authorized().unwrap());
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// index.put_authorize_typos(&mut txn, false).unwrap();
|
|
|
|
// txn.commit().unwrap();
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// let txn = index.read_txn().unwrap();
|
|
|
|
// let mut search = Search::new(&txn, &index);
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// assert!(!search.is_typo_authorized().unwrap());
|
2022-03-31 09:54:49 +02:00
|
|
|
|
2023-03-23 09:42:25 +01:00
|
|
|
// search.authorize_typos(true);
|
|
|
|
// assert!(!search.is_typo_authorized().unwrap());
|
|
|
|
// }
|
2022-04-01 11:05:18 +02:00
|
|
|
|
2023-03-23 09:39:16 +01:00
|
|
|
// #[test]
|
|
|
|
// fn test_one_typos_tolerance() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("zealend", false, 1, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[("zealand".to_string(), 1)]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_one_typos_first_letter() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("sealand", false, 1, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_two_typos_tolerance() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("zealemd", false, 2, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[("zealand".to_string(), 2)]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_two_typos_first_letter() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("sealand", false, 2, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[("zealand".to_string(), 2)]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_prefix() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("ze", true, 0, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[("zealand".to_string(), 0)]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_bad_prefix() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("se", true, 0, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[]);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn test_prefix_with_typo() {
|
|
|
|
// let fst = fst::Set::from_iter(["zealand"].iter()).unwrap().map_data(Cow::Owned).unwrap();
|
|
|
|
// let mut cache = HashMap::new();
|
|
|
|
// let found = word_derivations("zae", true, 1, &fst, &mut cache).unwrap();
|
|
|
|
|
|
|
|
// assert_eq!(found, &[("zealand".to_string(), 1)]);
|
|
|
|
// }
|
2022-03-31 09:54:49 +02:00
|
|
|
}
|