Skip to content

Commit

Permalink
Merge #588
Browse files Browse the repository at this point in the history
588: Support `searchCutoffMs` setting r=irevoire a=JWSong

# Pull Request

## Related issue
Fixes #581

## What does this PR do?
- support `searchCutoffMs` setting

## 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: JWSong <[email protected]>
  • Loading branch information
meili-bors[bot] and JWSong authored Jul 1, 2024
2 parents 9e57f3e + 41a3532 commit 9443600
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 1 deletion.
21 changes: 20 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,8 @@ update_settings_1: |-
"release_date"
])
.with_synonyms(synonyms)
.with_typo_tolerance(typo_tolerance);
.with_typo_tolerance(typo_tolerance)
.with_search_cutoff(5);
let task: TaskInfo = client
.index("movies")
Expand Down Expand Up @@ -1626,6 +1627,24 @@ reset_proximity_precision_settings_1: |-
.reset_proximity_precision()
.await
.unwrap();
get_search_cutoff_1: |-
let search_cutoff_ms: String = client
.index("books")
.get_search_cutoff_ms()
.await
.unwrap();
update_search_cutoff_1: |-
let task: TaskInfo = client
.index("books")
.set_search_cutoff_ms(Some(0))
.await
.unwrap();
reset_search_cutoff_1: |-
let task: TaskInfo = client
.index("books")
.reset_search_cutoff_ms()
.await
.unwrap();
create_snapshot_1: |-
client
.create_snapshot()
Expand Down
148 changes: 148 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ pub struct Settings {
/// Proximity precision settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub proximity_precision: Option<String>,
/// SearchCutoffMs settings.
#[serde(skip_serializing_if = "Option::is_none")]
pub search_cutoff_ms: Option<u64>,
}

#[allow(missing_docs)]
Expand Down Expand Up @@ -288,6 +291,13 @@ impl Settings {
..self
}
}

pub fn with_search_cutoff(self, search_cutoff_ms: u64) -> Settings {
Settings {
search_cutoff_ms: Some(search_cutoff_ms),
..self
}
}
}

impl<Http: HttpClient> Index<Http> {
Expand Down Expand Up @@ -757,6 +767,39 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// Get [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("get_search_cutoff_ms", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("get_search_cutoff_ms");
///
/// let task = index.get_search_cutoff_ms().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn get_search_cutoff_ms(&self) -> Result<Option<u64>, Error> {
self.client
.http_client
.request::<(), (), Option<u64>>(
&format!(
"{}/indexes/{}/settings/search-cutoff-ms",
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 @@ -1350,6 +1393,42 @@ impl<Http: HttpClient> Index<Http> {
.await
}

/// Update [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("update_search_cutoff_ms", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("update_search_cutoff_ms");
///
/// let task = index.set_search_cutoff_ms(Some(150)).await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn set_search_cutoff_ms(&self, ms: Option<u64>) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), Option<u64>, TaskInfo>(
&format!(
"{}/indexes/{}/settings/search-cutoff-ms",
self.client.host, self.uid
),
Method::Put {
body: ms,
query: (),
},
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).
Expand Down Expand Up @@ -1812,6 +1891,39 @@ 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_search_cutoff_ms", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// let mut index = client.index("reset_search_cutoff_ms");
///
/// let task = index.reset_search_cutoff_ms().await.unwrap();
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn reset_search_cutoff_ms(&self) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), (), TaskInfo>(
&format!(
"{}/indexes/{}/settings/search-cutoff-ms",
self.client.host, self.uid
),
Method::Delete { query: () },
202,
)
.await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -2066,4 +2178,40 @@ mod tests {

assert_eq!(expected, default);
}

#[meilisearch_test]
async fn test_get_search_cutoff_ms(index: Index) {
let expected = None;

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

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_set_search_cutoff_ms(client: Client, index: Index) {
let expected = Some(150);

let task_info = index.set_search_cutoff_ms(Some(150)).await.unwrap();
client.wait_for_task(task_info, None, None).await.unwrap();

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

assert_eq!(expected, res);
}

#[meilisearch_test]
async fn test_reset_search_cutoff_ms(index: Index) {
let expected = None;

let task = index.set_search_cutoff_ms(Some(150)).await.unwrap();
index.wait_for_task(task, None, None).await.unwrap();

let reset_task = index.reset_search_cutoff_ms().await.unwrap();
index.wait_for_task(reset_task, None, None).await.unwrap();

let default = index.get_search_cutoff_ms().await.unwrap();

assert_eq!(expected, default);
}
}

0 comments on commit 9443600

Please sign in to comment.