From b167058028950867feeac42873adaf1b6b7bebf1 Mon Sep 17 00:00:00 2001 From: Chen Xu Date: Thu, 19 Mar 2020 04:19:32 -0700 Subject: [PATCH] Fix prefix option for FuzzyTermQuery (#797) * Fix prefix option for FuzzyTermQuery * Update changelog --- CHANGELOG.md | 4 ++++ src/query/fuzzy_query.rs | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90a9e4edf..0ec34d74c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +Tantivy 0.13.0 +====================== +- Bugfix in `FuzzyTermQuery` not matching terms by prefix when it should (@Peachball) + Tantivy 0.12.0 ====================== - Removing static dispatch in tokenizers for simplicity. (#762) diff --git a/src/query/fuzzy_query.rs b/src/query/fuzzy_query.rs index 543a0980f..3bff39d50 100644 --- a/src/query/fuzzy_query.rs +++ b/src/query/fuzzy_query.rs @@ -106,7 +106,11 @@ impl FuzzyTermQuery { match LEV_BUILDER.get(&(self.distance, false)) { // Unwrap the option and build the Ok(AutomatonWeight) Some(automaton_builder) => { - let automaton = automaton_builder.build_dfa(self.term.text()); + let automaton = if self.prefix { + automaton_builder.build_prefix_dfa(self.term.text()) + } else { + automaton_builder.build_dfa(self.term.text()) + }; Ok(AutomatonWeight::new(self.term.field(), automaton)) } None => Err(InvalidArgument(format!( @@ -166,5 +170,17 @@ mod test { let (score, _) = top_docs[0]; assert_nearly_equals(1f32, score); } + + { + let term = Term::from_field_text(country_field, "jap"); + + let fuzzy_query = FuzzyTermQuery::new_prefix(term, 1, true); + let top_docs = searcher + .search(&fuzzy_query, &TopDocs::with_limit(2)) + .unwrap(); + assert_eq!(top_docs.len(), 1, "Expected only 1 document"); + let (score, _) = top_docs[0]; + assert_nearly_equals(1f32, score); + } } }