Skip to content

Commit 5912be2

Browse files
committed
Added docs and samples for batchStrategy
1 parent dce6c21 commit 5912be2

File tree

3 files changed

+100
-13
lines changed

3 files changed

+100
-13
lines changed

.code-samples.meilisearch.yaml

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -450,15 +450,22 @@ reset_typo_tolerance_1: |-
450450
.await
451451
.unwrap();
452452
get_all_batches_1: |-
453-
let batches: BatchesResults = client
454-
.get_batches()
455-
.await
456-
.unwrap();
453+
let mut query = meilisearch_sdk::batches::BatchesQuery::new(&client);
454+
query.with_limit(20);
455+
let batches: meilisearch_sdk::batches::BatchesResults =
456+
client.get_batches_with(&query).await.unwrap();
457457
get_batch_1: |-
458-
let batch: Batch = client
459-
.get_batch(42)
458+
let uid: u32 = 42;
459+
let batch: meilisearch_sdk::batches::Batch = client
460+
.get_batch(uid)
460461
.await
461462
.unwrap();
463+
get_all_batches_paginating_1: |-
464+
let mut query = meilisearch_sdk::batches::BatchesQuery::new(&client);
465+
query.with_limit(2);
466+
query.with_from(40);
467+
let batches: meilisearch_sdk::batches::BatchesResults =
468+
client.get_batches_with(&query).await.unwrap();
462469
get_stop_words_1: |-
463470
let stop_words: Vec<String> = client
464471
.index("movies")

src/batches.rs

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,29 @@ pub struct Batch {
3030
///
3131
/// Introduced in Meilisearch v1.15.
3232
#[serde(skip_serializing_if = "Option::is_none")]
33-
pub batch_strategy: Option<String>,
33+
pub batch_strategy: Option<BatchStrategy>,
34+
}
35+
36+
/// Reason why the autobatcher stopped batching tasks.
37+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
38+
#[serde(rename_all = "snake_case")]
39+
#[non_exhaustive]
40+
pub enum BatchStrategy {
41+
/// The batch reached its configured size threshold.
42+
SizeLimitReached,
43+
/// The batch reached its configured time window threshold.
44+
TimeLimitReached,
45+
/// Unknown strategy (forward-compatibility).
46+
#[serde(other)]
47+
Unknown,
3448
}
3549

3650
#[derive(Debug, Clone, Deserialize)]
3751
#[serde(rename_all = "camelCase")]
3852
pub struct BatchesResults {
3953
pub results: Vec<Batch>,
40-
#[serde(skip_serializing_if = "Option::is_none")]
41-
pub total: Option<u64>,
42-
#[serde(skip_serializing_if = "Option::is_none")]
43-
pub limit: Option<u32>,
54+
pub total: u32,
55+
pub limit: u32,
4456
#[serde(skip_serializing_if = "Option::is_none")]
4557
pub from: Option<u32>,
4658
#[serde(skip_serializing_if = "Option::is_none")]
@@ -91,6 +103,7 @@ impl<'a, Http: HttpClient> BatchesQuery<'a, Http> {
91103

92104
#[cfg(test)]
93105
mod tests {
106+
use crate::batches::BatchStrategy;
94107
use crate::client::Client;
95108

96109
#[tokio::test]
@@ -130,7 +143,7 @@ mod tests {
130143
assert_eq!(batches.results.len(), 1);
131144
let b = &batches.results[0];
132145
assert_eq!(b.uid, 42);
133-
assert_eq!(b.batch_strategy.as_deref(), Some("time_limit_reached"));
146+
assert_eq!(b.batch_strategy, Some(BatchStrategy::TimeLimitReached));
134147
}
135148

136149
#[tokio::test]
@@ -156,6 +169,31 @@ mod tests {
156169
let client = Client::new(base, None::<String>).unwrap();
157170
let batch = client.get_batch(99).await.expect("get batch failed");
158171
assert_eq!(batch.uid, 99);
159-
assert_eq!(batch.batch_strategy.as_deref(), Some("size_limit_reached"));
172+
assert_eq!(batch.batch_strategy, Some(BatchStrategy::SizeLimitReached));
173+
}
174+
175+
#[tokio::test]
176+
async fn test_query_serialization_for_batches() {
177+
use mockito::Matcher;
178+
let mut s = mockito::Server::new_async().await;
179+
let base = s.url();
180+
181+
let _m = s
182+
.mock("GET", "/batches")
183+
.match_query(Matcher::AllOf(vec![
184+
Matcher::UrlEncoded("limit".into(), "2".into()),
185+
Matcher::UrlEncoded("from".into(), "40".into()),
186+
]))
187+
.with_status(200)
188+
.with_header("content-type", "application/json")
189+
.with_body(r#"{"results":[],"limit":2,"total":0}"#)
190+
.create_async()
191+
.await;
192+
193+
let client = Client::new(base, None::<String>).unwrap();
194+
let mut q = crate::batches::BatchesQuery::new(&client);
195+
let _ = q.with_limit(2).with_from(40);
196+
let res = client.get_batches_with(&q).await.expect("request failed");
197+
assert_eq!(res.limit, 2);
160198
}
161199
}

src/client.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,19 @@ impl<Http: HttpClient> Client<Http> {
11151115
/// List batches using the Batches API.
11161116
///
11171117
/// See: https://www.meilisearch.com/docs/reference/api/batches
1118+
///
1119+
/// # Example
1120+
///
1121+
/// ```
1122+
/// # use meilisearch_sdk::client::Client;
1123+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1124+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1125+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1126+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1127+
/// let batches = client.get_batches().await.unwrap();
1128+
/// # let _ = batches;
1129+
/// # });
1130+
/// ```
11181131
pub async fn get_batches(&self) -> Result<crate::batches::BatchesResults, Error> {
11191132
let res = self
11201133
.http_client
@@ -1128,6 +1141,21 @@ impl<Http: HttpClient> Client<Http> {
11281141
}
11291142

11301143
/// List batches with pagination filters.
1144+
///
1145+
/// # Example
1146+
///
1147+
/// ```
1148+
/// # use meilisearch_sdk::{client::Client, batches::BatchesQuery};
1149+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1150+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1151+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1152+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1153+
/// let mut query = BatchesQuery::new(&client);
1154+
/// query.with_limit(1);
1155+
/// let batches = client.get_batches_with(&query).await.unwrap();
1156+
/// # let _ = batches;
1157+
/// # });
1158+
/// ```
11311159
pub async fn get_batches_with(
11321160
&self,
11331161
query: &crate::batches::BatchesQuery<'_, Http>,
@@ -1144,6 +1172,20 @@ impl<Http: HttpClient> Client<Http> {
11441172
}
11451173

11461174
/// Get a single batch by its uid.
1175+
///
1176+
/// # Example
1177+
///
1178+
/// ```
1179+
/// # use meilisearch_sdk::client::Client;
1180+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1181+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1182+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1183+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1184+
/// let uid: u32 = 42;
1185+
/// let batch = client.get_batch(uid).await.unwrap();
1186+
/// # let _ = batch;
1187+
/// # });
1188+
/// ```
11471189
pub async fn get_batch(&self, uid: u32) -> Result<crate::batches::Batch, Error> {
11481190
let res = self
11491191
.http_client

0 commit comments

Comments
 (0)