From 96cffeab1ec349ffb63d6f2adc8b8af936db9929 Mon Sep 17 00:00:00 2001 From: Irevoire Date: Wed, 17 Mar 2021 13:54:17 +0100 Subject: [PATCH 1/3] update all the response format to be ISO with meilisearch, see #64 --- meilisearch-http/src/routes/document.rs | 24 ++++++++++---- meilisearch-http/tests/common/index.rs | 2 +- .../tests/documents/add_documents.rs | 31 ++++++++++--------- .../tests/documents/delete_documents.rs | 12 +++---- .../tests/documents/get_documents.rs | 2 +- 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index 8e337d275..7b2bb4628 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -84,7 +84,9 @@ async fn delete_document( .delete_documents(path.index_uid.clone(), vec![path.document_id.clone()]) .await { - Ok(result) => Ok(HttpResponse::Ok().json(result)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -119,7 +121,7 @@ async fn get_all_documents( ) .await { - Ok(docs) => Ok(HttpResponse::Ok().json(docs)), + Ok(documents) => Ok(HttpResponse::Ok().json(documents)), Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -133,6 +135,7 @@ struct UpdateDocumentsQuery { } /// Route used when the payload type is "application/json" +/// Used to add or replace documents #[post("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn add_documents( data: web::Data, @@ -151,7 +154,9 @@ async fn add_documents( .await; match addition_result { - Ok(update) => Ok(HttpResponse::Ok().json(update)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -200,7 +205,9 @@ async fn update_documents( .await; match addition_result { - Ok(update) => Ok(HttpResponse::Ok().json(update)), + Ok(update) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -226,20 +233,25 @@ async fn delete_documents( .collect(); match data.delete_documents(path.index_uid.clone(), ids).await { - Ok(result) => Ok(HttpResponse::Ok().json(result)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } } } +/// delete all documents #[delete("/indexes/{index_uid}/documents", wrap = "Authentication::Private")] async fn clear_all_documents( data: web::Data, path: web::Path, ) -> Result { match data.clear_documents(path.index_uid.clone()).await { - Ok(update) => Ok(HttpResponse::Ok().json(update)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } diff --git a/meilisearch-http/tests/common/index.rs b/meilisearch-http/tests/common/index.rs index 8fda99ef9..b08381ee7 100644 --- a/meilisearch-http/tests/common/index.rs +++ b/meilisearch-http/tests/common/index.rs @@ -23,7 +23,7 @@ impl Index<'_> { .service .post_str(url, include_str!("../assets/test_set.json")) .await; - assert_eq!(code, 200); + assert_eq!(code, 202); let update_id = response["updateId"].as_i64().unwrap(); self.wait_update_id(update_id as u64).await; update_id as u64 diff --git a/meilisearch-http/tests/documents/add_documents.rs b/meilisearch-http/tests/documents/add_documents.rs index dd10541b5..87e5cecb7 100644 --- a/meilisearch-http/tests/documents/add_documents.rs +++ b/meilisearch-http/tests/documents/add_documents.rs @@ -16,13 +16,16 @@ async fn add_documents_no_index_creation() { ]); let (response, code) = index.add_documents(documents, None).await; - assert_eq!(code, 200); - assert_eq!(response["status"], "pending"); + assert_eq!(code, 202); assert_eq!(response["updateId"], 0); - assert_eq!(response["meta"]["type"], "DocumentsAddition"); - assert_eq!(response["meta"]["format"], "Json"); - assert_eq!(response["meta"]["primaryKey"], Value::Null); - assert!(response.get("enqueuedAt").is_some()); + /* + * currently we don’t check these field to stay ISO with meilisearch + * assert_eq!(response["status"], "pending"); + * assert_eq!(response["meta"]["type"], "DocumentsAddition"); + * assert_eq!(response["meta"]["format"], "Json"); + * assert_eq!(response["meta"]["primaryKey"], Value::Null); + * assert!(response.get("enqueuedAt").is_some()); + */ index.wait_update_id(0).await; @@ -75,7 +78,7 @@ async fn document_addition_with_primary_key() { } ]); let (_response, code) = index.add_documents(documents, Some("primary")).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; @@ -102,7 +105,7 @@ async fn document_update_with_primary_key() { } ]); let (_response, code) = index.update_documents(documents, Some("primary")).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; @@ -131,7 +134,7 @@ async fn add_documents_with_primary_key_and_primary_key_already_exists() { ]); let (_response, code) = index.add_documents(documents, Some("id")).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; @@ -160,7 +163,7 @@ async fn update_documents_with_primary_key_and_primary_key_already_exists() { ]); let (_response, code) = index.update_documents(documents, Some("id")).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; let (response, code) = index.get_update(0).await; @@ -187,7 +190,7 @@ async fn replace_document() { ]); let (_response, code) = index.add_documents(documents, None).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; @@ -199,7 +202,7 @@ async fn replace_document() { ]); let (_response, code) = index.add_documents(documents, None).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(1).await; @@ -246,7 +249,7 @@ async fn update_document() { ]); let (_response, code) = index.add_documents(documents, None).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; @@ -258,7 +261,7 @@ async fn update_document() { ]); let (_response, code) = index.update_documents(documents, None).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(1).await; diff --git a/meilisearch-http/tests/documents/delete_documents.rs b/meilisearch-http/tests/documents/delete_documents.rs index e7a01acd4..b69b4c11f 100644 --- a/meilisearch-http/tests/documents/delete_documents.rs +++ b/meilisearch-http/tests/documents/delete_documents.rs @@ -15,7 +15,7 @@ async fn delete_one_unexisting_document() { let index = server.index("test"); index.create(None).await; let (_response, code) = index.delete_document(0).await; - assert_eq!(code, 200); + assert_eq!(code, 202); let update = index.wait_update_id(0).await; assert_eq!(update["status"], "processed"); } @@ -29,7 +29,7 @@ async fn delete_one_document() { .await; index.wait_update_id(0).await; let (_response, code) = server.index("test").delete_document(0).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(1).await; let (_response, code) = index.get_document(0, None).await; @@ -55,7 +55,7 @@ async fn clear_all_documents() { .await; index.wait_update_id(0).await; let (_response, code) = index.clear_all_documents().await; - assert_eq!(code, 200); + assert_eq!(code, 202); let _update = index.wait_update_id(1).await; let (response, code) = index @@ -72,7 +72,7 @@ async fn clear_all_documents_empty_index() { index.create(None).await; let (_response, code) = index.clear_all_documents().await; - assert_eq!(code, 200); + assert_eq!(code, 202); let _update = index.wait_update_id(0).await; let (response, code) = index @@ -96,7 +96,7 @@ async fn delete_batch() { index.add_documents(json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }, { "id": 3, "content": "foobar" }]), Some("id")).await; index.wait_update_id(0).await; let (_response, code) = index.delete_batch(vec![1, 0]).await; - assert_eq!(code, 200); + assert_eq!(code, 202); let _update = index.wait_update_id(1).await; let (response, code) = index @@ -114,7 +114,7 @@ async fn delete_no_document_batch() { index.add_documents(json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }, { "id": 3, "content": "foobar" }]), Some("id")).await; index.wait_update_id(0).await; let (_response, code) = index.delete_batch(vec![]).await; - assert_eq!(code, 200); + assert_eq!(code, 202); let _update = index.wait_update_id(1).await; let (response, code) = index diff --git a/meilisearch-http/tests/documents/get_documents.rs b/meilisearch-http/tests/documents/get_documents.rs index d54234860..73e29576c 100644 --- a/meilisearch-http/tests/documents/get_documents.rs +++ b/meilisearch-http/tests/documents/get_documents.rs @@ -33,7 +33,7 @@ async fn get_document() { } ]); let (_, code) = index.add_documents(documents, None).await; - assert_eq!(code, 200); + assert_eq!(code, 202); index.wait_update_id(0).await; let (response, code) = index.get_document(0, None).await; assert_eq!(code, 200); From 79c63049d7fb67ebf8a85da97946232aabe4e25c Mon Sep 17 00:00:00 2001 From: tamo Date: Mon, 29 Mar 2021 20:19:37 +0200 Subject: [PATCH 2/3] update the settings routes --- meilisearch-http/src/index/updates.rs | 3 +++ meilisearch-http/src/routes/settings/mod.rs | 8 ++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/meilisearch-http/src/index/updates.rs b/meilisearch-http/src/index/updates.rs index a519eaa82..79558dd92 100644 --- a/meilisearch-http/src/index/updates.rs +++ b/meilisearch-http/src/index/updates.rs @@ -43,6 +43,9 @@ pub struct Settings { skip_serializing_if = "Option::is_none" )] pub ranking_rules: Option>>, + + // TODO we are missing the stopWords, synonyms and distinctAttribute for the GET settings + // request } impl Settings { diff --git a/meilisearch-http/src/routes/settings/mod.rs b/meilisearch-http/src/routes/settings/mod.rs index ee9b3e325..b5989bf87 100644 --- a/meilisearch-http/src/routes/settings/mod.rs +++ b/meilisearch-http/src/routes/settings/mod.rs @@ -131,7 +131,9 @@ async fn update_all( .update_settings(index_uid.into_inner(), body.into_inner(), true) .await { - Ok(update_result) => Ok(HttpResponse::Accepted().json(update_result)), + Ok(update_result) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_result.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -161,7 +163,9 @@ async fn delete_all( .update_settings(index_uid.into_inner(), settings, false) .await { - Ok(update_result) => Ok(HttpResponse::Accepted().json(update_result)), + Ok(update_result) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_result.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } From 73973e2b9ef2f93aabc9dfc22fa787b187bfd058 Mon Sep 17 00:00:00 2001 From: tamo Date: Thu, 1 Apr 2021 15:50:45 +0200 Subject: [PATCH 3/3] fix more settings routes --- meilisearch-http/src/routes/settings/mod.rs | 8 ++++++-- meilisearch-http/tests/settings/get_settings.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/meilisearch-http/src/routes/settings/mod.rs b/meilisearch-http/src/routes/settings/mod.rs index b5989bf87..8c6e04b84 100644 --- a/meilisearch-http/src/routes/settings/mod.rs +++ b/meilisearch-http/src/routes/settings/mod.rs @@ -27,7 +27,9 @@ macro_rules! make_setting_route { ..Default::default() }; match data.update_settings(index_uid.into_inner(), settings, false).await { - Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } @@ -46,7 +48,9 @@ macro_rules! make_setting_route { }; match data.update_settings(index_uid.into_inner(), settings, true).await { - Ok(update_status) => Ok(HttpResponse::Ok().json(update_status)), + Ok(update_status) => { + Ok(HttpResponse::Accepted().json(serde_json::json!({ "updateId": update_status.id() }))) + } Err(e) => { Ok(HttpResponse::BadRequest().json(serde_json::json!({ "error": e.to_string() }))) } diff --git a/meilisearch-http/tests/settings/get_settings.rs b/meilisearch-http/tests/settings/get_settings.rs index b1030aea9..d234cbb2b 100644 --- a/meilisearch-http/tests/settings/get_settings.rs +++ b/meilisearch-http/tests/settings/get_settings.rs @@ -142,7 +142,7 @@ macro_rules! test_setting_routes { .map(|c| if c == '_' { '-' } else { c }) .collect::()); let (response, code) = server.service.post(url, serde_json::Value::Null).await; - assert_eq!(code, 200, "{}", response); + assert_eq!(code, 202, "{}", response); let (response, code) = server.index("test").get().await; assert_eq!(code, 200, "{}", response); }