mirror of
				https://github.com/meilisearch/meilisearch.git
				synced 2025-10-31 07:56:28 +00:00 
			
		
		
		
	add authorize_typo_test
This commit is contained in:
		| @@ -1009,4 +1009,18 @@ pub(crate) mod tests { | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
|  | ||||
|     #[test] | ||||
|     fn put_and_retrieve_disable_typo() { | ||||
|         let index = TempIndex::new(); | ||||
|         let mut txn = index.write_txn().unwrap(); | ||||
|         // default value is true | ||||
|         assert!(index.authorize_typos(&txn).unwrap()); | ||||
|         // set to false | ||||
|         index.put_authorize_typos(&mut txn, false).unwrap(); | ||||
|         txn.commit().unwrap(); | ||||
|  | ||||
|         let txn = index.read_txn().unwrap(); | ||||
|         assert!(!index.authorize_typos(&txn).unwrap()); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -105,6 +105,12 @@ impl<'a> Search<'a> { | ||||
|         self | ||||
|     } | ||||
|  | ||||
|     fn is_typo_authorized(&self) -> Result<bool> { | ||||
|         let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?; | ||||
|         // only authorize typos if both the index and the query allow it. | ||||
|         Ok(self.authorize_typos && index_authorizes_typos) | ||||
|     } | ||||
|  | ||||
|     pub fn execute(&self) -> Result<SearchResult> { | ||||
|         // We create the query tree by spliting the query into tokens. | ||||
|         let before = Instant::now(); | ||||
| @@ -113,9 +119,7 @@ impl<'a> Search<'a> { | ||||
|                 let mut builder = QueryTreeBuilder::new(self.rtxn, self.index); | ||||
|                 builder.optional_words(self.optional_words); | ||||
|  | ||||
|                 // only authorize typos if both the index and the query allow it. | ||||
|                 let index_authorizes_typos = self.index.authorize_typos(self.rtxn)?; | ||||
|                 builder.authorize_typos(self.authorize_typos && index_authorizes_typos); | ||||
|                 builder.authorize_typos(self.is_typo_authorized()?); | ||||
|  | ||||
|                 builder.words_limit(self.words_limit); | ||||
|                 // We make sure that the analyzer is aware of the stop words | ||||
| @@ -364,3 +368,35 @@ pub fn build_dfa(word: &str, typos: u8, is_prefix: bool) -> DFA { | ||||
|         lev.build_dfa(word) | ||||
|     } | ||||
| } | ||||
|  | ||||
| #[cfg(test)] | ||||
| mod test { | ||||
|     use crate::index::tests::TempIndex; | ||||
|  | ||||
|     use super::*; | ||||
|  | ||||
|     #[test] | ||||
|     fn test_is_authorized_typos() { | ||||
|         let index = TempIndex::new(); | ||||
|         let mut txn = index.write_txn().unwrap(); | ||||
|  | ||||
|         let mut search = Search::new(&txn, &index); | ||||
|  | ||||
|         // default is authorized | ||||
|         assert!(search.is_typo_authorized().unwrap()); | ||||
|  | ||||
|         search.authorize_typos(false); | ||||
|         assert!(!search.is_typo_authorized().unwrap()); | ||||
|  | ||||
|         index.put_authorize_typos(&mut txn, false).unwrap(); | ||||
|         txn.commit().unwrap(); | ||||
|  | ||||
|         let txn = index.read_txn().unwrap(); | ||||
|         let mut search = Search::new(&txn, &index); | ||||
|  | ||||
|         assert!(!search.is_typo_authorized().unwrap()); | ||||
|  | ||||
|         search.authorize_typos(true); | ||||
|         assert!(!search.is_typo_authorized().unwrap()); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user