Skip to content

Commit 7aa8ed0

Browse files
Try #557:
2 parents efacc5c + 19a67f6 commit 7aa8ed0

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

src/indexes.rs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,14 +905,12 @@ impl<Http: HttpClient> Index<Http> {
905905
pub async fn add_or_update<T: Serialize + Send + Sync>(
906906
&self,
907907
documents: &[T],
908-
primary_key: Option<impl AsRef<str>>,
908+
primary_key: Option<&str>,
909909
) -> Result<TaskInfo, Error> {
910910
let url = if let Some(primary_key) = primary_key {
911911
format!(
912912
"{}/indexes/{}/documents?primaryKey={}",
913-
self.client.host,
914-
self.uid,
915-
primary_key.as_ref()
913+
self.client.host, self.uid, primary_key
916914
)
917915
} else {
918916
format!("{}/indexes/{}/documents", self.client.host, self.uid)
@@ -2063,6 +2061,40 @@ mod tests {
20632061
assert_eq!(res.offset, 2);
20642062
}
20652063

2064+
#[meilisearch_test]
2065+
async fn test_update_document_json(client: Client, index: Index) -> Result<(), Error> {
2066+
let old_json = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes();
2067+
let updated_json = r#"{ "id": 1, "second_body": "second_doggo" }{ "id": 2, "second_body": "second_catto" }"#.as_bytes();
2068+
2069+
let task = index
2070+
.add_documents(old_json, Some("id"))
2071+
.await?
2072+
.wait_for_completion(&client, None, None)
2073+
.await?;
2074+
let _ = index.get_task(task).await?;
2075+
2076+
let task = index
2077+
.add_or_update(updated_json, None)
2078+
.await?
2079+
.wait_for_completion(&client, None, None)
2080+
.await?;
2081+
2082+
let status = index.get_task(task).await?;
2083+
let elements = index.get_documents::<serde_json::Value>().await.unwrap();
2084+
2085+
assert!(matches!(status, Task::Succeeded { .. }));
2086+
assert_eq!(elements.results.len(), 2);
2087+
2088+
let expected_result = vec![
2089+
json!( {"body": "doggo", "id": 1, "second_body": "second_doggo"}),
2090+
json!( {"body": "catto", "id": 2, "second_body": "second_catto"}),
2091+
];
2092+
2093+
assert_eq!(elements.results, expected_result);
2094+
2095+
Ok(())
2096+
}
2097+
20662098
#[meilisearch_test]
20672099
async fn test_add_documents_ndjson(client: Client, index: Index) -> Result<(), Error> {
20682100
let ndjson = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes();

0 commit comments

Comments
 (0)