Skip to content

Add proximity precision to settings #548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1609,3 +1609,21 @@ search_parameter_guide_facet_stats_1: |-
.execute()
.await
.unwrap();
get_proximity_precision_settings_1: |-
let proximity_precision: String = client
.index("books")
.get_typo_tolerance()
.await
.unwrap();
update_proximity_precision_settings_1: |-
let task: TaskInfo = client
.index("books")
.set_proximity_precision("byAttribute".to_string())
.await
.unwrap();
reset_proximity_precision_settings_1: |-
let task: TaskInfo = client
.index("books")
.reset_proximity_precision()
.await
.unwrap();
154 changes: 154 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ pub struct Settings {
/// Dictionary settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub dictionary: Option<Vec<String>>,
/// Proximity precision settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub proximity_precision: Option<String>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -261,6 +264,13 @@ impl Settings {
..self
}
}

pub fn with_proximity_precision(self, proximity_precision: impl AsRef<str>) -> Settings {
Settings {
proximity_precision: Some(proximity_precision.as_ref().to_string()),
..self
}
}
}

impl Index {
Expand Down Expand Up @@ -654,6 +664,38 @@ impl Index {
.await
}

/// Get [proximity_precision](https://www.meilisearch.com/docs/reference/api/settings#proximity-precision) of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
/// # client.create_index("get_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let index = client.index("get_proximity_precision");
///
/// let proximity_precision = index.get_proximity_precision().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_proximity_precision(&self) -> Result<String, Error> {
request::<(), (), String>(
&format!(
"{}/indexes/{}/settings/proximity-precision",
self.client.host, self.uid
),
self.client.get_api_key(),
Method::Get { query: () },
200,
)
.await
}

/// Get [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) of the [Index].
///
/// ```
Expand Down Expand Up @@ -1225,6 +1267,44 @@ impl Index {
.await
}

/// Update [proximity-precision](https://www.meilisearch.com/docs/learn/configuration/proximity-precision) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
/// # client.create_index("set_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("set_proximity_precision");
///
/// let task = index.set_proximity_precision("byWord".to_string()).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_proximity_precision(
&self,
proximity_precision: String,
) -> Result<TaskInfo, Error> {
request::<(), String, TaskInfo>(
&format!(
"{}/indexes/{}/settings/proximity-precision",
self.client.host, self.uid
),
self.client.get_api_key(),
Method::Put {
query: (),
body: proximity_precision,
},
202,
)
.await
}

/// Reset [Settings] of the [Index].
///
/// All settings will be reset to their [default value](https://www.meilisearch.com/docs/reference/api/settings#reset-settings).
Expand Down Expand Up @@ -1641,6 +1721,38 @@ impl Index {
)
.await
}

/// Reset [proximity precision](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// #
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
/// # client.create_index("reset_proximity_precision", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_proximity_precision");
///
/// let task = index.reset_proximity_precision().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_proximity_precision(&self) -> Result<TaskInfo, Error> {
request::<(), (), TaskInfo>(
&format!(
"{}/indexes/{}/settings/proximity-precision",
self.client.host, self.uid
),
self.client.get_api_key(),
Method::Delete { query: () },
202,
)
.await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -1853,4 +1965,46 @@ mod tests {

assert_eq!(expected, default);
}

#[meilisearch_test]
async fn test_get_proximity_precision(index: Index) {
let expected = "byWord".to_string();

let res = index.get_proximity_precision().await.unwrap();

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_set_proximity_precision(client: Client, index: Index) {
let expected = "byAttribute".to_string();

let task_info = index
.set_proximity_precision("byAttribute".to_string())
.await
.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_proximity_precision().await.unwrap();

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_reset_proximity_precision(index: Index) {
let expected = "byWord".to_string();

let task = index
.set_proximity_precision("byAttribute".to_string())
.await
.unwrap();
index.wait_for_task(task, None, None).await.unwrap();

let reset_task = index.reset_proximity_precision().await.unwrap();
index.wait_for_task(reset_task, None, None).await.unwrap();

let default = index.get_proximity_precision().await.unwrap();

assert_eq!(expected, default);
}
}