Skip to content

Commit

Permalink
Bugfix transposition_cost_one in FuzzyQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Oct 6, 2021
1 parent 4d05b26 commit 8ac070b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Tantivy 0.16.2
================================
- Bugfix in FuzzyTermQuery. (tranposition_cost_one was not doing anything)

Tantivy 0.16.1
========================
- Major Bugfix on multivalued fastfield. #1151
Expand Down
28 changes: 27 additions & 1 deletion src/query/fuzzy_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl FuzzyTermQuery {

fn specialized_weight(&self) -> crate::Result<AutomatonWeight<DfaWrapper>> {
// LEV_BUILDER is a HashMap, whose `get` method returns an Option
match LEV_BUILDER.get(&(self.distance, false)) {
match LEV_BUILDER.get(&(self.distance, self.transposition_cost_one)) {
// Unwrap the option and build the Ok(AutomatonWeight)
Some(automaton_builder) => {
let automaton = if self.prefix {
Expand Down Expand Up @@ -164,6 +164,7 @@ impl Query for FuzzyTermQuery {
mod test {
use super::FuzzyTermQuery;
use crate::assert_nearly_equals;
use crate::collector::Count;
use crate::collector::TopDocs;
use crate::schema::Schema;
use crate::schema::TEXT;
Expand Down Expand Up @@ -226,4 +227,29 @@ mod test {
assert_nearly_equals!(1.0, score);
}
}

#[test]
pub fn test_fuzzy_term_transposition_cost_one() -> crate::Result<()> {
let mut schema_builder = Schema::builder();
let country_field = schema_builder.add_text_field("country", TEXT);
let schema = schema_builder.build();
let index = Index::create_in_ram(schema);
let mut index_writer = index.writer_for_tests()?;
index_writer.add_document(doc!(country_field => "japan"));
index_writer.commit()?;
let reader = index.reader()?;
let searcher = reader.searcher();
let term_jaapn = Term::from_field_text(country_field, "jaapn");
{
let fuzzy_query_transposition = FuzzyTermQuery::new(term_jaapn.clone(), 1, true);
let count = searcher.search(&fuzzy_query_transposition, &Count)?;
assert_eq!(count, 1);
}
{
let fuzzy_query_transposition = FuzzyTermQuery::new(term_jaapn, 1, false);
let count = searcher.search(&fuzzy_query_transposition, &Count)?;
assert_eq!(count, 0);
}
Ok(())
}
}

0 comments on commit 8ac070b

Please sign in to comment.