Skip to content

Commit fafebbf

Browse files
Merge #595
595: Supports text separation r=irevoire a=NoodleSamaChan # Pull Request ## Related issue #526 ## What does this PR do? Added two new settings: separatorTokens and nonSeparatorTokens with get, update, and reset methods associated. ## 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: NoodleSamaChan <[email protected]> Co-authored-by: NoodleSamaChan <[email protected]>
2 parents cfbcebe + 968dfab commit fafebbf

File tree

2 files changed

+344
-0
lines changed

2 files changed

+344
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,42 @@ reset_faceting_settings_1: |-
596596
.reset_faceting()
597597
.await
598598
.unwrap();
599+
get_separator_tokens_1: |-
600+
let task: TaskInfo = client
601+
.index('articles')
602+
.get_dictionary()
603+
.await
604+
.unwrap();
605+
update_separator_tokens_1: |-
606+
let task: TaskInfo = client
607+
.index('articles')
608+
.set_dictionary(['|', '&hellip;'])
609+
.await
610+
.unwrap();
611+
reset_separator_tokens_1: |-
612+
let task: TaskInfo = client
613+
.index('articles')
614+
.reset_dictionary()
615+
.await
616+
.unwrap();
617+
get_non_separator_tokens_1: |-
618+
let task: TaskInfo = client
619+
.index('articles')
620+
.get_dictionary()
621+
.await
622+
.unwrap();
623+
update_non_separator_tokens_1: |-
624+
let task: TaskInfo = client
625+
.index('articles')
626+
.set_dictionary(['@', '#'])
627+
.await
628+
.unwrap();
629+
reset_non_separator_tokens_1: |-
630+
let task: TaskInfo = client
631+
.index('articles')
632+
.reset_dictionary()
633+
.await
634+
.unwrap();
599635
get_dictionary_1: |-
600636
let task: TaskInfo = client
601637
.index('books')

src/settings.rs

Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ pub struct Settings {
106106
/// SearchCutoffMs settings.
107107
#[serde(skip_serializing_if = "Option::is_none")]
108108
pub search_cutoff_ms: Option<u64>,
109+
/// Configure strings as custom separator tokens indicating where a word ends and begins.
110+
#[serde(skip_serializing_if = "Option::is_none")]
111+
pub separator_tokens: Option<Vec<String>>,
112+
/// Remove tokens from Meilisearch's default [list of word separators](https://www.meilisearch.com/docs/learn/engine/datatypes#string).
113+
#[serde(skip_serializing_if = "Option::is_none")]
114+
pub non_separator_tokens: Option<Vec<String>>,
109115
}
110116

111117
#[allow(missing_docs)]
@@ -298,6 +304,38 @@ impl Settings {
298304
..self
299305
}
300306
}
307+
308+
#[must_use]
309+
pub fn with_separation_tokens(
310+
self,
311+
separator_tokens: impl IntoIterator<Item = impl AsRef<str>>,
312+
) -> Settings {
313+
Settings {
314+
separator_tokens: Some(
315+
separator_tokens
316+
.into_iter()
317+
.map(|v| v.as_ref().to_string())
318+
.collect(),
319+
),
320+
..self
321+
}
322+
}
323+
324+
#[must_use]
325+
pub fn with_non_separation_tokens(
326+
self,
327+
non_separator_tokens: impl IntoIterator<Item = impl AsRef<str>>,
328+
) -> Settings {
329+
Settings {
330+
non_separator_tokens: Some(
331+
non_separator_tokens
332+
.into_iter()
333+
.map(|v| v.as_ref().to_string())
334+
.collect(),
335+
),
336+
..self
337+
}
338+
}
301339
}
302340

