Skip to content

Commit f3f00f4

Browse files
meili-bors[bot]hasan-rajacurquiza
authored
Merge #529
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]>
2 parents 1bdfcb3 + 12b4821 commit f3f00f4

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,24 @@ reset_faceting_settings_1: |-
595595
.reset_faceting()
596596
.await
597597
.unwrap();
598+
get_dictionary_1: |-
599+
let task: TaskInfo = client
600+
.index('books')
601+
.get_dictionary()
602+
.await
603+
.unwrap();
604+
update_dictionary_1: |-
605+
let task: TaskInfo = client
606+
.index('books')
607+
.set_dictionary(['J. R. R.', 'W. E. B.'])
608+
.await
609+
.unwrap();
610+
reset_dictionary_1: |-
611+
let task: TaskInfo = client
612+
.index('books')
613+
.reset_dictionary()
614+
.await
615+
.unwrap();
598616
get_index_stats_1: |-
599617
let stats: IndexStats = client
600618
.index("movies")

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ pub enum ErrorCode {
243243
InvalidSettingsDistinctAttributes,
244244
InvalidSettingsTypoTolerance,
245245
InvalidSettingsFaceting,
246+
InvalidSettingsDictionary,
246247
InvalidSettingsPagination,
247248
InvalidTaskBeforeEnqueuedAt,
248249
InvalidTaskAfterEnqueuedAt,

src/settings.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ pub struct Settings {
9595
/// TypoTolerance settings
9696
#[serde(skip_serializing_if = "Option::is_none")]
9797
pub typo_tolerance: Option<TypoToleranceSettings>,
98+
/// Dictionary settings.
99+
#[serde(skip_serializing_if = "Option::is_none")]
100+
pub dictionary: Option<Vec<String>>,
98101
}
99102

100103
#[allow(missing_docs)]
@@ -243,6 +246,21 @@ impl Settings {
243246
..self
244247
}
245248
}
249+
250+
pub fn with_dictionary(
251+
self,
252+
dictionary: impl IntoIterator<Item = impl AsRef<str>>,
253+
) -> Settings {
254+
Settings {
255+
dictionary: Some(
256+
dictionary
257+
.into_iter()
258+
.map(|v| v.as_ref().to_string())
259+
.collect(),
260+
),
261+
..self
262+
}
263+
}
246264
}
247265

