Skip to content

Commit a641093

Browse files
Merge #620
620: Add support for language settings and search parameter r=ManyTheFish a=necocen # Pull Request ## Related issue Fixes #611 ## What does this PR do? - Added `locales` parameter and `with_locales` method to `SearchQuery` struct - Added localized attributes related methods to `Index` and `Settings` - Added tests - Added code samples - search_parameter_reference_locales_1 - get_localized_attribute_settings_1 - update_localized_attribute_settings_1 - reset_localized_attribute_settings_1 ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: necocen <necocen@gmail.com>
2 parents 38309ef + f74e472 commit a641093

File tree

3 files changed

+224
-0
lines changed

3 files changed

+224
-0
lines changed

.code-samples.meilisearch.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -1739,3 +1739,34 @@ search_parameter_reference_ranking_score_threshold_1: |-
17391739
.execute()
17401740
.await
17411741
.unwrap();
1742+
search_parameter_reference_locales_1: |-
1743+
let res = client
1744+
.index("books")
1745+
.search()
1746+
.with_query("進撃の巨人")
1747+
.with_locales(&["jpn"])
1748+
.execute()
1749+
.await
1750+
.unwrap();
1751+
get_localized_attribute_settings_1: |-
1752+
let localized_attributes: Option<Vec<LocalizedAttributes>> = client
1753+
.index("books")
1754+
.get_localized_attributes()
1755+
.await
1756+
.unwrap();
1757+
update_localized_attribute_settings_1: |-
1758+
let localized_attributes = vec![LocalizedAttributes {
1759+
locales: vec!["jpn".to_string()],
1760+
attribute_patterns: vec!["*_ja".to_string()],
1761+
}];
1762+
let task: TaskInfo = client
1763+
.index("books")
1764+
.set_localized_attributes(&localizced_attributes)
1765+
.await
1766+
.unwrap();
1767+
reset_localized_attribute_settings_1: |-
1768+
let task: TaskInfo = client
1769+
.index("books")
1770+
.reset_localized_attributes()
1771+
.await
1772+
.unwrap();

src/search.rs

+21
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,10 @@ pub struct SearchQuery<'a, Http: HttpClient> {
344344
#[serde(skip_serializing_if = "Option::is_none")]
345345
pub ranking_score_threshold: Option<f64>,
346346

347+
/// Defines the language of the search query.
348+
#[serde(skip_serializing_if = "Option::is_none")]
349+
pub locales: Option<&'a [&'a str]>,
350+
347351
#[serde(skip_serializing_if = "Option::is_none")]
348352
pub(crate) index_uid: Option<&'a str>,
349353
}
@@ -377,6 +381,7 @@ impl<'a, Http: HttpClient> SearchQuery<'a, Http> {
377381
index_uid: None,
378382
distinct: None,
379383
ranking_score_threshold: None,
384+
locales: None,
380385
}
381386
}
382387
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> {
580585
self.ranking_score_threshold = Some(ranking_score_threshold);
581586
self
582587
}
588+
pub fn with_locales<'b>(&'b mut self, locales: &'a [&'a str]) -> &'b mut SearchQuery<'a, Http> {
589+
self.locales = Some(locales);
590+
self
591+
}
583592
pub fn build(&mut self) -> SearchQuery<'a, Http> {
584593
self.clone()
585594
}
@@ -1161,6 +1170,18 @@ mod tests {
11611170
Ok(())
11621171
}
11631172