303341
impl<Http: HttpClient> Index<Http> {
@@ -800,6 +838,68 @@ impl<Http: HttpClient> Index<Http> {
800838
.await
801839
}
802840

841+
/// Get [separator token](https://www.meilisearch.com/docs/reference/api/settings#separator-tokens) of the [Index].
842+
///
843+
/// ```
844+
/// # use meilisearch_sdk::{client::*, indexes::*};
845+
/// #
846+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
847+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
848+
/// #
849+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
850+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
851+
/// # client.create_index("get_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
852+
/// let index = client.index("get_separator_tokens");
853+
///
854+
/// let separator_tokens = index.get_separator_tokens().await.unwrap();
855+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
856+
/// # });
857+
/// ```
858+
pub async fn get_separator_tokens(&self) -> Result<Vec<String>, Error> {
859+
self.client
860+
.http_client
861+
.request::<(), (), Vec<String>>(
862+
&format!(
863+
"{}/indexes/{}/settings/separator-tokens",
864+
self.client.host, self.uid
865+
),
866+
Method::Get { query: () },
867+
200,
868+
)
869+
.await
870+
}
871+
872+
/// Get [non separator token](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) of the [Index].
873+
///
874+
/// ```
875+
/// # use meilisearch_sdk::{client::*, indexes::*};
876+
/// #
877+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
878+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
879+
/// #
880+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
881+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
882+
/// # client.create_index("get_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
883+
/// let index = client.index("get_non_separator_tokens");
884+
///
885+
/// let non_separator_tokens = index.get_non_separator_tokens().await.unwrap();
886+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
887+
/// # });
888+
/// ```
889+
pub async fn get_non_separator_tokens(&self) -> Result<Vec<String>, Error> {
890+
self.client
891+
.http_client
892+
.request::<(), (), Vec<String>>(
893+
&format!(
894+
"{}/indexes/{}/settings/non-separator-tokens",
895+
self.client.host, self.uid
896+
),
897+
Method::Get { query: () },
898+
200,
899+
)
900+
.await
901+
}
902+
803903
/// Update [settings](../settings/struct.Settings) of the [Index].
804904
///
805905
/// Updates in the settings are partial. This means that any parameters corresponding to a `None` value will be left unchanged.
@@ -1354,6 +1454,88 @@ impl<Http: HttpClient> Index<Http> {
13541454
.await
13551455
}
13561456

1457+
/// Update [separator tokens](https://www.meilisearch.com/docs/reference/api/settings#separator-tokens) settings of the [Index].
1458+
///
1459+
/// # Example
1460+
///
1461+
/// ```
1462+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{TypoToleranceSettings, MinWordSizeForTypos}};
1463+
/// #
1464+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1465+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1466+
/// #
1467+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1468+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1469+
/// # client.create_index("set_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1470+
/// let mut index = client.index("set_separator_tokens");
1471+
///
1472+
/// let separator_token: Vec<String> = vec!["@".to_string(), "#".to_string()];
1473+
///
1474+
/// let task = index.set_separator_tokens(&separator_token).await.unwrap();
1475+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1476+
/// # });
1477+
/// ```
1478+
pub async fn set_separator_tokens(
1479+
&self,
1480+
separator_token: &Vec<String>,
1481+
) -> Result<TaskInfo, Error> {
1482+
self.client
1483+
.http_client
1484+
.request::<(), &Vec<String>, TaskInfo>(
1485+
&format!(
1486+
"{}/indexes/{}/settings/separator-tokens",
1487+
self.client.host, self.uid
1488+
),
1489+
Method::Put {
1490+
query: (),
1491+
body: separator_token,
1492+
},
1493+
202,
1494+
)
1495+
.await
1496+
}
1497+
1498+
/// Update [non separator tokens](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) settings of the [Index].
1499+
///
1500+
/// # Example
1501+
///
1502+
/// ```
1503+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{TypoToleranceSettings, MinWordSizeForTypos}};
1504+
/// #
1505+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1506+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1507+
/// #
1508+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1509+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1510+
/// # client.create_index("set_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1511+
/// let mut index = client.index("set_non_separator_tokens");
1512+
///
1513+
/// let non_separator_token: Vec<String> = vec!["@".to_string(), "#".to_string()];
1514+
///
1515+
/// let task = index.set_non_separator_tokens(&non_separator_token).await.unwrap();
1516+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1517+
/// # });
1518+
/// ```
1519+
pub async fn set_non_separator_tokens(
1520+
&self,
1521+
non_separator_token: &Vec<String>,
1522+
) -> Result<TaskInfo, Error> {
1523+
self.client
1524+
.http_client
1525+
.request::<(), &Vec<String>, TaskInfo>(
1526+
&format!(
1527+
"{}/indexes/{}/settings/non-separator-tokens",
1528+
self.client.host, self.uid
1529+
),
1530+
Method::Put {
1531+
query: (),
1532+
body: non_separator_token,
1533+
},
1534+
202,
1535+
)
1536+
.await
1537+
}
1538+
13571539
/// Update [proximity-precision](https://www.meilisearch.com/docs/learn/configuration/proximity-precision) settings of the [Index].
13581540
///
13591541
/// # Example
@@ -1924,6 +2106,72 @@ impl<Http: HttpClient> Index<Http> {
19242106
)
19252107
.await
19262108
}
2109+
2110+
/// Reset [search cutoff](https://www.meilisearch.com/docs/reference/api/settings#search-cutoff) settings of the [Index].
2111+
///
2112+
/// # Example
2113+
///
2114+
/// ```
2115+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
2116+
/// #
2117+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
2118+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
2119+
/// #
2120+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
2121+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2122+
/// # client.create_index("reset_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2123+
/// let mut index = client.index("reset_separator_tokens");
2124+
///
2125+
/// let task = index.reset_separator_tokens().await.unwrap();
2126+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2127+
/// # });
2128+
/// ```
2129+
pub async fn reset_separator_tokens(&self) -> Result<TaskInfo, Error> {
2130+
self.client
2131+
.http_client
2132+
.request::<(), (), TaskInfo>(
2133+
&format!(
2134+
"{}/indexes/{}/settings/separator-tokens",
2135+
self.client.host, self.uid
2136+
),
2137+
Method::Delete { query: () },
2138+
202,
2139+
)
2140+
.await
2141+
}
2142+
2143+
/// Reset [non separator tokens](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) settings of the [Index].
2144+
///
2145+
/// # Example
2146+
///
2147+
/// ```
2148+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
2149+
/// #
2150+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
2151+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
2152+
/// #
2153+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
2154+
/// let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
2155+
/// # client.create_index("reset_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2156+
/// let mut index = client.index("reset_non_separator_tokens");
2157+
///
2158+
/// let task = index.reset_non_separator_tokens().await.unwrap();
2159+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
2160+
/// # });
2161+
/// ```
2162+
pub async fn reset_non_separator_tokens(&self) -> Result<TaskInfo, Error> {
2163+
self.client
2164+
.http_client
2165+
.request::<(), (), TaskInfo>(
2166+
&format!(
2167+
"{}/indexes/{}/settings/non-separator-tokens",
2168+
self.client.host, self.uid
2169+
),
2170+
Method::Delete { query: () },
2171+
202,
2172+
)
2173+
.await
2174+
}
19272175
}
19282176

