Skip to content

Commit

Permalink
Merge #529
Browse files Browse the repository at this point in the history
529: Add feature: dictionary settings r=curquiza a=hasan-raja

# Pull Request

## Related issue
Fixes #527

## What does this PR do?
 - Added the `dictionary` setting to the `Settings` struct, providing users with a flexible way to manage their search dictionary.
 - Implemented three new methods for interacting with the `dictionary` setting:
   - `get_dictionary`: Retrieves the current dictionary configuration.
   - `set_dictionary`: Updates the dictionary with a new configuration.
   - `reset_dictionary`: Resets the dictionary configuration to default values.
- Created comprehensive integration tests to ensure the correctness and robustness of these new methods.

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Added integration tests for the new methods.
- [x] Verified the correctness of existing unit tests.
- [x] Ensured that the codebase adheres to the project's coding style guidelines.
- [x] Updated the `.code-samples.meilisearch.yaml` file to incorporate new examples for the `get_dictionary`, `set_dictionary`, and `reset_dictionary` methods.

Please review and provide feedback on the changes introduced in this pull request.


Co-authored-by: hasan-raja <[email protected]>
Co-authored-by: Clémentine U. - curqui <[email protected]>
  • Loading branch information
3 people authored Oct 11, 2023
2 parents 1bdfcb3 + 12b4821 commit f3f00f4
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
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
165 changes: 165 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,48 @@ 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

0 comments on commit f3f00f4

Please sign in to comment.