diff --git a/milli/src/search/query_tree.rs b/milli/src/search/query_tree.rs index e689ae440..7ac4fded4 100755 --- a/milli/src/search/query_tree.rs +++ b/milli/src/search/query_tree.rs @@ -339,18 +339,18 @@ fn typos(word: String, authorize_typos: bool, config: TypoConfig) -> QueryKind { /// and create the list of operations for the query tree fn synonyms(ctx: &impl Context, word: &[&str]) -> heed::Result>> { let synonyms = ctx.synonyms(word)?; - Ok(synonyms.map(|synonyms| { synonyms .into_iter() .map(|synonym| { - let words = synonym - .into_iter() - .map(|word| { - Operation::Query(Query { prefix: false, kind: QueryKind::exact(word) }) + if synonym.len() == 1 { + Operation::Query(Query { + prefix: false, + kind: QueryKind::exact(synonym[0].clone()), }) - .collect(); - Operation::and(words) + } else { + Operation::Phrase(synonym.into_iter().map(Some).collect()) + } }) .collect() })) @@ -1058,9 +1058,7 @@ mod test { AND OR Exact { word: "hi" } - AND - Exact { word: "good" } - Exact { word: "morning" } + PHRASE [Some("good"), Some("morning")] Tolerant { word: "hello", max typo: 1 } OR Exact { word: "earth" } @@ -1070,6 +1068,24 @@ mod test { "###); } + #[test] + fn simple_synonyms() { + let query = "nyc"; + let tokens = query.tokenize(); + + let (query_tree, _) = TestContext::default() + .build(TermsMatchingStrategy::Last, true, None, tokens) + .unwrap() + .unwrap(); + + insta::assert_debug_snapshot!(query_tree, @r###" + OR + PHRASE [Some("new"), Some("york")] + PHRASE [Some("new"), Some("york"), Some("city")] + PrefixExact { word: "nyc" } + "###); + } + #[test] fn complex_synonyms() { let query = "new york city "; @@ -1092,16 +1108,11 @@ mod test { AND OR Exact { word: "nyc" } - AND - Exact { word: "new" } - Exact { word: "york" } - Exact { word: "city" } + PHRASE [Some("new"), Some("york"), Some("city")] Tolerant { word: "newyork", max typo: 1 } Exact { word: "city" } Exact { word: "nyc" } - AND - Exact { word: "new" } - Exact { word: "york" } + PHRASE [Some("new"), Some("york")] Tolerant { word: "newyorkcity", max typo: 1 } "###); }