Skip to content

Commit cb32534

Browse files
meili-bors[bot]Nguyen Trung Khanhcurquizairevoire
authored
Merge #557
557: fix(indexes): allow None `primary_key` in `add_or_update` function r=curquiza a=khanhnt2 # Pull Request ## Related issue Cannot pass `None` value to `add_or_update` function ## What does this PR do? Replace `impl AsRef<str>` to `&str` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Nguyen Trung Khanh <[email protected]> Co-authored-by: Khanh Nguyen <[email protected]> Co-authored-by: Clémentine U. - curqui <[email protected]> Co-authored-by: Tamo <[email protected]>
2 parents 3a76275 + 83d06ed commit cb32534

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/indexes.rs

+46-4
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,50 @@ 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 = [
2067+
json!({ "id": 1, "body": "doggo" }),
2068+
json!({ "id": 2, "body": "catto" }),
2069+
];
2070+
let updated_json = [
2071+
json!({ "id": 1, "second_body": "second_doggo" }),
2072+
json!({ "id": 2, "second_body": "second_catto" }),
2073+
];
2074+
2075+
let task = index
2076+
.add_documents(&old_json, Some("id"))
2077+
.await
2078+
.unwrap()
2079+
.wait_for_completion(&client, None, None)
2080+
.await
2081+
.unwrap();
2082+
let _ = index.get_task(task).await?;
2083+
2084+
let task = index
2085+
.add_or_update(&updated_json, None)
2086+
.await
2087+
.unwrap()
2088+
.wait_for_completion(&client, None, None)
2089+
.await
2090+
.unwrap();
2091+
2092+
let status = index.get_task(task).await?;
2093+
let elements = index.get_documents::<serde_json::Value>().await.unwrap();
2094+
2095+
assert!(matches!(status, Task::Succeeded { .. }));
2096+
assert_eq!(elements.results.len(), 2);
2097+
2098+
let expected_result = vec![
2099+
json!( {"body": "doggo", "id": 1, "second_body": "second_doggo"}),
2100+
json!( {"body": "catto", "id": 2, "second_body": "second_catto"}),
2101+
];
2102+
2103+
assert_eq!(elements.results, expected_result);
2104+
2105+
Ok(())
2106+
}
2107+
20662108
#[meilisearch_test]
20672109
async fn test_add_documents_ndjson(client: Client, index: Index) -> Result<(), Error> {
20682110
let ndjson = r#"{ "id": 1, "body": "doggo" }{ "id": 2, "body": "catto" }"#.as_bytes();

0 commit comments

Comments
 (0)