mirror of
https://github.com/meilisearch/MeiliSearch
synced 2024-12-25 06:00:08 +01:00
feat: Trigger synonym replacement only when the last word is tipped
This commit is contained in:
parent
8f044c6853
commit
8f2a551cca
@ -31,18 +31,25 @@ fn generate_automatons<S: Store>(query: &str, store: &S) -> Result<Vec<(usize, D
|
|||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let mut ngrams = query_words.windows(n).peekable();
|
let mut ngrams = query_words.windows(n).peekable();
|
||||||
|
|
||||||
while let Some(ngram) = ngrams.next() {
|
while let Some(ngram_slice) = ngrams.next() {
|
||||||
let ngram = ngram.join(" ");
|
let ngram = ngram_slice.join(" ");
|
||||||
|
let ngram_nb_words = ngram_slice.len();
|
||||||
|
|
||||||
let has_following_word = ngrams.peek().is_some();
|
let lev = build_prefix_dfa(&ngram);
|
||||||
let not_prefix_dfa = has_following_word || has_end_whitespace || ngram.chars().all(is_cjk);
|
|
||||||
|
|
||||||
let lev = if not_prefix_dfa { build_dfa(&ngram) } else { build_prefix_dfa(&ngram) };
|
|
||||||
let mut stream = synonyms.search(&lev).into_stream();
|
let mut stream = synonyms.search(&lev).into_stream();
|
||||||
while let Some(word) = stream.next() {
|
while let Some(base) = stream.next() {
|
||||||
if let Some(synonyms) = store.alternatives_to(word)? {
|
|
||||||
|
// only trigger alternatives when the last word has been typed
|
||||||
|
// i.e. "new " do not but "new yo" triggers alternatives to "new york"
|
||||||
|
let base = std::str::from_utf8(base).unwrap();
|
||||||
|
let base_nb_words = split_query_string(base).count();
|
||||||
|
if ngram_nb_words != base_nb_words { continue }
|
||||||
|
|
||||||
|
if let Some(synonyms) = store.alternatives_to(base.as_bytes())? {
|
||||||
|
|
||||||
let mut stream = synonyms.into_stream();
|
let mut stream = synonyms.into_stream();
|
||||||
while let Some(synonyms) = stream.next() {
|
while let Some(synonyms) = stream.next() {
|
||||||
|
|
||||||
let synonyms = std::str::from_utf8(synonyms).unwrap();
|
let synonyms = std::str::from_utf8(synonyms).unwrap();
|
||||||
for synonym in split_query_string(synonyms) {
|
for synonym in split_query_string(synonyms) {
|
||||||
let lev = build_dfa(synonym);
|
let lev = build_dfa(synonym);
|
||||||
@ -53,6 +60,11 @@ fn generate_automatons<S: Store>(query: &str, store: &S) -> Result<Vec<(usize, D
|
|||||||
}
|
}
|
||||||
|
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
|
let has_following_word = ngrams.peek().is_some();
|
||||||
|
let not_prefix_dfa = has_following_word || has_end_whitespace || ngram.chars().all(is_cjk);
|
||||||
|
|
||||||
|
let lev = if not_prefix_dfa { build_dfa(&ngram) } else { build_prefix_dfa(&ngram) };
|
||||||
|
|
||||||
automatons.push((index, ngram, lev));
|
automatons.push((index, ngram, lev));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -559,12 +571,24 @@ mod tests {
|
|||||||
let results = builder.query("sal blabla", 0..20).unwrap();
|
let results = builder.query("sal blabla", 0..20).unwrap();
|
||||||
let mut iter = results.into_iter();
|
let mut iter = results.into_iter();
|
||||||
|
|
||||||
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches }) => {
|
||||||
|
assert_eq!(matches.len(), 1);
|
||||||
|
let match_ = matches[0];
|
||||||
|
assert_eq!(match_.query_index, 0);
|
||||||
|
assert_eq!(match_.word_index, 0);
|
||||||
|
});
|
||||||
assert_matches!(iter.next(), None);
|
assert_matches!(iter.next(), None);
|
||||||
|
|
||||||
let builder = QueryBuilder::new(&store);
|
let builder = QueryBuilder::new(&store);
|
||||||
let results = builder.query("bonj blabla", 0..20).unwrap();
|
let results = builder.query("bonj blabla", 0..20).unwrap();
|
||||||
let mut iter = results.into_iter();
|
let mut iter = results.into_iter();
|
||||||
|
|
||||||
|
assert_matches!(iter.next(), Some(Document { id: DocumentId(0), matches }) => {
|
||||||
|
assert_eq!(matches.len(), 1);
|
||||||
|
let match_ = matches[0];
|
||||||
|
assert_eq!(match_.query_index, 0);
|
||||||
|
assert_eq!(match_.word_index, 0);
|
||||||
|
});
|
||||||
assert_matches!(iter.next(), None);
|
assert_matches!(iter.next(), None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user