19292177
#[cfg(test)]
@@ -2200,6 +2448,26 @@ mod tests {
22002448
assert_eq!(expected, res);
22012449
}
22022450

2451+
#[meilisearch_test]
2452+
async fn test_get_separator_tokens(index: Index) {
2453+
let separator: Vec<&str> = vec![];
2454+
let res = index.get_separator_tokens().await.unwrap();
2455+
2456+
assert_eq!(separator, res);
2457+
}
2458+
2459+
#[meilisearch_test]
2460+
async fn test_set_separator_tokens(client: Client, index: Index) {
2461+
let expected: Vec<String> = vec!["#".to_string(), "@".to_string()];
2462+
2463+
let task_info = index.set_separator_tokens(&expected).await.unwrap();
2464+
client.wait_for_task(task_info, None, None).await.unwrap();
2465+
2466+
let res = index.get_separator_tokens().await.unwrap();
2467+
2468+
assert_eq!(expected, res);
2469+
}
2470+
22032471
#[meilisearch_test]
22042472
async fn test_reset_search_cutoff_ms(index: Index) {
22052473
let expected = None;
@@ -2214,4 +2482,44 @@ mod tests {
22142482

22152483
assert_eq!(expected, default);
22162484
}
2485+
2486+
#[meilisearch_test]
2487+
async fn test_reset_separator_tokens(client: Client, index: Index) {
2488+
let separator: Vec<&str> = vec![];
2489+
let task_info = index.reset_separator_tokens().await.unwrap();
2490+
client.wait_for_task(task_info, None, None).await.unwrap();
2491+
2492+
let res = index.get_dictionary().await.unwrap();
2493+
assert_eq!(separator, res);
2494+
}
2495+
2496+
#[meilisearch_test]
2497+
async fn test_get_non_separator_tokens(index: Index) {
2498+
let separator: Vec<&str> = vec![];
2499+
let res = index.get_non_separator_tokens().await.unwrap();
2500+
2501+
assert_eq!(separator, res);
2502+
}
2503+
2504+
#[meilisearch_test]
2505+
async fn test_set_non_separator_tokens(client: Client, index: Index) {
2506+
let expected: Vec<String> = vec!["#".to_string(), "@".to_string()];
2507+
2508+
let task_info = index.set_non_separator_tokens(&expected).await.unwrap();
2509+
client.wait_for_task(task_info, None, None).await.unwrap();
2510+
2511+
let res = index.get_non_separator_tokens().await.unwrap();
2512+
2513+
assert_eq!(expected, res);
2514+
}
2515+
2516+
#[meilisearch_test]
2517+
async fn test_reset_non_separator_tokens(client: Client, index: Index) {
2518+
let separator: Vec<&str> = vec![];
2519+
let task_info = index.reset_non_separator_tokens().await.unwrap();
2520+
client.wait_for_task(task_info, None, None).await.unwrap();
2521+
2522+
let res = index.get_dictionary().await.unwrap();
2523+
assert_eq!(separator, res);
2524+
}
22172525
}

0 commit comments

Comments
 (0)