From b0f3dc2c06bc6bdb6c4d9ad3a53d8baf98048f55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Lecrenier?= Date: Tue, 29 Nov 2022 13:49:12 +0100 Subject: [PATCH] Interpret synonyms as phrases --- milli/src/search/query_tree.rs | 45 +++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 17 deletions(-) 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 } "###); }