Skip to content

Commit 02d1008

Browse files
bidoubiwameili-botalallema
authored
Add tasks filters for v0.30.0 (#375)
* Update README.md * Update README.tpl * Add filters in tasksQuery * Add new error codes * Add builder methods and tests * Fix clippy suggestions * Update src/tasks.rs Co-authored-by: Amélie <[email protected]> * Fix flacky tests * Rename index_uids and task types * Rename uid, status and types filters to plural * Add new error codes * Update error code to plural form * Update code samples Co-authored-by: meili-bot <[email protected]> Co-authored-by: Amélie <[email protected]>
1 parent 376bf5f commit 02d1008

File tree

5 files changed

+204
-38
lines changed

5 files changed

+204
-38
lines changed

.code-samples.meilisearch.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ get_all_tasks_1: |-
110110
.unwrap();
111111
get_all_tasks_filtering_1: |-
112112
let mut query = TasksQuery::new(&client)
113-
.with_index_uid(["movies"])
113+
.with_index_uids(["movies"])
114114
.execute()
115115
.await
116116
.unwrap();
117117
get_all_tasks_filtering_2: |-
118118
let mut query = TasksQuery::new(&client)
119-
.with_status(["succeeded", "failed"])
120-
.with_type(["documentAdditionOrUpdate"])
119+
.with_statuses(["succeeded", "failed"])
120+
.with_types(["documentAdditionOrUpdate"])
121121
.execute()
122122
.await
123123
.unwrap();

src/client.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ impl Client {
755755
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
756756
///
757757
/// let mut query = tasks::TasksQuery::new(&client);
758-
/// query.with_index_uid(["get_tasks_with"]);
758+
/// query.with_index_uids(["get_tasks_with"]);
759759
/// let tasks = client.get_tasks_with(&query).await.unwrap();
760760
/// # });
761761
/// ```
@@ -965,15 +965,15 @@ mod tests {
965965
#[meilisearch_test]
966966
async fn test_get_tasks(client: Client) {
967967
let tasks = client.get_tasks().await.unwrap();
968-
assert!(tasks.results.len() >= 2);
968+
assert!(tasks.limit == 20);
969969
}
970970

971971
#[meilisearch_test]
972972
async fn test_get_tasks_with_params(client: Client) {
973973
let query = TasksQuery::new(&client);
974974
let tasks = client.get_tasks_with(&query).await.unwrap();
975975

976-
assert!(tasks.results.len() >= 2);
976+
assert!(tasks.limit == 20);
977977
}
978978

979979
#[meilisearch_test]

src/errors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ pub enum ErrorCode {
156156
InvalidApiKeyIndexes,
157157
InvalidApiKeyExpiresAt,
158158
ApiKeyNotFound,
159+
InvalidTaskTypesFilter,
160+
InvalidTaskStatusesFilter,
161+
InvalidTaskCanceledByFilter,
162+
InvalidTaskUidsFilter,
163+
InvalidTaskDateFilter,
164+
MissingTaskFilters,
159165

160166
/// That's unexpected. Please open a GitHub issue after ensuring you are
161167
/// using the supported version of the Meilisearch server.

src/indexes.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,8 @@ impl Index {
449449
///
450450
/// # futures::executor::block_on(async move {
451451
/// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
452-
/// let movie_index = client.index("get_documents");
452+
///
453+
/// let movie_index = client.index("get_documents_with");
453454
///
454455
/// # movie_index.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
455456
///
@@ -939,7 +940,7 @@ impl Index {
939940
/// ```
940941
pub async fn get_tasks(&self) -> Result<TasksResults, Error> {
941942
let mut query = TasksQuery::new(&self.client);
942-
query.with_index_uid([self.uid.as_str()]);
943+
query.with_index_uids([self.uid.as_str()]);
943944

944945
self.client.get_tasks_with(&query).await
945946
}
@@ -960,7 +961,7 @@ impl Index {
960961
/// # let index = client.create_index("get_tasks_with", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap().try_make_index(&client).unwrap();
961962
///
962963
/// let mut query = TasksQuery::new(&client);
963-
/// query.with_index_uid(["none_existant"]);
964+
/// query.with_index_uids(["none_existant"]);
964965
/// let tasks = index.get_tasks_with(&query).await.unwrap();
965966
///
966967
/// assert!(tasks.results.len() > 0);
@@ -972,7 +973,7 @@ impl Index {
972973
tasks_query: &TasksQuery<'_>,
973974
) -> Result<TasksResults, Error> {
974975
let mut query = tasks_query.clone();
975-
query.with_index_uid([self.uid.as_str()]);
976+
query.with_index_uids([self.uid.as_str()]);
976977

977978
self.client.get_tasks_with(&query).await
978979
}

src/tasks.rs

Lines changed: 187 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -406,13 +406,52 @@ pub struct TasksQuery<'a> {
406406
pub client: &'a Client,
407407
// Index uids array to only retrieve the tasks of the indexes.
408408
#[serde(skip_serializing_if = "Option::is_none")]
409-
pub index_uid: Option<Vec<&'a str>>,
409+
pub index_uids: Option<Vec<&'a str>>,
410410
// Statuses array to only retrieve the tasks with these statuses.
411411
#[serde(skip_serializing_if = "Option::is_none")]
412-
pub status: Option<Vec<&'a str>>,
412+
pub statuses: Option<Vec<&'a str>>,
413413
// Types array to only retrieve the tasks with these [TaskType].
414-
#[serde(skip_serializing_if = "Option::is_none", rename = "type")]
415-
pub task_type: Option<Vec<&'a str>>,
414+
#[serde(skip_serializing_if = "Option::is_none", rename = "types")]
415+
pub task_types: Option<Vec<&'a str>>,
416+
// Uids of the tasks to retrieve
417+
#[serde(skip_serializing_if = "Option::is_none")]
418+
pub uids: Option<Vec<&'a u32>>,
419+
// Date to retrieve all tasks that were enqueued before it.
420+
#[serde(
421+
skip_serializing_if = "Option::is_none",
422+
serialize_with = "time::serde::rfc3339::option::serialize"
423+
)]
424+
pub before_enqueued_at: Option<OffsetDateTime>,
425+
// Date to retrieve all tasks that were enqueued after it.
426+
#[serde(
427+
skip_serializing_if = "Option::is_none",
428+
serialize_with = "time::serde::rfc3339::option::serialize"
429+
)]
430+
pub after_enqueued_at: Option<OffsetDateTime>,
431+
// Date to retrieve all tasks that were started before it.
432+
#[serde(
433+
skip_serializing_if = "Option::is_none",
434+
serialize_with = "time::serde::rfc3339::option::serialize"
435+
)]
436+
pub before_started_at: Option<OffsetDateTime>,
437+
// Date to retrieve all tasks that were started after it.
438+
#[serde(
439+
skip_serializing_if = "Option::is_none",
440+
serialize_with = "time::serde::rfc3339::option::serialize"
441+
)]
442+
pub after_started_at: Option<OffsetDateTime>,
443+
// Date to retrieve all tasks that were finished before it.
444+
#[serde(
445+
skip_serializing_if = "Option::is_none",
446+
serialize_with = "time::serde::rfc3339::option::serialize"
447+
)]
448+
pub before_finished_at: Option<OffsetDateTime>,
449+
// Date to retrieve all tasks that were finished after it.
450+
#[serde(
451+
skip_serializing_if = "Option::is_none",
452+
serialize_with = "time::serde::rfc3339::option::serialize"
453+
)]
454+
pub after_finished_at: Option<OffsetDateTime>,
416455
// Maximum number of tasks to return
417456
#[serde(skip_serializing_if = "Option::is_none")]
418457
pub limit: Option<u32>,
@@ -426,32 +465,88 @@ impl<'a> TasksQuery<'a> {
426465
pub fn new(client: &'a Client) -> TasksQuery<'a> {
427466
TasksQuery {
428467
client,
429-
index_uid: None,
430-
status: None,
431-
task_type: None,
468+
index_uids: None,
469+
statuses: None,
470+
task_types: None,
432471
limit: None,
433472
from: None,
473+
uids: None,
474+
before_enqueued_at: None,
475+
after_enqueued_at: None,
476+
before_started_at: None,
477+
after_started_at: None,
478+
before_finished_at: None,
479+
after_finished_at: None,
434480
}
435481
}
436-
pub fn with_index_uid<'b>(
482+
pub fn with_index_uids<'b>(
483+
&'b mut self,
484+
index_uids: impl IntoIterator<Item = &'a str>,
485+
) -> &'b mut TasksQuery<'a> {
486+
self.index_uids = Some(index_uids.into_iter().collect());
487+
self
488+
}
489+
pub fn with_statuses<'b>(
490+
&'b mut self,
491+
statuses: impl IntoIterator<Item = &'a str>,
492+
) -> &'b mut TasksQuery<'a> {
493+
self.statuses = Some(statuses.into_iter().collect());
494+
self
495+
}
496+
pub fn with_types<'b>(
497+
&'b mut self,
498+
task_types: impl IntoIterator<Item = &'a str>,
499+
) -> &'b mut TasksQuery<'a> {
500+
self.task_types = Some(task_types.into_iter().collect());
501+
self
502+
}
503+
pub fn with_uids<'b>(
504+
&'b mut self,
505+
uids: impl IntoIterator<Item = &'a u32>,
506+
) -> &'b mut TasksQuery<'a> {
507+
self.uids = Some(uids.into_iter().collect());
508+
self
509+
}
510+
pub fn with_before_enqueued_at<'b>(
437511
&'b mut self,
438-
index_uid: impl IntoIterator<Item = &'a str>,
512+
before_enqueued_at: &'a OffsetDateTime,
439513
) -> &'b mut TasksQuery<'a> {
440-
self.index_uid = Some(index_uid.into_iter().collect());
514+
self.before_enqueued_at = Some(*before_enqueued_at);
441515
self
442516
}
443-
pub fn with_status<'b>(
517+
pub fn with_after_enqueued_at<'b>(
444518
&'b mut self,
445-
status: impl IntoIterator<Item = &'a str>,
519+
after_enqueued_at: &'a OffsetDateTime,
446520
) -> &'b mut TasksQuery<'a> {
447-
self.status = Some(status.into_iter().collect());
521+
self.after_enqueued_at = Some(*after_enqueued_at);
448522
self
449523
}
450-
pub fn with_type<'b>(
524+
pub fn with_before_started_at<'b>(
451525
&'b mut self,
452-
task_type: impl IntoIterator<Item = &'a str>,
526+
before_started_at: &'a OffsetDateTime,
453527
) -> &'b mut TasksQuery<'a> {
454-
self.task_type = Some(task_type.into_iter().collect());
528+
self.before_started_at = Some(*before_started_at);
529+
self
530+
}
531+
pub fn with_after_started_at<'b>(
532+
&'b mut self,
533+
after_started_at: &'a OffsetDateTime,
534+
) -> &'b mut TasksQuery<'a> {
535+
self.after_started_at = Some(*after_started_at);
536+
self
537+
}
538+
pub fn with_before_finished_at<'b>(
539+
&'b mut self,
540+
before_finished_at: &'a OffsetDateTime,
541+
) -> &'b mut TasksQuery<'a> {
542+
self.before_finished_at = Some(*before_finished_at);
543+
self
544+
}
545+
pub fn with_after_finished_at<'b>(
546+
&'b mut self,
547+
after_finished_at: &'a OffsetDateTime,
548+
) -> &'b mut TasksQuery<'a> {
549+
self.after_finished_at = Some(*after_finished_at);
455550
self
456551
}
457552
pub fn with_limit<'b>(&'b mut self, limit: u32) -> &'b mut TasksQuery<'a> {
@@ -640,17 +735,81 @@ mod test {
640735
let mock_server_url = &mockito::server_url();
641736
let client = Client::new(mock_server_url, "masterKey");
642737
let path =
643-
"/tasks?indexUid=movies,test&status=equeued&type=documentDeletion&limit=0&from=1";
738+
"/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion&uids=1&limit=0&from=1";
644739

645740
let mock_res = mock("GET", path).with_status(200).create();
646741

647742
let mut query = TasksQuery::new(&client);
648743
query
649-
.with_index_uid(["movies", "test"])
650-
.with_status(["equeued"])
651-
.with_type(["documentDeletion"])
744+
.with_index_uids(["movies", "test"])
745+
.with_statuses(["equeued"])
746+
.with_types(["documentDeletion"])
652747
.with_from(1)
653-
.with_limit(0);
748+
.with_limit(0)
749+
.with_uids([&1]);
750+
751+
let _ = client.get_tasks_with(&query).await;
752+
753+
mock_res.assert();
754+
Ok(())
755+
}
756+
757+
#[meilisearch_test]
758+
async fn test_get_tasks_with_date_params() -> Result<(), Error> {
759+
let mock_server_url = &mockito::server_url();
760+
let client = Client::new(mock_server_url, "masterKey");
761+
let path = "/tasks?\
762+
beforeEnqueuedAt=2022-02-03T13%3A02%3A38.369634Z\
763+
&afterEnqueuedAt=2023-02-03T13%3A02%3A38.369634Z\
764+
&beforeStartedAt=2024-02-03T13%3A02%3A38.369634Z\
765+
&afterStartedAt=2025-02-03T13%3A02%3A38.369634Z\
766+
&beforeFinishedAt=2026-02-03T13%3A02%3A38.369634Z\
767+
&afterFinishedAt=2027-02-03T13%3A02%3A38.369634Z";
768+
769+
let mock_res = mock("GET", path).with_status(200).create();
770+
771+
let before_enqueued_at = OffsetDateTime::parse(
772+
"2022-02-03T13:02:38.369634Z",
773+
&::time::format_description::well_known::Rfc3339,
774+
)
775+
.unwrap();
776+
let after_enqueued_at = OffsetDateTime::parse(
777+
"2023-02-03T13:02:38.369634Z",
778+
&::time::format_description::well_known::Rfc3339,
779+
)
780+
.unwrap();
781+
let before_started_at = OffsetDateTime::parse(
782+
"2024-02-03T13:02:38.369634Z",
783+
&::time::format_description::well_known::Rfc3339,
784+
)
785+
.unwrap();
786+
787+
let after_started_at = OffsetDateTime::parse(
788+
"2025-02-03T13:02:38.369634Z",
789+
&::time::format_description::well_known::Rfc3339,
790+
)
791+
.unwrap();
792+
793+
let before_finished_at = OffsetDateTime::parse(
794+
"2026-02-03T13:02:38.369634Z",
795+
&::time::format_description::well_known::Rfc3339,
796+
)
797+
.unwrap();
798+
799+
let after_finished_at = OffsetDateTime::parse(
800+
"2027-02-03T13:02:38.369634Z",
801+
&::time::format_description::well_known::Rfc3339,
802+
)
803+
.unwrap();
804+
805+
let mut query = TasksQuery::new(&client);
806+
query
807+
.with_before_enqueued_at(&before_enqueued_at)
808+
.with_after_enqueued_at(&after_enqueued_at)
809+
.with_before_started_at(&before_started_at)
810+
.with_after_started_at(&after_started_at)
811+
.with_before_finished_at(&before_finished_at)
812+
.with_after_finished_at(&after_finished_at);
654813

655814
let _ = client.get_tasks_with(&query).await;
656815

@@ -662,15 +821,15 @@ mod test {
662821
async fn test_get_tasks_on_struct_with_params() -> Result<(), Error> {
663822
let mock_server_url = &mockito::server_url();
664823
let client = Client::new(mock_server_url, "masterKey");
665-
let path = "/tasks?indexUid=movies,test&status=equeued&type=documentDeletion";
824+
let path = "/tasks?indexUids=movies,test&statuses=equeued&types=documentDeletion";
666825

667826
let mock_res = mock("GET", path).with_status(200).create();
668827

669828
let mut query = TasksQuery::new(&client);
670829
let _ = query
671-
.with_index_uid(["movies", "test"])
672-
.with_status(["equeued"])
673-
.with_type(["documentDeletion"])
830+
.with_index_uids(["movies", "test"])
831+
.with_statuses(["equeued"])
832+
.with_types(["documentDeletion"])
674833
.execute()
675834
.await;
676835

@@ -680,9 +839,9 @@ mod test {
680839
}
681840

682841
#[meilisearch_test]
683-
async fn test_get_tasks_with_none_existant_index_uid(client: Client) -> Result<(), Error> {
842+
async fn test_get_tasks_with_none_existant_index_uids(client: Client) -> Result<(), Error> {
684843
let mut query = TasksQuery::new(&client);
685-
query.with_index_uid(["no_name"]);
844+
query.with_index_uids(["no_name"]);
686845
let tasks = client.get_tasks_with(&query).await.unwrap();
687846

688847
assert_eq!(tasks.results.len(), 0);
@@ -692,7 +851,7 @@ mod test {
692851
#[meilisearch_test]
693852
async fn test_get_tasks_with_execute(client: Client) -> Result<(), Error> {
694853
let tasks = TasksQuery::new(&client)
695-
.with_index_uid(["no_name"])
854+
.with_index_uids(["no_name"])
696855
.execute()
697856
.await
698857
.unwrap();

0 commit comments

Comments
 (0)