2023-03-30 22:10:10 +08:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use levenshtein_automata::{LevenshteinAutomatonBuilder as LevBuilder, DFA};
|
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use roaring::bitmap::RoaringBitmap;
|
|
|
|
|
2023-05-29 21:32:09 +08:00
|
|
|
pub use self::facet::{FacetDistribution, Filter, OrderBy, DEFAULT_VALUES_PER_FACET};
|
2023-10-30 18:00:46 +08:00
|
|
|
pub use self::new::matches::{FormatOptions, MatchBounds, MatcherBuilder, MatchingWords};
|
2023-11-15 22:46:37 +08:00
|
|
|
use self::new::{execute_vector_search, PartialSearchResult};
|
2023-06-07 00:26:33 +08:00
|
|
|
use crate::score_details::{ScoreDetails, ScoringStrategy};
|
2023-12-13 22:38:44 +08:00
|
|
|
use crate::vector::DistributionShift;
|
2023-03-23 16:35:53 +08:00
|
|
|
use crate::{
|
2024-03-13 17:24:21 +08:00
|
|
|
execute_search, filtered_universe, AscDesc, DefaultSearchLogger, DocumentId, Index, Result,
|
2024-03-05 18:21:46 +08:00
|
|
|
SearchContext, TimeBudget,
|
2023-03-23 16:35:53 +08:00
|
|
|
};
|
2021-06-15 17:51:32 +08:00
|
|
|
|
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));
|
|
|
|
|
2022-08-31 19:03:36 +08:00
|
|
|
pub mod facet;
|
2022-03-16 00:28:57 +08:00
|
|
|
mod fst_utils;
|
2023-11-15 22:46:37 +08:00
|
|
|
pub mod hybrid;
|
2023-02-21 16:40:41 +08:00
|
|
|
pub mod new;
|
2020-11-20 17:54:41 +08:00
|
|
|
|
|
|
|
pub struct Search<'a> {
|
|
|
|
query: Option<String>,
|
2023-06-09 00:47:06 +08:00
|
|
|
vector: Option<Vec<f32>>,
|
2021-10-22 07:59:38 +08:00
|
|
|
// this should be linked to the String in the query
|
2021-10-22 20:33:18 +08:00
|
|
|
filter: Option<Filter<'a>>,
|
2020-11-20 17:54:41 +08:00
|
|
|
offset: usize,
|
|
|
|
limit: usize,
|
2021-08-23 17:37:18 +08:00
|
|
|
sort_criteria: Option<Vec<AscDesc>>,
|
2023-06-07 17:24:40 +08:00
|
|
|
searchable_attributes: Option<&'a [String]>,
|
2023-04-13 19:45:34 +08:00
|
|
|
geo_strategy: new::GeoSortStrategy,
|
2022-08-22 23:37:36 +08:00
|
|
|
terms_matching_strategy: TermsMatchingStrategy,
|
2023-06-07 00:26:33 +08:00
|
|
|
scoring_strategy: ScoringStrategy,
|
2021-04-14 01:10:58 +08:00
|
|
|
words_limit: usize,
|
2022-07-12 23:56:50 +08:00
|
|
|
exhaustive_number_hits: bool,
|
2023-12-13 18:46:09 +08:00
|
|
|
/// TODO: Add semantic ratio or pass it directly to execute_hybrid()
|
2020-11-20 17:54:41 +08:00
|
|
|
rtxn: &'a heed::RoTxn<'a>,
|
|
|
|
index: &'a Index,
|
2023-12-13 22:38:44 +08:00
|
|
|
distribution_shift: Option<DistributionShift>,
|
|
|
|
embedder_name: Option<String>,
|
2024-03-05 18:21:46 +08:00
|
|
|
|
|
|
|
time_budget: TimeBudget,
|
2020-11-20 17:54:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2023-06-09 00:47:06 +08:00
|
|
|
vector: None,
|
2021-06-01 21:25:17 +08:00
|
|
|
filter: None,
|
2021-03-10 18:16:30 +08:00
|
|
|
offset: 0,
|
|
|
|
limit: 20,
|
2021-08-23 17:37:18 +08:00
|
|
|
sort_criteria: None,
|
2023-05-31 20:57:46 +08:00
|
|
|
searchable_attributes: None,
|
2023-04-13 19:45:34 +08:00
|
|
|
geo_strategy: new::GeoSortStrategy::default(),
|
2022-08-22 23:37:36 +08:00
|
|
|
terms_matching_strategy: TermsMatchingStrategy::default(),
|
2023-06-07 00:26:33 +08:00
|
|
|
scoring_strategy: Default::default(),
|
2022-07-12 23:56:50 +08:00
|
|
|
exhaustive_number_hits: false,
|
2021-04-14 01:10:58 +08:00
|
|
|
words_limit: 10,
|
2021-03-10 18:16:30 +08:00
|
|
|
rtxn,
|
|
|
|
index,
|
2023-12-13 22:38:44 +08:00
|
|
|
distribution_shift: None,
|
|
|
|
embedder_name: None,
|
2024-03-05 18:21:46 +08:00
|
|
|
time_budget: TimeBudget::max(),
|
2021-03-10 18:16:30 +08:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:46:37 +08:00
|
|
|
pub fn vector(&mut self, vector: Vec<f32>) -> &mut Search<'a> {
|
|
|
|
self.vector = Some(vector);
|
2023-06-09 00:47:06 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2020-11-20 17:54:41 +08:00
|
|
|
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 17:37:18 +08:00
|
|
|
pub fn sort_criteria(&mut self, criteria: Vec<AscDesc>) -> &mut Search<'a> {
|
|
|
|
self.sort_criteria = Some(criteria);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-07 17:24:40 +08:00
|
|
|
pub fn searchable_attributes(&mut self, searchable: &'a [String]) -> &mut Search<'a> {
|
2023-05-31 20:57:46 +08:00
|
|
|
self.searchable_attributes = Some(searchable);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-08-22 23:37:36 +08:00
|
|
|
pub fn terms_matching_strategy(&mut self, value: TermsMatchingStrategy) -> &mut Search<'a> {
|
|
|
|
self.terms_matching_strategy = value;
|
2021-03-10 18:16:30 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-07 00:26:33 +08:00
|
|
|
pub fn scoring_strategy(&mut self, value: ScoringStrategy) -> &mut Search<'a> {
|
|
|
|
self.scoring_strategy = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-04-14 01:10:58 +08:00
|
|
|
pub fn words_limit(&mut self, value: usize) -> &mut Search<'a> {
|
|
|
|
self.words_limit = value;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-10-22 20:33:18 +08:00
|
|
|
pub fn filter(&mut self, condition: Filter<'a>) -> &mut Search<'a> {
|
2021-06-01 21:25:17 +08:00
|
|
|
self.filter = Some(condition);
|
2020-11-20 17:54:41 +08:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-04-13 19:45:34 +08:00
|
|
|
#[cfg(test)]
|
|
|
|
pub fn geo_sort_strategy(&mut self, strategy: new::GeoSortStrategy) -> &mut Search<'a> {
|
|
|
|
self.geo_strategy = strategy;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-06-07 00:26:33 +08:00
|
|
|
/// Forces the search to exhaustively compute the number of candidates,
|
2022-10-17 20:41:57 +08:00
|
|
|
/// this will increase the search time but allows finite pagination.
|
2022-07-12 23:56:50 +08:00
|
|
|
pub fn exhaustive_number_hits(&mut self, exhaustive_number_hits: bool) -> &mut Search<'a> {
|
|
|
|
self.exhaustive_number_hits = exhaustive_number_hits;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-12-13 22:38:44 +08:00
|
|
|
pub fn distribution_shift(
|
|
|
|
&mut self,
|
|
|
|
distribution_shift: Option<DistributionShift>,
|
|
|
|
) -> &mut Search<'a> {
|
|
|
|
self.distribution_shift = distribution_shift;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn embedder_name(&mut self, embedder_name: impl Into<String>) -> &mut Search<'a> {
|
|
|
|
self.embedder_name = Some(embedder_name.into());
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-03-05 18:21:46 +08:00
|
|
|
pub fn time_budget(&mut self, time_budget: TimeBudget) -> &mut Search<'a> {
|
|
|
|
self.time_budget = time_budget;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2023-12-14 23:01:35 +08:00
|
|
|
pub fn execute_for_candidates(&self, has_vector_search: bool) -> Result<RoaringBitmap> {
|
|
|
|
if has_vector_search {
|
|
|
|
let ctx = SearchContext::new(self.index, self.rtxn);
|
|
|
|
filtered_universe(&ctx, &self.filter)
|
|
|
|
} else {
|
|
|
|
Ok(self.execute()?.candidates)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-14 22:46:19 +08:00
|
|
|
pub fn execute(&self) -> Result<SearchResult> {
|
2023-12-13 22:38:44 +08:00
|
|
|
let embedder_name;
|
|
|
|
let embedder_name = match &self.embedder_name {
|
|
|
|
Some(embedder_name) => embedder_name,
|
|
|
|
None => {
|
|
|
|
embedder_name = self.index.default_embedding_name(self.rtxn)?;
|
|
|
|
&embedder_name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-04-06 21:02:23 +08:00
|
|
|
let mut ctx = SearchContext::new(self.index, self.rtxn);
|
2023-06-13 20:42:38 +08:00
|
|
|
|
|
|
|
if let Some(searchable_attributes) = self.searchable_attributes {
|
|
|
|
ctx.searchable_attributes(searchable_attributes)?;
|
|
|
|
}
|
|
|
|
|
2023-11-15 22:46:37 +08:00
|
|
|
let universe = filtered_universe(&ctx, &self.filter)?;
|
2024-03-05 18:21:46 +08:00
|
|
|
let PartialSearchResult {
|
|
|
|
located_query_terms,
|
|
|
|
candidates,
|
|
|
|
documents_ids,
|
|
|
|
document_scores,
|
|
|
|
degraded,
|
2024-03-27 01:01:27 +08:00
|
|
|
used_negative_operator,
|
2024-03-05 18:21:46 +08:00
|
|
|
} = match self.vector.as_ref() {
|
|
|
|
Some(vector) => execute_vector_search(
|
|
|
|
&mut ctx,
|
|
|
|
vector,
|
|
|
|
self.scoring_strategy,
|
|
|
|
universe,
|
|
|
|
&self.sort_criteria,
|
|
|
|
self.geo_strategy,
|
|
|
|
self.offset,
|
|
|
|
self.limit,
|
|
|
|
self.distribution_shift,
|
|
|
|
embedder_name,
|
2024-03-15 00:34:46 +08:00
|
|
|
self.time_budget.clone(),
|
2024-03-05 18:21:46 +08:00
|
|
|
)?,
|
|
|
|
None => execute_search(
|
|
|
|
&mut ctx,
|
|
|
|
self.query.as_deref(),
|
|
|
|
self.terms_matching_strategy,
|
|
|
|
self.scoring_strategy,
|
|
|
|
self.exhaustive_number_hits,
|
|
|
|
universe,
|
|
|
|
&self.sort_criteria,
|
|
|
|
self.geo_strategy,
|
|
|
|
self.offset,
|
|
|
|
self.limit,
|
|
|
|
Some(self.words_limit),
|
|
|
|
&mut DefaultSearchLogger,
|
|
|
|
&mut DefaultSearchLogger,
|
2024-03-15 00:34:46 +08:00
|
|
|
self.time_budget.clone(),
|
2024-03-05 18:21:46 +08:00
|
|
|
)?,
|
|
|
|
};
|
2023-04-06 21:02:23 +08:00
|
|
|
|
|
|
|
// consume context and located_query_terms to build MatchingWords.
|
|
|
|
let matching_words = match located_query_terms {
|
|
|
|
Some(located_query_terms) => MatchingWords::new(ctx, located_query_terms),
|
|
|
|
None => MatchingWords::default(),
|
|
|
|
};
|
|
|
|
|
2024-03-27 01:01:27 +08:00
|
|
|
Ok(SearchResult {
|
|
|
|
matching_words,
|
|
|
|
candidates,
|
|
|
|
document_scores,
|
|
|
|
documents_ids,
|
|
|
|
degraded,
|
|
|
|
used_negative_operator,
|
|
|
|
})
|
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,
|
2023-06-09 00:47:06 +08:00
|
|
|
vector: _,
|
2021-06-01 21:25:17 +08:00
|
|
|
filter,
|
2021-03-10 18:16:30 +08:00
|
|
|
offset,
|
|
|
|
limit,
|
2021-08-23 17:37:18 +08:00
|
|
|
sort_criteria,
|
2023-05-31 20:57:46 +08:00
|
|
|
searchable_attributes,
|
2023-04-13 19:45:34 +08:00
|
|
|
geo_strategy: _,
|
2022-08-22 23:37:36 +08:00
|
|
|
terms_matching_strategy,
|
2023-06-07 00:26:33 +08:00
|
|
|
scoring_strategy,
|
2021-04-14 01:10:58 +08:00
|
|
|
words_limit,
|
2022-07-12 23:56:50 +08:00
|
|
|
exhaustive_number_hits,
|
2021-03-10 18:16:30 +08:00
|
|
|
rtxn: _,
|
|
|
|
index: _,
|
2023-12-13 22:38:44 +08:00
|
|
|
distribution_shift,
|
|
|
|
embedder_name,
|
2024-03-05 18:21:46 +08:00
|
|
|
time_budget,
|
2021-03-10 18:16:30 +08:00
|
|
|
} = self;
|
2020-11-20 17:54:41 +08:00
|
|
|
f.debug_struct("Search")
|
2020-11-22 22:40:11 +08:00
|
|
|
.field("query", query)
|
2023-06-09 00:47:06 +08:00
|
|
|
.field("vector", &"[...]")
|
2021-06-01 21:25:17 +08:00
|
|
|
.field("filter", filter)
|
2020-11-22 22:40:11 +08:00
|
|
|
.field("offset", offset)
|
|
|
|
.field("limit", limit)
|
2021-08-23 17:37:18 +08:00
|
|
|
.field("sort_criteria", sort_criteria)
|
2023-05-31 20:57:46 +08:00
|
|
|
.field("searchable_attributes", searchable_attributes)
|
2022-08-22 23:37:36 +08:00
|
|
|
.field("terms_matching_strategy", terms_matching_strategy)
|
2023-06-07 00:26:33 +08:00
|
|
|
.field("scoring_strategy", scoring_strategy)
|
2022-07-12 23:56:50 +08:00
|
|
|
.field("exhaustive_number_hits", exhaustive_number_hits)
|
2021-04-14 01:10:58 +08:00
|
|
|
.field("words_limit", words_limit)
|
2023-12-13 22:38:44 +08:00
|
|
|
.field("distribution_shift", distribution_shift)
|
|
|
|
.field("embedder_name", embedder_name)
|
2024-03-19 21:49:15 +08:00
|
|
|
.field("time_budget", time_budget)
|
2020-11-20 17:54:41 +08:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 00:50:47 +08:00
|
|
|
#[derive(Default, Debug)]
|
2020-11-20 17:54:41 +08:00
|
|
|
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
|
|
|
pub documents_ids: Vec<DocumentId>,
|
2023-06-07 00:26:33 +08:00
|
|
|
pub document_scores: Vec<Vec<ScoreDetails>>,
|
2024-03-05 18:21:46 +08:00
|
|
|
pub degraded: bool,
|
2024-03-27 01:01:27 +08:00
|
|
|
pub used_negative_operator: bool,
|
2020-11-20 17:54:41 +08:00
|
|
|
}
|
2021-03-03 19:03:31 +08:00
|
|
|
|
2022-08-18 23:36:08 +08: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-21 01:35:11 +08:00
|
|
|
fn get_first(s: &str) -> &str {
|
|
|
|
match s.chars().next() {
|
|
|
|
Some(c) => &s[..c.len_utf8()],
|
2022-01-25 17:06:27 +08:00
|
|
|
None => panic!("unexpected empty query"),
|
2022-01-21 01:35:11 +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)
|
|
|
|
}
|
|
|
|
}
|
2022-03-31 15:54:49 +08:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2023-05-04 21:46:11 +08:00
|
|
|
#[allow(unused_imports)]
|
2022-03-31 15:54:49 +08:00
|
|
|
use super::*;
|
|
|
|
|
2023-04-24 06:26:08 +08:00
|
|
|
#[cfg(feature = "japanese")]
|
2023-02-02 01:39:54 +08:00
|
|
|
#[test]
|
|
|
|
fn test_kanji_language_detection() {
|
2023-05-04 21:46:11 +08:00
|
|
|
use crate::index::tests::TempIndex;
|
|
|
|
|
2023-02-02 01:39:54 +08:00
|
|
|
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]);
|
|
|
|
}
|
2022-03-31 15:54:49 +08:00
|
|
|
}
|