Skip to content

Commit 8e35931

Browse files
committed
add support for typo tolerance customization
1 parent dcb81fd commit 8e35931

File tree

1 file changed

+207
-19
lines changed

1 file changed

+207
-19
lines changed

src/settings.rs

Lines changed: 207 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,23 @@ use std::collections::HashMap;
1010
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Copy)]
1111
#[serde(rename_all = "camelCase")]
1212
pub struct PaginationSetting {
13-
pub max_total_hits: usize
13+
pub max_total_hits: usize,
14+
}
15+
16+
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
17+
#[serde(rename_all = "camelCase")]
18+
pub struct MinWordSizeForTypos {
19+
pub one_typo: i64,
20+
pub two_typos: i64,
21+
}
22+
23+
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq)]
24+
#[serde(rename_all = "camelCase")]
25+
pub struct TypoToleranceSettings {
26+
pub enabled: bool,
27+
pub disable_on_attributes: Vec<String>,
28+
pub disable_on_words: Vec<String>,
29+
pub min_word_size_for_typos: MinWordSizeForTypos,
1430
}
1531

1632
#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)]
@@ -77,6 +93,9 @@ pub struct Settings {
7793
/// Faceting settings
7894
#[serde(skip_serializing_if = "Option::is_none")]
7995
pub faceting: Option<FacetingSettings>,
96+
/// TypoTolerance settings
97+
#[serde(skip_serializing_if = "Option::is_none")]
98+
pub typo_tolerance: Option<TypoToleranceSettings>,
8099
}
81100

82101
#[allow(missing_docs)]
@@ -94,6 +113,7 @@ impl Settings {
94113
displayed_attributes: None,
95114
pagination: None,
96115
faceting: None,
116+
typo_tolerance: None,
97117
}
98118
}
99119
pub fn with_synonyms<S, U, V>(self, synonyms: HashMap<S, U>) -> Settings
@@ -140,6 +160,13 @@ impl Settings {
140160
}
141161
}
142162

163+
pub fn with_typo_tolerance(self, typo_tolerance_settings: TypoToleranceSettings) -> Settings {
164+
Settings {
165+
typo_tolerance: Some(typo_tolerance_settings),
166+
..self
167+
}
168+
}
169+
143170
pub fn with_ranking_rules(
144171
self,
145172
ranking_rules: impl IntoIterator<Item = impl AsRef<str>>,
@@ -222,10 +249,7 @@ impl Settings {
222249
}
223250
}
224251

