From 8800b348f095891ef447a03df361982ea46ad199 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Mon, 30 May 2022 17:12:53 +0200 Subject: [PATCH] Implement the StarOr on all the tasks filters --- meilisearch-http/src/routes/tasks.rs | 49 +++++++++++++++------------- meilisearch-http/tests/tasks/mod.rs | 27 ++++++++++++++- 2 files changed, 52 insertions(+), 24 deletions(-) diff --git a/meilisearch-http/src/routes/tasks.rs b/meilisearch-http/src/routes/tasks.rs index a5dcc6513..6bb9d1e91 100644 --- a/meilisearch-http/src/routes/tasks.rs +++ b/meilisearch-http/src/routes/tasks.rs @@ -22,8 +22,8 @@ pub fn configure(cfg: &mut web::ServiceConfig) { #[serde(rename_all = "camelCase", deny_unknown_fields)] pub struct TaskFilterQuery { #[serde(rename = "type")] - type_: Option>, - status: Option>, + type_: Option>>, + status: Option>>, index_uid: Option>>, } @@ -47,6 +47,20 @@ impl FromStr for StarOr { } } +/// Extracts the raw values from the `StarOr` types and +/// return None if a `StarOr::Star` is encountered. +fn fold_star_or(content: Vec>) -> Option> { + content + .into_iter() + .fold(Some(Vec::new()), |acc, val| match (acc, val) { + (None, _) | (_, StarOr::Star) => None, + (Some(mut acc), StarOr::Other(uid)) => { + acc.push(uid); + Some(acc) + } + }) +} + #[rustfmt::skip] fn task_type_matches_content(type_: &TaskType, content: &TaskContent) -> bool { matches!((type_, content), @@ -91,25 +105,14 @@ async fn get_tasks( let search_rules = &meilisearch.filters().search_rules; - // We first tranform a potential indexUid=* into a - // "not specified indexUid filter". - let index_uid = - match index_uid { - Some(indexes) => indexes - .into_inner() - .into_iter() - .fold(Some(Vec::new()), |acc, val| match (acc, val) { - (None, _) | (_, StarOr::Star) => None, - (Some(mut acc), StarOr::Other(uid)) => { - acc.push(uid); - Some(acc) - } - }), - None => None, - }; + // We first tranform a potential indexUid=* into a "not specified indexUid filter" + // for every one of the filters: type, status, and indexUid. + let type_ = type_.map(CS::into_inner).and_then(fold_star_or); + let status = status.map(CS::into_inner).and_then(fold_star_or); + let index_uid = index_uid.map(CS::into_inner).and_then(fold_star_or); - // Then we filter on potential indexes and make sure - // that the search filter restrictions are also applied. + // Then we filter on potential indexes and make sure that the search filter + // restrictions are also applied. let indexes_filters = match index_uid { Some(indexes) => { let mut filters = TaskFilter::default(); @@ -135,7 +138,7 @@ async fn get_tasks( // Then we complete the task filter with other potential status and types filters. let filters = match (type_, status) { - (Some(CS(types)), Some(CS(statuses))) => { + (Some(types), Some(statuses)) => { let mut filters = indexes_filters.unwrap_or_default(); filters.filter_fn(move |task| { let matches_type = types @@ -148,7 +151,7 @@ async fn get_tasks( }); Some(filters) } - (Some(CS(types)), None) => { + (Some(types), None) => { let mut filters = indexes_filters.unwrap_or_default(); filters.filter_fn(move |task| { types @@ -157,7 +160,7 @@ async fn get_tasks( }); Some(filters) } - (None, Some(CS(statuses))) => { + (None, Some(statuses)) => { let mut filters = indexes_filters.unwrap_or_default(); filters.filter_fn(move |task| { statuses diff --git a/meilisearch-http/tests/tasks/mod.rs b/meilisearch-http/tests/tasks/mod.rs index b14491fd2..1ba7a4936 100644 --- a/meilisearch-http/tests/tasks/mod.rs +++ b/meilisearch-http/tests/tasks/mod.rs @@ -60,7 +60,7 @@ async fn list_tasks() { } #[actix_rt::test] -async fn list_tasks_with_index_filter() { +async fn list_tasks_with_star_filters() { let server = Server::new().await; let index = server.index("test"); index.create(None).await; @@ -82,6 +82,31 @@ async fn list_tasks_with_index_filter() { let (response, code) = index.service.get("/tasks?indexUid=*,pasteque").await; assert_eq!(code, 200); assert_eq!(response["results"].as_array().unwrap().len(), 2); + + let (response, code) = index.service.get("/tasks?type=*").await; + assert_eq!(code, 200); + assert_eq!(response["results"].as_array().unwrap().len(), 2); + + let (response, code) = index + .service + .get("/tasks?type=*,documentAdditionOrUpdate&status=*") + .await; + assert_eq!(code, 200, "{:?}", response); + assert_eq!(response["results"].as_array().unwrap().len(), 2); + + let (response, code) = index + .service + .get("/tasks?type=*,documentAdditionOrUpdate&status=*,failed&indexUid=test") + .await; + assert_eq!(code, 200, "{:?}", response); + assert_eq!(response["results"].as_array().unwrap().len(), 2); + + let (response, code) = index + .service + .get("/tasks?type=*,documentAdditionOrUpdate&status=*,failed&indexUid=test,*") + .await; + assert_eq!(code, 200, "{:?}", response); + assert_eq!(response["results"].as_array().unwrap().len(), 2); } #[actix_rt::test]