Interpret synonyms as phrases

This commit is contained in:
Loïc Lecrenier 2022-11-29 13:49:12 +01:00
parent a8defb585b
commit b0f3dc2c06

View File

@ -339,18 +339,18 @@ fn typos(word: String, authorize_typos: bool, config: TypoConfig) -> QueryKind {
/// and create the list of operations for the query tree /// and create the list of operations for the query tree
fn synonyms(ctx: &impl Context, word: &[&str]) -> heed::Result<Option<Vec<Operation>>> { fn synonyms(ctx: &impl Context, word: &[&str]) -> heed::Result<Option<Vec<Operation>>> {
let synonyms = ctx.synonyms(word)?; let synonyms = ctx.synonyms(word)?;
Ok(synonyms.map(|synonyms| { Ok(synonyms.map(|synonyms| {
synonyms synonyms
.into_iter() .into_iter()
.map(|synonym| { .map(|synonym| {
let words = synonym if synonym.len() == 1 {
.into_iter() Operation::Query(Query {
.map(|word| { prefix: false,
Operation::Query(Query { prefix: false, kind: QueryKind::exact(word) }) kind: QueryKind::exact(synonym[0].clone()),
}) })
.collect(); } else {
Operation::and(words) Operation::Phrase(synonym.into_iter().map(Some).collect())
}
}) })
.collect() .collect()
})) }))
@ -1058,9 +1058,7 @@ mod test {
AND AND
OR OR
Exact { word: "hi" } Exact { word: "hi" }
AND PHRASE [Some("good"), Some("morning")]
Exact { word: "good" }
Exact { word: "morning" }
Tolerant { word: "hello", max typo: 1 } Tolerant { word: "hello", max typo: 1 }
OR OR
Exact { word: "earth" } 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] #[test]
fn complex_synonyms() { fn complex_synonyms() {
let query = "new york city "; let query = "new york city ";
@ -1092,16 +1108,11 @@ mod test {
AND AND
OR OR
Exact { word: "nyc" } Exact { word: "nyc" }
AND PHRASE [Some("new"), Some("york"), Some("city")]
Exact { word: "new" }
Exact { word: "york" }
Exact { word: "city" }
Tolerant { word: "newyork", max typo: 1 } Tolerant { word: "newyork", max typo: 1 }
Exact { word: "city" } Exact { word: "city" }
Exact { word: "nyc" } Exact { word: "nyc" }
AND PHRASE [Some("new"), Some("york")]
Exact { word: "new" }
Exact { word: "york" }
Tolerant { word: "newyorkcity", max typo: 1 } Tolerant { word: "newyorkcity", max typo: 1 }
"###); "###);
} }