225-
pub fn with_faceting(
226-
self,
227-
faceting: &FacetingSettings,
228-
) -> Settings {
252+
pub fn with_faceting(self, faceting: &FacetingSettings) -> Settings {
229253
Settings {
230254
faceting: Some(faceting.clone()),
231255
..self
@@ -550,6 +574,35 @@ impl Index {
550574
.await
551575
}
552576

577+
/// Get [typo tolerance](https://docs.meilisearch.com/learn/configuration/typo_tolerance.html#typo-tolerance) of the [Index].
578+
///
579+
/// ```
580+
/// # use meilisearch_sdk::{client::*, indexes::*};
581+
/// #
582+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
583+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
584+
/// #
585+
/// # futures::executor::block_on(async move {
586+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
587+
/// # client.create_index("get_typo_tolerance", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
588+
/// let index = client.index("get_typo_tolerance");
589+
/// let typotolerance = index.get_typo_tolerance().await.unwrap();
590+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
591+
/// # });
592+
/// ```
593+
pub async fn get_typo_tolerance(&self) -> Result<TypoToleranceSettings, Error> {
594+
request::<(), TypoToleranceSettings>(
595+
&format!(
596+
"{}/indexes/{}/settings/typo-tolerance",
597+
self.client.host, self.uid
598+
),
599+
&self.client.api_key,
600+
Method::Get(()),
601+
200,
602+
)
603+
.await
604+
}
605+
553606
/// Update [settings](../settings/struct.Settings.html) of the [Index].
554607
/// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged.
555608
///
@@ -645,7 +698,7 @@ impl Index {
645698
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
646699
/// # });
647700
/// ```
648-
pub async fn set_pagination(&self, pagination: PaginationSetting,) -> Result<TaskInfo, Error> {
701+
pub async fn set_pagination(&self, pagination: PaginationSetting) -> Result<TaskInfo, Error> {
649702
request::<&PaginationSetting, TaskInfo>(
650703
&format!(
651704
"{}/indexes/{}/settings/pagination",
@@ -969,10 +1022,7 @@ impl Index {
9691022
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
9701023
/// # });
9711024
/// ```
972-
pub async fn set_faceting(
973-
&self,
974-
faceting: &FacetingSettings,
975-
) -> Result<TaskInfo, Error> {
1025+
pub async fn set_faceting(&self, faceting: &FacetingSettings) -> Result<TaskInfo, Error> {
9761026
request::<&FacetingSettings, TaskInfo>(
9771027
&format!(
9781028
"{}/indexes/{}/settings/faceting",
@@ -985,6 +1035,48 @@ impl Index {
9851035
.await
9861036
}
9871037

1038+
/// Update [typo tolerance](https://docs.meilisearch.com/learn/configuration/typo_tolerance.html#typo-tolerance) settings of the [Index].
1039+
///
1040+
/// # Example
1041+
///
1042+
/// ```
1043+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{TypoToleranceSettings, MinWordSizeForTypos}};
1044+
/// #
1045+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
1046+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1047+
/// #
1048+
/// # futures::executor::block_on(async move {
1049+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
1050+
/// # client.create_index("set_typo_tolerance", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1051+
/// let mut index = client.index("set_typo_tolerance");
1052+
///
1053+
/// let mut typo_tolerance = TypoToleranceSettings {
1054+
/// enabled: true,
1055+
/// disable_on_attributes: vec!(),
1056+
/// disable_on_words: vec!(),
1057+
/// min_word_size_for_typos: MinWordSizeForTypos { one_typo : 1, two_typos: 2},
1058+
/// };
1059+
///
1060+
/// let task = index.set_typo_tolerance(&typo_tolerance).await.unwrap();
1061+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1062+
/// # });
1063+
/// ```
1064+
pub async fn set_typo_tolerance(
1065+
&self,
1066+
typo_tolerance: &TypoToleranceSettings,
1067+
) -> Result<TaskInfo, Error> {
1068+
request::<&TypoToleranceSettings, TaskInfo>(
1069+
&format!(
1070+
"{}/indexes/{}/settings/typo-tolerance",
1071+
self.client.host, self.uid
1072+
),
1073+
&self.client.api_key,
1074+
Method::Patch(typo_tolerance),
1075+
202,
1076+
)
1077+
.await
1078+
}
1079+
9881080
/// Reset [Settings] of the [Index].
9891081
/// All settings will be reset to their [default value](https://docs.meilisearch.com/reference/api/settings.html#reset-settings).
9901082
///
@@ -1334,8 +1426,39 @@ impl Index {
13341426
)
13351427
.await
13361428
}
1337-
}
13381429

1430+
/// Reset [typo tolerance](https://docs.meilisearch.com/learn/configuration/typo_tolerance.html#typo-tolerance) settings of the [Index].
1431+
///
1432+
/// # Example
1433+
///
1434+
/// ```
1435+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1436+
/// #
1437+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
1438+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1439+
/// #
1440+
/// # futures::executor::block_on(async move {
1441+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
1442+
/// # client.create_index("reset_typo_tolerance", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1443+
/// let mut index = client.index("reset_typo_tolerance");
1444+
///
1445+
/// let task = index.reset_typo_tolerance().await.unwrap();
1446+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1447+
/// # });
1448+
/// ```
1449+
pub async fn reset_typo_tolerance(&self) -> Result<TaskInfo, Error> {
1450+
request::<(), TaskInfo>(
1451+
&format!(
1452+
"{}/indexes/{}/settings/typo-tolerance",
1453+
self.client.host, self.uid
1454+
),
1455+
&self.client.api_key,
1456+
Method::Delete,
1457+
202,
1458+
)
1459+
.await
1460+
}
1461+
}
13391462

13401463
#[cfg(test)]
13411464
mod tests {
@@ -1349,8 +1472,7 @@ mod tests {
13491472
let faceting = FacetingSettings {
13501473
max_values_per_facet: 5,
13511474
};
1352-
let settings = Settings::new()
1353-
.with_faceting(&faceting);
1475+
let settings = Settings::new().with_faceting(&faceting);
13541476

13551477
let task_info = index.set_settings(&settings).await.unwrap();
13561478
client.wait_for_task(task_info, None, None).await.unwrap();
@@ -1410,9 +1532,7 @@ mod tests {
14101532

14111533
#[meilisearch_test]
14121534
async fn test_set_pagination(index: Index) {
1413-
let pagination = PaginationSetting {
1414-
max_total_hits: 11,
1415-
};
1535+
let pagination = PaginationSetting { max_total_hits: 11 };
14161536
let task = index.set_pagination(pagination).await.unwrap();
14171537
index.wait_for_task(task, None, None).await.unwrap();
14181538

@@ -1423,10 +1543,10 @@ mod tests {
14231543

14241544
#[meilisearch_test]
14251545
async fn test_reset_pagination(index: Index) {
1426-
let pagination = PaginationSetting {
1427-
max_total_hits: 10,
1546+
let pagination = PaginationSetting { max_total_hits: 10 };
1547+
let default = PaginationSetting {
1548+
max_total_hits: 1000,
14281549
};
1429-
let default = PaginationSetting { max_total_hits: 1000};
14301550

14311551
let task = index.set_pagination(pagination).await.unwrap();
14321552
index.wait_for_task(task, None, None).await.unwrap();
@@ -1438,4 +1558,72 @@ mod tests {
14381558

14391559
assert_eq!(default, res);
14401560
}
1561+
1562+
#[meilisearch_test]
1563+
async fn test_get_typo_tolerance(index: Index) {
1564+
let typo_tolerance = TypoToleranceSettings {
1565+
enabled: true,
1566+
disable_on_attributes: vec![],
1567+
disable_on_words: vec![],
1568+
min_word_size_for_typos: MinWordSizeForTypos {
1569+
one_typo: 5,
1570+
two_typos: 9,
1571+
},
1572+
};
1573+
1574+
let res = index.get_typo_tolerance().await.unwrap();
1575+
1576+
assert_eq!(typo_tolerance, res);
1577+
}
1578+
1579+
#[meilisearch_test]
1580+
async fn test_set_typo_tolerance(client: Client, index: Index) {
1581+
let typo_tolerance = TypoToleranceSettings {
1582+
enabled: false,
1583+
disable_on_attributes: vec![],
1584+
disable_on_words: vec![],
1585+
min_word_size_for_typos: MinWordSizeForTypos {
1586+
one_typo: 6,
1587+
two_typos: 9,
1588+
},
1589+
};
1590+
let task_info = index.set_typo_tolerance(&typo_tolerance).await.unwrap();
1591+
client.wait_for_task(task_info, None, None).await.unwrap();
1592+
1593+
let res = index.get_typo_tolerance().await.unwrap();
1594+
1595+
assert_eq!(typo_tolerance, res);
1596+
}
1597+
1598+
#[meilisearch_test]
1599+
async fn test_reset_typo_tolerance(index: Index) {
1600+
let typo_tolerance = TypoToleranceSettings {
1601+
enabled: false,
1602+
disable_on_attributes: vec![],
1603+
disable_on_words: vec![],
1604+
min_word_size_for_typos: MinWordSizeForTypos {
1605+
one_typo: 1,
1606+
two_typos: 2,
1607+
},
1608+
};
1609+
let default = TypoToleranceSettings {
1610+
enabled: true,
1611+
disable_on_attributes: vec![],
1612+
disable_on_words: vec![],
1613+
min_word_size_for_typos: MinWordSizeForTypos {
1614+
one_typo: 5,
1615+
two_typos: 9,
1616+
},
1617+
};
1618+
1619+
let task = index.set_typo_tolerance(&typo_tolerance).await.unwrap();
1620+
index.wait_for_task(task, None, None).await.unwrap();
1621+
1622+
let reset_task = index.reset_typo_tolerance().await.unwrap();
1623+
index.wait_for_task(reset_task, None, None).await.unwrap();
1624+
1625+
let res = index.get_typo_tolerance().await.unwrap();
1626+
1627+
assert_eq!(default, res);
1628+
}
14411629
}

0 commit comments

Comments
 (0)