Skip to content

Commit

Permalink
Merge #595
Browse files Browse the repository at this point in the history
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 <lferraraccio@outlook.com>
Co-authored-by: NoodleSamaChan <150461421+NoodleSamaChan@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 2, 2024
2 parents cfbcebe + 968dfab commit fafebbf
Show file tree
Hide file tree
Showing 2 changed files with 344 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,42 @@ reset_faceting_settings_1: |-
.reset_faceting()
.await
.unwrap();
get_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.get_dictionary()
.await
.unwrap();
update_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.set_dictionary(['|', '&hellip;'])
.await
.unwrap();
reset_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.reset_dictionary()
.await
.unwrap();
get_non_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.get_dictionary()
.await
.unwrap();
update_non_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.set_dictionary(['@', '#'])
.await
.unwrap();
reset_non_separator_tokens_1: |-
let task: TaskInfo = client
.index('articles')
.reset_dictionary()
.await
.unwrap();
get_dictionary_1: |-
let task: TaskInfo = client
.index('books')
Expand Down
308 changes: 308 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ pub struct Settings {
/// SearchCutoffMs settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub search_cutoff_ms: Option<u64>,
/// Configure strings as custom separator tokens indicating where a word ends and begins.
#[serde(skip_serializing_if = "Option::is_none")]
pub separator_tokens: Option<Vec<String>>,
/// 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<Vec<String>>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -298,6 +304,38 @@ impl Settings {
..self
}
}

#[must_use]
pub fn with_separation_tokens(
self,
separator_tokens: impl IntoIterator<Item = impl AsRef<str>>,
) -> Settings {
Settings {
separator_tokens: Some(
separator_tokens
.into_iter()
.map(|v| v.as_ref().to_string())
.collect(),
),
..self
}
}

#[must_use]
pub fn with_non_separation_tokens(
self,
non_separator_tokens: impl IntoIterator<Item = impl AsRef<str>>,
) -> Settings {
Settings {
non_separator_tokens: Some(
non_separator_tokens
.into_iter()
.map(|v| v.as_ref().to_string())
.collect(),
),
..self
}
}
}

impl<Http: HttpClient> Index<Http> {
Expand Down Expand Up @@ -800,6 +838,68 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// Get [separator token](https://www.meilisearch.com/docs/reference/api/settings#separator-tokens) of the [Index].
///
/// ```
/// # 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");
/// #
/// # 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_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let index = client.index("get_separator_tokens");
///
/// let separator_tokens = index.get_separator_tokens().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_separator_tokens(&self) -> Result<Vec<String>, Error> {
self.client
.http_client
.request::<(), (), Vec<String>>(
&format!(
"{}/indexes/{}/settings/separator-tokens",
self.client.host, self.uid
),
Method::Get { query: () },
200,
)
.await
}

/// Get [non separator token](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) of the [Index].
///
/// ```
/// # 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");
/// #
/// # 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_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let index = client.index("get_non_separator_tokens");
///
/// let non_separator_tokens = index.get_non_separator_tokens().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_non_separator_tokens(&self) -> Result<Vec<String>, Error> {
self.client
.http_client
.request::<(), (), Vec<String>>(
&format!(
"{}/indexes/{}/settings/non-separator-tokens",
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.
Expand Down Expand Up @@ -1354,6 +1454,88 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// Update [separator tokens](https://www.meilisearch.com/docs/reference/api/settings#separator-tokens) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{TypoToleranceSettings, MinWordSizeForTypos}};
/// #
/// # 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_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("set_separator_tokens");
///
/// let separator_token: Vec<String> = vec!["@".to_string(), "#".to_string()];
///
/// let task = index.set_separator_tokens(&separator_token).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_separator_tokens(
&self,
separator_token: &Vec<String>,
) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), &Vec<String>, TaskInfo>(
&format!(
"{}/indexes/{}/settings/separator-tokens",
self.client.host, self.uid
),
Method::Put {
query: (),
body: separator_token,
},
202,
)
.await
}

/// Update [non separator tokens](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) settings of the [Index].
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings, settings::{TypoToleranceSettings, MinWordSizeForTypos}};
/// #
/// # 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_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("set_non_separator_tokens");
///
/// let non_separator_token: Vec<String> = vec!["@".to_string(), "#".to_string()];
///
/// let task = index.set_non_separator_tokens(&non_separator_token).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_non_separator_tokens(
&self,
non_separator_token: &Vec<String>,
) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), &Vec<String>, TaskInfo>(
&format!(
"{}/indexes/{}/settings/non-separator-tokens",
self.client.host, self.uid
),
Method::Put {
query: (),
body: non_separator_token,
},
202,
)
.await
}

/// Update [proximity-precision](https://www.meilisearch.com/docs/learn/configuration/proximity-precision) settings of the [Index].
///
/// # Example
Expand Down Expand Up @@ -1924,6 +2106,72 @@ impl<Http: HttpClient> Index<Http> {
)
.await
}

/// Reset [search cutoff](https://www.meilisearch.com/docs/reference/api/settings#search-cutoff) 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_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_separator_tokens");
///
/// let task = index.reset_separator_tokens().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_separator_tokens(&self) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), (), TaskInfo>(
&format!(
"{}/indexes/{}/settings/separator-tokens",
self.client.host, self.uid
),
Method::Delete { query: () },
202,
)
.await
}

/// Reset [non separator tokens](https://www.meilisearch.com/docs/reference/api/settings#non-separator-tokens) 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_non_separator_tokens", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_non_separator_tokens");
///
/// let task = index.reset_non_separator_tokens().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_non_separator_tokens(&self) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), (), TaskInfo>(
&format!(
"{}/indexes/{}/settings/non-separator-tokens",
self.client.host, self.uid
),
Method::Delete { query: () },
202,
)
.await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -2200,6 +2448,26 @@ mod tests {
assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_get_separator_tokens(index: Index) {
let separator: Vec<&str> = vec![];
let res = index.get_separator_tokens().await.unwrap();

assert_eq!(separator, res);
}

#[meilisearch_test]
async fn test_set_separator_tokens(client: Client, index: Index) {
let expected: Vec<String> = vec!["#".to_string(), "@".to_string()];

let task_info = index.set_separator_tokens(&expected).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_separator_tokens().await.unwrap();

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_reset_search_cutoff_ms(index: Index) {
let expected = None;
Expand All @@ -2214,4 +2482,44 @@ mod tests {

assert_eq!(expected, default);
}

#[meilisearch_test]
async fn test_reset_separator_tokens(client: Client, index: Index) {
let separator: Vec<&str> = vec![];
let task_info = index.reset_separator_tokens().await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_dictionary().await.unwrap();
assert_eq!(separator, res);
}

#[meilisearch_test]
async fn test_get_non_separator_tokens(index: Index) {
let separator: Vec<&str> = vec![];
let res = index.get_non_separator_tokens().await.unwrap();

assert_eq!(separator, res);
}

#[meilisearch_test]
async fn test_set_non_separator_tokens(client: Client, index: Index) {
let expected: Vec<String> = vec!["#".to_string(), "@".to_string()];

let task_info = index.set_non_separator_tokens(&expected).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_non_separator_tokens().await.unwrap();

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_reset_non_separator_tokens(client: Client, index: Index) {
let separator: Vec<&str> = vec![];
let task_info = index.reset_non_separator_tokens().await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

let res = index.get_dictionary().await.unwrap();
assert_eq!(separator, res);
}
}

0 comments on commit fafebbf

Please sign in to comment.