1173+
#[meilisearch_test]
1174+
async fn test_query_locales(client: Client, index: Index) -> Result<(), Error> {
1175+
setup_test_index(&client, &index).await?;
1176+
1177+
let mut query = SearchQuery::new(&index);
1178+
query.with_query("Harry Styles");
1179+
query.with_locales(&["eng"]);
1180+
let results: SearchResults<Document> = index.execute_query(&query).await.unwrap();
1181+
assert_eq!(results.hits.len(), 7);
1182+
Ok(())
1183+
}
1184+
11641185
#[meilisearch_test]
11651186
async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> {
11661187
setup_test_index(&client, &index).await?;

src/settings.rs

+172
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ pub struct FacetingSettings {
3636
pub max_values_per_facet: usize,
3737
}
3838

39+
#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)]
40+
#[serde(rename_all = "camelCase")]
41+
pub struct LocalizedAttributes {
42+
pub locales: Vec<String>,
43+
pub attribute_patterns: Vec<String>,
44+
}
45+
3946
/// Struct reprensenting a set of settings.
4047
///
4148
/// You can build this struct using the builder syntax.
@@ -112,6 +119,9 @@ pub struct Settings {
112119
/// Remove tokens from Meilisearch's default [list of word separators](https://www.meilisearch.com/docs/learn/engine/datatypes#string).
113120
#[serde(skip_serializing_if = "Option::is_none")]
114121
pub non_separator_tokens: Option<Vec<String>>,
122+
/// LocalizedAttributes settings.
123+
#[serde(skip_serializing_if = "Option::is_none")]
124+
pub localized_attributes: Option<Vec<LocalizedAttributes>>,
115125
}
116126

117127
#[allow(missing_docs)]
@@ -336,6 +346,17 @@ impl Settings {
336346
..self
337347
}
338348
}
349+
350+
#[must_use]
351+
pub fn with_localized_attributes(
352+
self,
353+
localized_attributes: impl IntoIterator<Item = LocalizedAttributes>,
354+
) -> Settings {
355+
Settings {
356+
localized_attributes: Some(localized_attributes.into_iter().collect()),
357+
..self
358+
}
359+
}
339360
}
340361

341362
impl<Http: HttpClient> Index<Http> {
@@ -900,6 +921,39 @@ impl<Http: HttpClient> Index<Http> {
900921
.await
901922
}
902923

924+
/// Get [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) settings of the [Index].
925+
///
926+
/// ```
927+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::LocalizedAttributes};
928+
/// #
929+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
930+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
931+
/// #
932+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
933+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
934+
/// # client.create_index("get_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
935+
/// let index = client.index("get_localized_attributes");
936+
///
937+
/// let localized_attributes = index.get_localized_attributes().await.unwrap();
938+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
939+
/// # });
940+
/// ```
941+
pub async fn get_localized_attributes(
942+
&self,
943+
) -> Result<Option<Vec<LocalizedAttributes>>, Error> {
944+
self.client
945+
.http_client
946+
.request::<(), (), Option<Vec<LocalizedAttributes>>>(
947+
&format!(
948+
"{}/indexes/{}/settings/localized-attributes",
949+
self.client.host, self.uid
950+
),
951+
Method::Get { query: () },
952+
200,
953+
)
954+
.await
955+
}
956+
903957
/// Update [settings](../settings/struct.Settings) of the [Index].
904958
///
905959
/// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged.
@@ -1611,6 +1665,50 @@ impl<Http: HttpClient> Index<Http> {
16111665
.await
16121666
}
16131667

