Skip to content

Commit

Permalink
Merge #557
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
4 people authored Apr 15, 2024
2 parents 3a76275 + 83d06ed commit cb32534
Showing 1 changed file with 46 additions and 4 deletions.
50 changes: 46 additions & 4 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,14 +905,12 @@ impl<Http: HttpClient> Index<Http> {
pub async fn add_or_update<T: Serialize + Send + Sync>(
&self,
documents: &[T],
primary_key: Option<impl AsRef<str>>,
primary_key: Option<&str>,
) -> Result<TaskInfo, Error> {
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)
Expand Down Expand Up @@ -2063,6 +2061,50 @@ mod tests {
assert_eq!(res.offset, 2);
}

#[meilisearch_test]
async fn test_update_document_json(client: Client, index: Index) -> Result<(), Error> {
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
.unwrap()
.wait_for_completion(&client, None, None)
.await
.unwrap();
let _ = index.get_task(task).await?;

let task = index
.add_or_update(&updated_json, None)
.await
.unwrap()
.wait_for_completion(&client, None, None)
.await
.unwrap();

let status = index.get_task(task).await?;
let elements = index.get_documents::<serde_json::Value>().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();
Expand Down

0 comments on commit cb32534

Please sign in to comment.