diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 6bdef025..35385049 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -958,6 +958,19 @@ typo_tolerance_guide_2: |- min_word_size_for_typos: None, }; +typo_tolerance_guide_5: |- + // Deactivate typo tolerance on numbers and other high entropy words + let typo_tolerance = TypoToleranceSettings { + disable_on_numbers: Some(true), + ..Default::default() + }; + + let task: TaskInfo = client + .index("movies") + .set_typo_tolerance(&typo_tolerance) + .await + .unwrap(); + let task: TaskInfo = client .index("movies") .set_typo_tolerance(&typo_tolerance) diff --git a/src/settings.rs b/src/settings.rs index ba1c05e2..04ac8045 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -28,6 +28,9 @@ pub struct TypoToleranceSettings { pub disable_on_attributes: Option>, pub disable_on_words: Option>, pub min_word_size_for_typos: Option, + /// Deactivate typo tolerance on high entropy words such as numbers + #[serde(skip_serializing_if = "Option::is_none")] + pub disable_on_numbers: Option, } #[derive(Debug, Deserialize, Clone, Eq, PartialEq, Serialize)] @@ -1795,6 +1798,7 @@ impl Index { /// disable_on_attributes: Some(vec!["title".to_string()]), /// disable_on_words: Some(vec![]), /// min_word_size_for_typos: Some(MinWordSizeForTypos::default()), + /// ..Default::default() /// }; /// /// let task = index.set_typo_tolerance(&typo_tolerance).await.unwrap(); @@ -3038,6 +3042,8 @@ mod tests { one_typo: Some(5), two_typos: Some(9), }), + // The server may return `false` explicitly for this new setting + disable_on_numbers: Some(false), }; let res = index.get_typo_tolerance().await.unwrap(); @@ -3055,6 +3061,7 @@ mod tests { one_typo: Some(5), two_typos: Some(9), }), + disable_on_numbers: Some(false), }; let typo_tolerance = TypoToleranceSettings { @@ -3080,6 +3087,7 @@ mod tests { one_typo: Some(5), two_typos: Some(9), }), + disable_on_numbers: Some(false), }; let typo_tolerance = TypoToleranceSettings { @@ -3098,6 +3106,28 @@ mod tests { assert_eq!(expected, default); } + #[meilisearch_test] + async fn test_set_disable_on_numbers(client: Client, index: Index) { + // Set disable_on_numbers to true + let typo_tolerance = TypoToleranceSettings { + disable_on_numbers: Some(true), + ..Default::default() + }; + + let task_info = index.set_typo_tolerance(&typo_tolerance).await.unwrap(); + client.wait_for_task(task_info, None, None).await.unwrap(); + + // Fetch and assert it is set + let res = index.get_typo_tolerance().await.unwrap(); + assert_eq!(res.disable_on_numbers, Some(true)); + + // Reset and ensure it goes back to default false + let reset_task = index.reset_typo_tolerance().await.unwrap(); + client.wait_for_task(reset_task, None, None).await.unwrap(); + let default = index.get_typo_tolerance().await.unwrap(); + assert_eq!(default.disable_on_numbers, Some(false)); + } + #[meilisearch_test] async fn test_get_proximity_precision(index: Index) { let expected = "byWord".to_string();