diff --git a/milli/src/error.rs b/milli/src/error.rs index 471952a36..611160319 100644 --- a/milli/src/error.rs +++ b/milli/src/error.rs @@ -292,7 +292,7 @@ ranking rules settings to use the sort parameter at search time.", Self::UnknownInternalDocumentId { document_id } => { write!(f, "An unknown internal document id have been used: `{}`.", document_id) } - Self::InvalidMinTypoWordSetting(one, two) => write!(f, "`minWordSizeForTypos` setting is invalid. `oneTypo` and `twoTypos` fields should be between `0` and `255`, and `twoTypos` should be greater or equals to `oneTypo` but found `oneTypo: {}` and twoTypos: {}`."", one, two), + Self::InvalidMinTypoWordSetting(one, two) => write!(f, "`minWordSizeForTypos` setting is invalid. `oneTypo` and `twoTypos` fields should be between `0` and `255`, and `twoTypos` should be greater or equals to `oneTypo` but found `oneTypo: {}` and twoTypos: {}`.", one, two), } } } diff --git a/milli/src/index.rs b/milli/src/index.rs index 3c1ba948f..0095352e4 100644 --- a/milli/src/index.rs +++ b/milli/src/index.rs @@ -937,6 +937,7 @@ pub(crate) mod tests { use maplit::btreemap; use tempfile::TempDir; + use crate::index::{DEFAULT_MIN_WORD_LEN_1_TYPO, DEFAULT_MIN_WORD_LEN_2_TYPOS}; use crate::update::{IndexDocuments, IndexDocumentsConfig, IndexerConfig}; use crate::Index; @@ -1064,4 +1065,22 @@ pub(crate) mod tests { let txn = index.read_txn().unwrap(); assert!(!index.authorize_typos(&txn).unwrap()); } + + #[test] + fn set_min_word_len_for_typos() { + let index = TempIndex::new(); + let mut txn = index.write_txn().unwrap(); + + assert_eq!(index.min_word_len_1_typo(&txn).unwrap(), DEFAULT_MIN_WORD_LEN_1_TYPO); + assert_eq!(index.min_word_len_2_typo(&txn).unwrap(), DEFAULT_MIN_WORD_LEN_2_TYPOS); + + index.put_min_word_len_1_typo(&mut txn, 3).unwrap(); + index.put_min_word_len_2_typo(&mut txn, 15).unwrap(); + + txn.commit().unwrap(); + + let txn = index.read_txn().unwrap(); + assert_eq!(index.min_word_len_1_typo(&txn).unwrap(), 3); + assert_eq!(index.min_word_len_2_typo(&txn).unwrap(), 15); + } } diff --git a/milli/src/search/query_tree.rs b/milli/src/search/query_tree.rs index 6db2ce7a7..acaba680f 100644 --- a/milli/src/search/query_tree.rs +++ b/milli/src/search/query_tree.rs @@ -264,6 +264,7 @@ fn split_best_frequency(ctx: &impl Context, word: &str) -> heed::Result Settings<'a, 't, 'u, 'i> { fn update_min_typo_word_len(&mut self) -> Result<()> { match (&self.min_1_typo_word_len, &self.min_2_typos_word_len) { (Setting::Set(one), Setting::Set(two)) => { - if one < two { + if one > two { + return Err(UserError::InvalidMinTypoWordSetting(*one, *two).into()); + } else { self.index.put_min_word_len_1_typo(&mut self.wtxn, *one)?; self.index.put_min_word_len_2_typo(&mut self.wtxn, *two)?; - } else { - return Err(UserError::InvalidMinTypoWordSetting(*one, *two).into()); } } (Setting::Set(one), _) => { let two = self.index.min_word_len_2_typo(&self.wtxn)?; - if *one < two { - self.index.put_min_word_len_1_typo(&mut self.wtxn, *one)?; - } else { + if *one > two { return Err(UserError::InvalidMinTypoWordSetting(*one, two).into()); + } else { + self.index.put_min_word_len_1_typo(&mut self.wtxn, *one)?; } } (_, Setting::Set(two)) => { let one = self.index.min_word_len_1_typo(&self.wtxn)?; - if one < *two { - self.index.put_min_word_len_2_typo(&mut self.wtxn, *two)?; - } else { + if one > *two { return Err(UserError::InvalidMinTypoWordSetting(one, *two).into()); + } else { + self.index.put_min_word_len_2_typo(&mut self.wtxn, *two)?; } } _ => (), @@ -1286,4 +1286,37 @@ mod tests { builder.execute(|_| ()).unwrap(); assert!(!index.authorize_typos(&txn).unwrap()); } + + #[test] + fn update_min_word_len_for_typo() { + let index = TempIndex::new(); + let config = IndexerConfig::default(); + + // Set the genres setting + let mut txn = index.write_txn().unwrap(); + let mut builder = Settings::new(&mut txn, &index, &config); + builder.set_min_1_typo_word_len(8); + builder.set_min_2_typos_word_len(8); + builder.execute(|_| ()).unwrap(); + + txn.commit().unwrap(); + + let txn = index.read_txn().unwrap(); + + assert_eq!(index.min_word_len_1_typo(&txn).unwrap(), 8); + assert_eq!(index.min_word_len_2_typo(&txn).unwrap(), 8); + } + + #[test] + fn update_invalid_min_word_len_for_typo() { + let index = TempIndex::new(); + let config = IndexerConfig::default(); + + // Set the genres setting + let mut txn = index.write_txn().unwrap(); + let mut builder = Settings::new(&mut txn, &index, &config); + builder.set_min_1_typo_word_len(10); + builder.set_min_2_typos_word_len(7); + assert!(builder.execute(|_| ()).is_err()); + } }