Skip to content

Commit ae5df59

Browse files
Merge #548
548: Add proximity precision to settings r=curquiza a=cyprx # Pull Request ## Related issue Fixes #540 ## What does this PR do? - ... ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: cyprx <[email protected]>
2 parents d914ad2 + 404ab90 commit ae5df59

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,3 +1609,21 @@ search_parameter_guide_facet_stats_1: |-
16091609
.execute()
16101610
.await
16111611
.unwrap();
1612+
get_proximity_precision_settings_1: |-
1613+
let proximity_precision: String = client
1614+
.index("books")
1615+
.get_typo_tolerance()
1616+
.await
1617+
.unwrap();
1618+
update_proximity_precision_settings_1: |-
1619+
let task: TaskInfo = client
1620+
.index("books")
1621+
.set_proximity_precision("byAttribute".to_string())
1622+
.await
1623+
.unwrap();
1624+
reset_proximity_precision_settings_1: |-
1625+
let task: TaskInfo = client
1626+
.index("books")
1627+
.reset_proximity_precision()
1628+
.await
1629+
.unwrap();

src/settings.rs

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ pub struct Settings {
9898
/// Dictionary settings.
9999
#[serde(skip_serializing_if = "Option::is_none")]
100100
pub dictionary: Option<Vec<String>>,
101+
/// Proximity precision settings.
102+
#[serde(skip_serializing_if = "Option::is_none")]
103+
pub proximity_precision: Option<String>,
101104
}
102105

103106
#[allow(missing_docs)]
@@ -261,6 +264,13 @@ impl Settings {
261264
..self
262265
}
263266
}
267+
268+
pub fn with_proximity_precision(self, proximity_precision: impl AsRef<str>) -> Settings {
269+
Settings {
270+
proximity_precision: Some(proximity_precision.as_ref().to_string()),
271+
..self
272+
}
273+
}
264274
}
265275

266276
impl Index {
@@ -654,6 +664,38 @@ impl Index {
654664
.await
655665
}
656666

667+
/// Get [proximity_precision](https://www.meilisearch.com/docs/reference/api/settings#proximity-precision) of the [Index].
668+
///
669+
/// # Example
670+
///
671+
/// ```
672+
/// # use meilisearch_sdk::{client::*, indexes::*};
673+
/// #
674+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
675+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
676+
/// #
677+
/// # futures::executor::block_on(async move {
678+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
679+
/// # client.create_index("get_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
680+
/// let index = client.index("get_proximity_precision");
681+
///
682+
/// let proximity_precision = index.get_proximity_precision().await.unwrap();
683+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
684+
/// # });
685+
/// ```
686+
pub async fn get_proximity_precision(&self) -> Result<String, Error> {
687+
request::<(), (), String>(
688+
&format!(
689+
"{}/indexes/{}/settings/proximity-precision",
690+
self.client.host, self.uid
691+
),
692+
self.client.get_api_key(),
693+
Method::Get { query: () },
694+
200,
695+
)
696+
.await
697+
}
698+
657699
/// Get [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) of the [Index].
658700
///
659701
/// ```
@@ -1225,6 +1267,44 @@ impl Index {
12251267
.await
12261268
}
12271269

1270+
/// Update [proximity-precision](https://www.meilisearch.com/docs/learn/configuration/proximity-precision) settings of the [Index].
1271+
///
1272+
/// # Example
1273+
///
1274+
/// ```
1275+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1276+
/// #
1277+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1278+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1279+
/// #
1280+
/// # futures::executor::block_on(async move {
1281+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
1282+
/// # client.create_index("set_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1283+
/// let mut index = client.index("set_proximity_precision");
1284+
///
1285+
/// let task = index.set_proximity_precision("byWord".to_string()).await.unwrap();
1286+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1287+
/// # });
1288+
/// ```
1289+
pub async fn set_proximity_precision(
1290+
&self,
1291+
proximity_precision: String,
1292+
) -> Result<TaskInfo, Error> {
1293+
request::<(), String, TaskInfo>(
1294+
&format!(
1295+
"{}/indexes/{}/settings/proximity-precision",
1296+
self.client.host, self.uid
1297+
),
1298+
self.client.get_api_key(),
1299+
Method::Put {
1300+
query: (),
1301+
body: proximity_precision,
1302+
},
1303+
202,
1304+
)
1305+
.await
1306+
}
1307+
12281308
/// Reset [Settings] of the [Index].
12291309
///
12301310
/// All settings will be reset to their [default value](https://www.meilisearch.com/docs/reference/api/settings#reset-settings).
@@ -1641,6 +1721,38 @@ impl Index {
16411721
)
16421722
.await
16431723
}
1724+
1725+
/// Reset [proximity precision](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
1726+
///
1727+
/// # Example
1728+
///
1729+
/// ```
1730+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1731+
/// #
1732+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1733+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1734+
/// #
1735+
/// # futures::executor::block_on(async move {
1736+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
1737+
/// # client.create_index("reset_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1738+
/// let mut index = client.index("reset_proximity_precision");
1739+
///
1740+
/// let task = index.reset_proximity_precision().await.unwrap();
1741+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1742+
/// # });
1743+
/// ```
1744+
pub async fn reset_proximity_precision(&self) -> Result<TaskInfo, Error> {
1745+
request::<(), (), TaskInfo>(
1746+
&format!(
1747+
"{}/indexes/{}/settings/proximity-precision",
1748+
self.client.host, self.uid
1749+
),
1750+
self.client.get_api_key(),
1751+
Method::Delete { query: () },
1752+
202,
1753+
)
1754+
.await
1755+
}
16441756
}
16451757

16461758
#[cfg(test)]
@@ -1853,4 +1965,46 @@ mod tests {
18531965

18541966
assert_eq!(expected, default);
18551967
}
1968+
1969+
#[meilisearch_test]
1970+
async fn test_get_proximity_precision(index: Index) {
1971+
let expected = "byWord".to_string();
1972+
1973+
let res = index.get_proximity_precision().await.unwrap();
1974+
1975+
assert_eq!(expected, res);
1976+
}
1977+
1978+
#[meilisearch_test]
1979+
async fn test_set_proximity_precision(client: Client, index: Index) {
1980+
let expected = "byAttribute".to_string();
1981+
1982+
let task_info = index
1983+
.set_proximity_precision("byAttribute".to_string())
1984+
.await
1985+
.unwrap();
1986+
client.wait_for_task(task_info, None, None).await.unwrap();
1987+
1988+
let res = index.get_proximity_precision().await.unwrap();
1989+
1990+
assert_eq!(expected, res);
1991+
}
1992+
1993+
#[meilisearch_test]
1994+
async fn test_reset_proximity_precision(index: Index) {
1995+
let expected = "byWord".to_string();
1996+
1997+
let task = index
1998+
.set_proximity_precision("byAttribute".to_string())
1999+
.await
2000+
.unwrap();
2001+
index.wait_for_task(task, None, None).await.unwrap();
2002+
2003+
let reset_task = index.reset_proximity_precision().await.unwrap();
2004+
index.wait_for_task(reset_task, None, None).await.unwrap();
2005+
2006+
let default = index.get_proximity_precision().await.unwrap();
2007+
2008+
assert_eq!(expected, default);
2009+
}
18562010
}

0 commit comments

Comments
 (0)