@@ -905,14 +905,12 @@ impl<Http: HttpClient> Index<Http> {
905
905
pub async fn add_or_update < T : Serialize + Send + Sync > (
906
906
& self ,
907
907
documents : & [ T ] ,
908
- primary_key : Option < impl AsRef < str > > ,
908
+ primary_key : Option < & str > ,
909
909
) -> Result < TaskInfo , Error > {
910
910
let url = if let Some ( primary_key) = primary_key {
911
911
format ! (
912
912
"{}/indexes/{}/documents?primaryKey={}" ,
913
- self . client. host,
914
- self . uid,
915
- primary_key. as_ref( )
913
+ self . client. host, self . uid, primary_key
916
914
)
917
915
} else {
918
916
format ! ( "{}/indexes/{}/documents" , self . client. host, self . uid)
@@ -2063,6 +2061,40 @@ mod tests {
2063
2061
assert_eq ! ( res. offset, 2 ) ;
2064
2062
}
2065
2063
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
+
2066
2098
#[ meilisearch_test]
2067
2099
async fn test_add_documents_ndjson ( client : Client , index : Index ) -> Result < ( ) , Error > {
2068
2100
let ndjson = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"# . as_bytes ( ) ;
0 commit comments