Skip to content
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

Add feature: dictionary settings #529

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -595,6 +595,24 @@ reset_faceting_settings_1: |-
.reset_faceting()
.await
.unwrap();
get_dictionary_1: |-
let task: TaskInfo = client
.index('books')
.get_dictionary()
.await
.unwrap();
update_dictionary_1: |-
let task: TaskInfo = client
.index('books')
.set_dictionary(['J. R. R.', 'W. E. B.'])
.await
.unwrap();
reset_dictionary_1: |-
let task: TaskInfo = client
.index('books')
.reset_dictionary()
.await
.unwrap();
get_index_stats_1: |-
let stats: IndexStats = client
.index("movies")
Expand Down
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ pub enum ErrorCode {
InvalidSettingsDistinctAttributes,
InvalidSettingsTypoTolerance,
InvalidSettingsFaceting,
InvalidSettingsDictionary,
InvalidSettingsPagination,
InvalidTaskBeforeEnqueuedAt,
InvalidTaskAfterEnqueuedAt,
Expand Down
166 changes: 166 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ pub struct Settings {
/// TypoTolerance settings
#[serde(skip_serializing_if = "Option::is_none")]
pub typo_tolerance: Option<TypoToleranceSettings>,
/// Dictionary settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub dictionary: Option<Vec<String>>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -243,6 +246,21 @@ impl Settings {
..self
}
}

pub fn with_dictionary(
self,
dictionary: impl IntoIterator<Item = impl AsRef<str>>,
) -> Settings {
Settings {
dictionary: Some(
dictionary
.into_iter()
.map(|v| v.as_ref().to_string())
.collect(),
),
..self
}
}
}

impl Index {
Expand Down Expand Up @@ -604,6 +622,38 @@ impl Index {
.await
}

/// Get [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) 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_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let index = client.index("get_dictionary");
///
/// let dictionary = index.get_dictionary().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_dictionary(&self) -> Result<Vec<String>, Error> {
request::<(), (), Vec<String>>(
&format!(
"{}/indexes/{}/settings/dictionary",
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 @@ -1089,6 +1139,47 @@ impl Index {
.await
}

/// Update [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) 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_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("set_dictionary");
///
/// let task = index.set_dictionary(["J. K.", "J. R. R."]).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_dictionary(
&self,
dictionary: impl IntoIterator<Item = impl AsRef<str>>,
) -> Result<TaskInfo, Error> {
request::<(), Vec<String>, TaskInfo>(
&format!(
"{}/indexes/{}/settings/dictionary",
self.client.host, self.uid
),
self.client.get_api_key(),
Method::Put {
query: (),
body: dictionary
.into_iter()
.map(|v| v.as_ref().to_string())
.collect(),
},
202,
)
.await
}

/// Update [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
///
/// # Example
Expand Down Expand Up @@ -1487,6 +1578,38 @@ impl Index {
.await
}

/// Reset [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) 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_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_dictionary");
///
/// let task = index.reset_dictionary().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_dictionary(&self) -> Result<TaskInfo, Error> {
request::<(), (), TaskInfo>(
&format!(
"{}/indexes/{}/settings/dictionary",
self.client.host, self.uid
),
self.client.get_api_key(),
Method::Delete { query: () },
202,
)
.await
}

/// Reset [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
///
/// # Example
Expand Down Expand Up @@ -1579,6 +1702,49 @@ mod tests {
assert_eq!(faceting, res);
}

#[meilisearch_test]
async fn test_get_dicitonary(index: Index) {
let dictionary: Vec<String> = vec![];

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

assert_eq!(dictionary, res);
}

#[meilisearch_test]
async fn test_set_dicitonary(client: Client, index: Index) {
let dictionary: Vec<&str> = vec!["J. K.", "J. R. R."];
let task_info = index.set_dictionary(&dictionary).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

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

assert_eq!(dictionary, res);
}

#[meilisearch_test]
async fn test_set_empty_dicitonary(client: Client, index: Index) {
let dictionary: Vec<&str> = vec![];
let task_info = index.set_dictionary(&dictionary).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

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

assert_eq!(dictionary, res);
}


#[meilisearch_test]
async fn test_reset_dictionary(client: Client, index: Index) {
let dictionary: Vec<&str> = vec![];
let task_info = index.reset_dictionary().await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

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

assert_eq!(dictionary, res);
}

#[meilisearch_test]
async fn test_get_pagination(index: Index) {
let pagination = PaginationSetting {
Expand Down