248266
impl Index {
@@ -604,6 +622,38 @@ impl Index {
604622
.await
605623
}
606624

625+
/// Get [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) of the [Index].
626+
///
627+
/// # Example
628+
///
629+
/// ```
630+
/// # use meilisearch_sdk::{client::*, indexes::*};
631+
/// #
632+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
633+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
634+
/// #
635+
/// # futures::executor::block_on(async move {
636+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
637+
/// # client.create_index("get_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
638+
/// let index = client.index("get_dictionary");
639+
///
640+
/// let dictionary = index.get_dictionary().await.unwrap();
641+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
642+
/// # });
643+
/// ```
644+
pub async fn get_dictionary(&self) -> Result<Vec<String>, Error> {
645+
request::<(), (), Vec<String>>(
646+
&format!(
647+
"{}/indexes/{}/settings/dictionary",
648+
self.client.host, self.uid
649+
),
650+
self.client.get_api_key(),
651+
Method::Get { query: () },
652+
200,
653+
)
654+
.await
655+
}
656+
607657
/// Get [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) of the [Index].
608658
///
609659
/// ```
@@ -1089,6 +1139,47 @@ impl Index {
10891139
.await
10901140
}
10911141

1142+
/// Update [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) of the [Index].
1143+
///
1144+
/// # Example
1145+
///
1146+
/// ```
1147+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1148+
/// #
1149+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1150+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1151+
/// #
1152+
/// # futures::executor::block_on(async move {
1153+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
1154+
/// # client.create_index("set_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1155+
/// let mut index = client.index("set_dictionary");
1156+
///
1157+
/// let task = index.set_dictionary(["J. K.", "J. R. R."]).await.unwrap();
1158+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1159+
/// # });
1160+
/// ```
1161+
pub async fn set_dictionary(
1162+
&self,
1163+
dictionary: impl IntoIterator<Item = impl AsRef<str>>,
1164+
) -> Result<TaskInfo, Error> {
1165+
request::<(), Vec<String>, TaskInfo>(
1166+
&format!(
1167+
"{}/indexes/{}/settings/dictionary",
1168+
self.client.host, self.uid
1169+
),
1170+
self.client.get_api_key(),
1171+
Method::Put {
1172+
query: (),
1173+
body: dictionary
1174+
.into_iter()
1175+
.map(|v| v.as_ref().to_string())
1176+
.collect(),
1177+
},
1178+
202,
1179+
)
1180+
.await
1181+
}
1182+
10921183
/// Update [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
10931184
///
10941185
/// # Example
@@ -1487,6 +1578,38 @@ impl Index {
14871578
.await
14881579
}
14891580

1581+
/// Reset [dictionary](https://www.meilisearch.com/docs/reference/api/settings#dictionary) of the [Index].
1582+
///
1583+
/// # Example
1584+
///
1585+
/// ```
1586+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1587+
/// #
1588+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1589+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1590+
/// #
1591+
/// # futures::executor::block_on(async move {
1592+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
1593+
/// # client.create_index("reset_dictionary", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1594+
/// let mut index = client.index("reset_dictionary");
1595+
///
1596+
/// let task = index.reset_dictionary().await.unwrap();
1597+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1598+
/// # });
1599+
/// ```
1600+
pub async fn reset_dictionary(&self) -> Result<TaskInfo, Error> {
1601+
request::<(), (), TaskInfo>(
1602+
&format!(
1603+
"{}/indexes/{}/settings/dictionary",
1604+
self.client.host, self.uid
1605+
),
1606+
self.client.get_api_key(),
1607+
Method::Delete { query: () },
1608+
202,
1609+
)
1610+
.await
1611+
}
1612+
14901613
/// Reset [typo tolerance](https://www.meilisearch.com/docs/learn/configuration/typo_tolerance#typo-tolerance) settings of the [Index].
14911614
///
14921615
/// # Example
@@ -1579,6 +1702,48 @@ mod tests {
15791702
assert_eq!(faceting, res);
15801703
}
15811704

1705+
#[meilisearch_test]
1706+
async fn test_get_dicitonary(index: Index) {
1707+
let dictionary: Vec<String> = vec![];
1708+
1709+
let res = index.get_dictionary().await.unwrap();
1710+
1711+
assert_eq!(dictionary, res);
1712+
}
1713+
1714+
#[meilisearch_test]
1715+
async fn test_set_dicitonary(client: Client, index: Index) {
1716+
let dictionary: Vec<&str> = vec!["J. K.", "J. R. R."];
1717+
let task_info = index.set_dictionary(&dictionary).await.unwrap();
1718+
client.wait_for_task(task_info, None, None).await.unwrap();
1719+
1720+
let res = index.get_dictionary().await.unwrap();
1721+
1722+
assert_eq!(dictionary, res);
1723+
}
1724+
1725+
#[meilisearch_test]
1726+
async fn test_set_empty_dicitonary(client: Client, index: Index) {
1727+
let dictionary: Vec<&str> = vec![];
1728+
let task_info = index.set_dictionary(&dictionary).await.unwrap();
1729+
client.wait_for_task(task_info, None, None).await.unwrap();
1730+
1731+
let res = index.get_dictionary().await.unwrap();
1732+
1733+
assert_eq!(dictionary, res);
1734+
}
1735+
1736+
#[meilisearch_test]
1737+
async fn test_reset_dictionary(client: Client, index: Index) {
1738+
let dictionary: Vec<&str> = vec![];
1739+
let task_info = index.reset_dictionary().await.unwrap();
1740+
client.wait_for_task(task_info, None, None).await.unwrap();
1741+
1742+
let res = index.get_dictionary().await.unwrap();
1743+
1744+
assert_eq!(dictionary, res);
1745+
}
1746+
15821747
#[meilisearch_test]
15831748
async fn test_get_pagination(index: Index) {
15841749
let pagination = PaginationSetting {

0 commit comments

Comments
 (0)