From f374f81524b5c63dd09650e1d2eff5338400e1bb Mon Sep 17 00:00:00 2001 From: necocen Date: Fri, 11 Oct 2024 02:10:31 +0900 Subject: [PATCH 1/3] Added locales parameter to SearchQuery --- .code-samples.meilisearch.yaml | 9 +++++++++ src/search.rs | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 00eb3c5f..6ebd785b 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1739,3 +1739,12 @@ search_parameter_reference_ranking_score_threshold_1: |- .execute() .await .unwrap(); +search_parameter_reference_locales_1: |- + let res = client + .index("books") + .search() + .with_query("進撃の巨人") + .with_locales(&["jpn"]) + .execute() + .await + .unwrap(); diff --git a/src/search.rs b/src/search.rs index 73272202..bcfd5762 100644 --- a/src/search.rs +++ b/src/search.rs @@ -344,6 +344,10 @@ pub struct SearchQuery<'a, Http: HttpClient> { #[serde(skip_serializing_if = "Option::is_none")] pub ranking_score_threshold: Option, + /// Defines the language of the search query. + #[serde(skip_serializing_if = "Option::is_none")] + pub locales: Option<&'a [&'a str]>, + #[serde(skip_serializing_if = "Option::is_none")] pub(crate) index_uid: Option<&'a str>, } @@ -377,6 +381,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> { index_uid: None, distinct: None, ranking_score_threshold: None, + locales: None, } } pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut SearchQuery<'a, Http> { @@ -580,6 +585,10 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> { self.ranking_score_threshold = Some(ranking_score_threshold); self } + pub fn with_locales<'b>(&'b mut self, locales: &'a [&'a str]) -> &'b mut SearchQuery<'a, Http> { + self.locales = Some(locales); + self + } pub fn build(&mut self) -> SearchQuery<'a, Http> { self.clone() } @@ -1161,6 +1170,18 @@ mod tests { Ok(()) } + #[meilisearch_test] + async fn test_query_locales(client: Client, index: Index) -> Result<(), Error> { + setup_test_index(&client, &index).await?; + + let mut query = SearchQuery::new(&index); + query.with_query("Harry Styles"); + query.with_locales(&["eng"]); + let results: SearchResults = index.execute_query(&query).await.unwrap(); + assert_eq!(results.hits.len(), 7); + Ok(()) + } + #[meilisearch_test] async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> { setup_test_index(&client, &index).await?; From 710f5f365c9dbaeaef4f12cee0d7f7ab61b16fa6 Mon Sep 17 00:00:00 2001 From: necocen Date: Fri, 11 Oct 2024 02:11:19 +0900 Subject: [PATCH 2/3] Added localized attributes support to Index --- .code-samples.meilisearch.yaml | 22 +++++ src/settings.rs | 158 +++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 6ebd785b..dd56fe90 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -1748,3 +1748,25 @@ search_parameter_reference_locales_1: |- .execute() .await .unwrap(); +get_localized_attribute_settings_1: |- + let localized_attributes: Option> = client + .index("books") + .get_localized_attributes() + .await + .unwrap(); +update_localized_attribute_settings_1: |- + let localized_attributes = vec![LocalizedAttributes { + locales: vec!["jpn".to_string()], + attribute_patterns: vec!["*_ja".to_string()], + }]; + let task: TaskInfo = client + .index("books") + .set_localized_attributes(&localizced_attributes) + .await + .unwrap(); +reset_localized_attribute_settings_1: |- + let task: TaskInfo = client + .index("books") + .reset_localized_attributes() + .await + .unwrap(); diff --git a/src/settings.rs b/src/settings.rs index 97993aff..a78ce400 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -36,6 +36,13 @@ pub struct FacetingSettings { pub max_values_per_facet: usize, } +#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct LocalizedAttributes { + pub locales: Vec, + pub attribute_patterns: Vec, +} + /// Struct reprensenting a set of settings. /// /// You can build this struct using the builder syntax. @@ -900,6 +907,39 @@ impl Index { .await } + /// Get [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) settings of the [Index]. + /// + /// ``` + /// # use meilisearch_sdk::{client::*, indexes::*, settings::LocalizedAttributes}; + /// # + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); + /// # + /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { + /// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); + /// # client.create_index("get_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// let index = client.index("get_localized_attributes"); + /// + /// let localized_attributes = index.get_localized_attributes().await.unwrap(); + /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// # }); + /// ``` + pub async fn get_localized_attributes( + &self, + ) -> Result>, Error> { + self.client + .http_client + .request::<(), (), Option>>( + &format!( + "{}/indexes/{}/settings/localized-attributes", + self.client.host, self.uid + ), + Method::Get { query: () }, + 200, + ) + .await + } + /// Update [settings](../settings/struct.Settings) of the [Index]. /// /// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged. @@ -1611,6 +1651,50 @@ impl Index { .await } + /// Update [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) settings of the [Index]. + /// + /// # Example + /// + /// ``` + /// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{LocalizedAttributes}}; + /// # + /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700"); + /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey"); + /// # + /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { + /// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); + /// # client.create_index("set_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// let mut index = client.index("set_localized_attributes"); + /// + /// let localized_attributes = vec![LocalizedAttributes { + /// locales: vec!["jpn".to_string()], + /// attribute_patterns: vec!["*_ja".to_string()], + /// }]; + /// + /// let task = index.set_localized_attributes(&localized_attributes).await.unwrap(); + /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// # }); + /// ``` + pub async fn set_localized_attributes( + &self, + localized_attributes: &Vec, + ) -> Result { + self.client + .http_client + .request::<(), &Vec, TaskInfo>( + &format!( + "{}/indexes/{}/settings/localized-attributes", + self.client.host, self.uid + ), + Method::Put { + query: (), + body: localized_attributes, + }, + 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). @@ -2172,6 +2256,39 @@ impl Index { ) .await } + + /// Reset [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) 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"); + /// # + /// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async { + /// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap(); + /// # client.create_index("reset_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// let index = client.index("reset_localized_attributes"); + /// + /// let task = index.reset_localized_attributes().await.unwrap(); + /// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap(); + /// # }); + /// ``` + pub async fn reset_localized_attributes(&self) -> Result { + self.client + .http_client + .request::<(), (), TaskInfo>( + &format!( + "{}/indexes/{}/settings/localized-attributes", + self.client.host, self.uid + ), + Method::Delete { query: () }, + 202, + ) + .await + } } #[cfg(test)] @@ -2522,4 +2639,45 @@ mod tests { let res = index.get_dictionary().await.unwrap(); assert_eq!(separator, res); } + + #[meilisearch_test] + async fn test_get_localized_attributes(index: Index) { + let res = index.get_localized_attributes().await.unwrap(); + assert_eq!(None, res); + } + + #[meilisearch_test] + async fn test_set_localized_attributes(client: Client, index: Index) { + let localized_attributes = vec![LocalizedAttributes { + locales: vec!["jpn".to_string()], + attribute_patterns: vec!["*_ja".to_string()], + }]; + let task_info = index + .set_localized_attributes(&localized_attributes) + .await + .unwrap(); + client.wait_for_task(task_info, None, None).await.unwrap(); + + let res = index.get_localized_attributes().await.unwrap(); + assert_eq!(Some(localized_attributes), res); + } + + #[meilisearch_test] + async fn test_reset_localized_attributes(client: Client, index: Index) { + let localized_attributes = vec![LocalizedAttributes { + locales: vec!["jpn".to_string()], + attribute_patterns: vec!["*_ja".to_string()], + }]; + let task_info = index + .set_localized_attributes(&localized_attributes) + .await + .unwrap(); + client.wait_for_task(task_info, None, None).await.unwrap(); + + let reset_task = index.reset_localized_attributes().await.unwrap(); + client.wait_for_task(reset_task, None, None).await.unwrap(); + + let res = index.get_localized_attributes().await.unwrap(); + assert_eq!(None, res); + } } From f74e472024040c56f6534605a360e7770ea4447a Mon Sep 17 00:00:00 2001 From: necocen Date: Fri, 11 Oct 2024 20:55:15 +0900 Subject: [PATCH 3/3] Added localized_attributes parameter and builder method to Settings --- src/settings.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/settings.rs b/src/settings.rs index a78ce400..55722628 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -119,6 +119,9 @@ pub struct Settings { /// Remove tokens from Meilisearch's default [list of word separators](https://www.meilisearch.com/docs/learn/engine/datatypes#string). #[serde(skip_serializing_if = "Option::is_none")] pub non_separator_tokens: Option>, + /// LocalizedAttributes settings. + #[serde(skip_serializing_if = "Option::is_none")] + pub localized_attributes: Option>, } #[allow(missing_docs)] @@ -343,6 +346,17 @@ impl Settings { ..self } } + + #[must_use] + pub fn with_localized_attributes( + self, + localized_attributes: impl IntoIterator, + ) -> Settings { + Settings { + localized_attributes: Some(localized_attributes.into_iter().collect()), + ..self + } + } } impl Index {