From f86fba96df82fd8b31dbf1b55a3f7bbaa30aeca4 Mon Sep 17 00:00:00 2001 From: Nguyen Trung Khanh Date: Tue, 19 Mar 2024 17:40:35 +0700 Subject: [PATCH 1/3] fix(indexes): allow None `primary_key` in `add_or_update` function --- src/indexes.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/indexes.rs b/src/indexes.rs index c9bca878..86a55bed 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -899,14 +899,12 @@ impl Index { pub async fn add_or_update( &self, documents: &[T], - primary_key: Option>, + primary_key: Option<&str>, ) -> Result { let url = if let Some(primary_key) = primary_key { format!( "{}/indexes/{}/documents?primaryKey={}", - self.client.host, - self.uid, - primary_key.as_ref() + self.client.host, self.uid, primary_key ) } else { format!("{}/indexes/{}/documents", self.client.host, self.uid) From 039d18730594ff2d6447523c658b7926fec5fa32 Mon Sep 17 00:00:00 2001 From: Khanh Nguyen Date: Sun, 7 Apr 2024 16:33:24 +0700 Subject: [PATCH 2/3] add a test case --- src/indexes.rs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/indexes.rs b/src/indexes.rs index 86a55bed..aaf80154 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -2043,6 +2043,40 @@ mod tests { assert_eq!(res.offset, 2); } + #[meilisearch_test] + async fn test_update_document_json(client: Client, index: Index) -> Result<(), Error> { + let old_json = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes(); + let updated_json = r#"{ "id": 1, "second_body": "second_doggo" }{ "id": 2, "second_body": "second_catto" }"#.as_bytes(); + + let task = index + .add_documents(old_json, Some("id")) + .await? + .wait_for_completion(&client, None, None) + .await?; + let _ = index.get_task(task).await?; + + let task = index + .add_or_update(updated_json, None) + .await? + .wait_for_completion(&client, None, None) + .await?; + + let status = index.get_task(task).await?; + let elements = index.get_documents::().await.unwrap(); + + assert!(matches!(status, Task::Succeeded { .. })); + assert_eq!(elements.results.len(), 2); + + let expected_result = vec![ + json!( {"body": "doggo", "id": 1, "second_body": "second_doggo"}), + json!( {"body": "catto", "id": 2, "second_body": "second_catto"}), + ]; + + assert_eq!(elements.results, expected_result); + + Ok(()) + } + #[meilisearch_test] async fn test_add_documents_ndjson(client: Client, index: Index) -> Result<(), Error> { let ndjson = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes(); From 83d06ed0b9658ef7a8ffb2d87ef7c7e474782697 Mon Sep 17 00:00:00 2001 From: Tamo Date: Mon, 15 Apr 2024 16:48:11 +0200 Subject: [PATCH 3/3] fix the test --- src/indexes.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/src/indexes.rs b/src/indexes.rs index 9baf4209..05c728f1 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -2063,21 +2063,31 @@ mod tests { #[meilisearch_test] async fn test_update_document_json(client: Client, index: Index) -> Result<(), Error> { - let old_json = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes(); - let updated_json = r#"{ "id": 1, "second_body": "second_doggo" }{ "id": 2, "second_body": "second_catto" }"#.as_bytes(); + let old_json = [ + json!({ "id": 1, "body": "doggo" }), + json!({ "id": 2, "body": "catto" }), + ]; + let updated_json = [ + json!({ "id": 1, "second_body": "second_doggo" }), + json!({ "id": 2, "second_body": "second_catto" }), + ]; let task = index - .add_documents(old_json, Some("id")) - .await? + .add_documents(&old_json, Some("id")) + .await + .unwrap() .wait_for_completion(&client, None, None) - .await?; + .await + .unwrap(); let _ = index.get_task(task).await?; let task = index - .add_or_update(updated_json, None) - .await? + .add_or_update(&updated_json, None) + .await + .unwrap() .wait_for_completion(&client, None, None) - .await?; + .await + .unwrap(); let status = index.get_task(task).await?; let elements = index.get_documents::().await.unwrap();