1668+
/// Update [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) settings of the [Index].
1669+
///
1670+
/// # Example
1671+
///
1672+
/// ```
1673+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{LocalizedAttributes}};
1674+
/// #
1675+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1676+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1677+
/// #
1678+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1679+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1680+
/// # client.create_index("set_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1681+
/// let mut index = client.index("set_localized_attributes");
1682+
///
1683+
/// let localized_attributes = vec![LocalizedAttributes {
1684+
/// locales: vec!["jpn".to_string()],
1685+
/// attribute_patterns: vec!["*_ja".to_string()],
1686+
/// }];
1687+
///
1688+
/// let task = index.set_localized_attributes(&localized_attributes).await.unwrap();
1689+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1690+
/// # });
1691+
/// ```
1692+
pub async fn set_localized_attributes(
1693+
&self,
1694+
localized_attributes: &Vec<LocalizedAttributes>,
1695+
) -> Result<TaskInfo, Error> {
1696+
self.client
1697+
.http_client
1698+
.request::<(), &Vec<LocalizedAttributes>, TaskInfo>(
1699+
&format!(
1700+
"{}/indexes/{}/settings/localized-attributes",
1701+
self.client.host, self.uid
1702+
),
1703+
Method::Put {
1704+
query: (),
1705+
body: localized_attributes,
1706+
},
1707+
202,
1708+
)
1709+
.await
1710+
}
1711+
16141712
/// Reset [Settings] of the [Index].
16151713
///
16161714
/// All settings will be reset to their [default value](https://www.meilisearch.com/docs/reference/api/settings#reset-settings).
@@ -2172,6 +2270,39 @@ impl<Http: HttpClient> Index<Http> {
21722270
)
21732271
.await
21742272
}
2273+
2274+
/// Reset [localized attributes](https://www.meilisearch.com/docs/reference/api/settings#localized-attributes-object) settings of the [Index].
2275+
///
2276+
/// # Example
2277+
///
2278+
/// ```
2279+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
2280+
/// #
2281+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
2282+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
2283+
/// #
2284+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
2285+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2286+
/// # client.create_index("reset_localized_attributes", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2287+
/// let index = client.index("reset_localized_attributes");
2288+
///
2289+
/// let task = index.reset_localized_attributes().await.unwrap();
2290+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2291+
/// # });
2292+
/// ```
2293+
pub async fn reset_localized_attributes(&self) -> Result<TaskInfo, Error> {
2294+
self.client
2295+
.http_client
2296+
.request::<(), (), TaskInfo>(
2297+
&format!(
2298+
"{}/indexes/{}/settings/localized-attributes",
2299+
self.client.host, self.uid
2300+
),
2301+
Method::Delete { query: () },
2302+
202,
2303+
)
2304+
.await
2305+
}
21752306
}
21762307

21772308
#[cfg(test)]
@@ -2522,4 +2653,45 @@ mod tests {
25222653
let res = index.get_dictionary().await.unwrap();
25232654
assert_eq!(separator, res);
25242655
}
2656+
2657+
#[meilisearch_test]
2658+
async fn test_get_localized_attributes(index: Index) {
2659+
let res = index.get_localized_attributes().await.unwrap();
2660+
assert_eq!(None, res);
2661+
}
2662+
2663+
#[meilisearch_test]
2664+
async fn test_set_localized_attributes(client: Client, index: Index) {
2665+
let localized_attributes = vec![LocalizedAttributes {
2666+
locales: vec!["jpn".to_string()],
2667+
attribute_patterns: vec!["*_ja".to_string()],
2668+
}];
2669+
let task_info = index
2670+
.set_localized_attributes(&localized_attributes)
2671+
.await
2672+
.unwrap();
2673+
client.wait_for_task(task_info, None, None).await.unwrap();
2674+
2675+
let res = index.get_localized_attributes().await.unwrap();
2676+
assert_eq!(Some(localized_attributes), res);
2677+
}
2678+
2679+
#[meilisearch_test]
2680+
async fn test_reset_localized_attributes(client: Client, index: Index) {
2681+
let localized_attributes = vec![LocalizedAttributes {
2682+
locales: vec!["jpn".to_string()],
2683+
attribute_patterns: vec!["*_ja".to_string()],
2684+
}];
2685+
let task_info = index
2686+
.set_localized_attributes(&localized_attributes)
2687+
.await
2688+
.unwrap();
2689+
client.wait_for_task(task_info, None, None).await.unwrap();
2690+
2691+
let reset_task = index.reset_localized_attributes().await.unwrap();
2692+
client.wait_for_task(reset_task, None, None).await.unwrap();
2693+
2694+
let res = index.get_localized_attributes().await.unwrap();
2695+
assert_eq!(None, res);
2696+
}
25252697
}

0 commit comments

Comments
 (0)