Skip to content

add functions for csv documents #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,96 @@ impl Index {
.await
}

/// Add a raw csv payload and update them if they already.
///
/// It configures the correct content type for csv data.
///
/// If you send an already existing document (same id) the old document will be only partially updated according to the fields of the new document.
/// Thus, any fields not present in the new document are kept and remained unchanged.
///
/// To completely overwrite a document, check out the [`Index::add_documents_csv`] documents method.
///
/// # Example
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use meilisearch_sdk::{client::*, indexes::*};
/// # use std::thread::sleep;
/// # use std::time::Duration;
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// # futures::executor::block_on(async move {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
/// let movie_index = client.index("update_documents_csv");
///
/// let task = movie_index.update_documents_csv(
/// "id,body\n1,\"doggo\"\n2,\"catto\"".as_bytes(),
/// Some("id"),
/// ).await.unwrap();
/// // Meilisearch may take some time to execute the request so we are going to wait till it's completed
/// client.wait_for_task(task, None, None).await.unwrap();
///
/// let movies = movie_index.get_documents::<serde_json::Value>().await.unwrap();
/// assert!(movies.results.len() == 2);
/// # movie_index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub async fn update_documents_csv<T: futures_io::AsyncRead + Send + Sync + 'static>(
&self,
payload: T,
primary_key: Option<&str>,
) -> Result<TaskInfo, Error> {
self.add_or_update_unchecked_payload(payload, "text/csv", primary_key)
.await
}

/// Add a raw csv payload to meilisearch.
///
/// It configures the correct content type for csv data.
///
/// If you send an already existing document (same id) the **whole existing document** will be overwritten by the new document.
/// Fields previously in the document not present in the new document are removed.
///
/// For a partial update of the document see [`Index::update_documents_csv`].
///
/// # Example
///
/// ```
/// # use serde::{Serialize, Deserialize};
/// # use meilisearch_sdk::{client::*, indexes::*};
/// # use std::thread::sleep;
/// # use std::time::Duration;
/// #
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// # futures::executor::block_on(async move {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
/// let movie_index = client.index("add_documents_csv");
///
/// let task = movie_index.add_documents_csv(
/// "id,body\n1,\"doggo\"\n2,\"catto\"".as_bytes(),
/// Some("id"),
/// ).await.unwrap();
/// // Meilisearch may take some time to execute the request so we are going to wait till it's completed
/// client.wait_for_task(task, None, None).await.unwrap();
///
/// let movies = movie_index.get_documents::<serde_json::Value>().await.unwrap();
/// assert!(movies.results.len() == 2);
/// # movie_index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
#[cfg(not(target_arch = "wasm32"))]
pub async fn add_documents_csv<T: futures_io::AsyncRead + Send + Sync + 'static>(
&self,
payload: T,
primary_key: Option<&str>,
) -> Result<TaskInfo, Error> {
self.add_or_replace_unchecked_payload(payload, "text/csv", primary_key)
.await
}

/// Add a list of documents and update them if they already.
///
/// If you send an already existing document (same id) the old document will be only partially updated according to the fields of the new document.
Expand Down Expand Up @@ -2007,6 +2097,58 @@ mod tests {
Ok(())
}

#[meilisearch_test]
async fn test_add_documents_csv(client: Client, index: Index) -> Result<(), Error> {
let csv_input = "id,body\n1,\"doggo\"\n2,\"catto\"".as_bytes();

let task = index
.add_documents_csv(csv_input, Some("id"))
.await?
.wait_for_completion(&client, None, None)
.await?;

let status = index.get_task(task).await?;
let elements = index.get_documents::<serde_json::Value>().await.unwrap();
assert!(matches!(status, Task::Succeeded { .. }));
assert!(elements.results.len() == 2);

Ok(())
}

#[meilisearch_test]
async fn test_update_documents_csv(client: Client, index: Index) -> Result<(), Error> {
let old_csv = "id,body\n1,\"doggo\"\n2,\"catto\"".as_bytes();
let updated_csv = "id,body\n1,\"new_doggo\"\n2,\"new_catto\"".as_bytes();
// Add first njdson document
let task = index
.add_documents_csv(old_csv, Some("id"))
.await?
.wait_for_completion(&client, None, None)
.await?;
let _ = index.get_task(task).await?;

// Update via njdson document
let task = index
.update_documents_csv(updated_csv, Some("id"))
.await?
.wait_for_completion(&client, None, None)
.await?;

let status = index.get_task(task).await?;
let elements = index.get_documents::<serde_json::Value>().await.unwrap();

assert!(matches!(status, Task::Succeeded { .. }));
assert!(elements.results.len() == 2);

let expected_result = vec![
json!( {"body": "new_doggo", "id": "1"}),
json!( {"body": "new_catto", "id": "2"}),
];

assert_eq!(elements.results, expected_result);

Ok(())
}
#[meilisearch_test]
async fn test_get_one_task(client: Client, index: Index) -> Result<(), Error> {
let task = index
Expand Down