Skip to content
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

Add the csv_delimiter #455

Merged
merged 1 commit into from
Apr 3, 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
70 changes: 53 additions & 17 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ impl Index {
/// { "id": 2, "body": "catto" }"#.as_bytes(),
/// "application/x-ndjson",
/// Some("id"),
/// None
/// ).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();
Expand All @@ -604,15 +605,16 @@ impl Index {
payload: T,
content_type: &str,
primary_key: Option<&str>,
csv_delimiter: 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
)
} else {
format!("{}/indexes/{}/documents", self.client.host, self.uid)
};
let mut url = format!("{}/indexes/{}/documents?", self.client.host, self.uid);
if let Some(primary_key) = primary_key {
url = format!("{}primaryKey={}&", url, primary_key)
}

if let Some(csv_delimiter) = csv_delimiter {
url = format!("{}csvDelimiter={}", url, csv_delimiter)
}
stream_request::<(), T, TaskInfo>(
&url,
&self.client.api_key,
Expand Down Expand Up @@ -736,13 +738,14 @@ impl Index {
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// # futures::executor::block_on(async move {
/// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
/// let movie_index = client.index("add_or_replace_unchecked_payload");
/// let movie_index = client.index("add_or_update_unchecked_payload");
///
/// let task = movie_index.add_or_update_unchecked_payload(
/// r#"{ "id": 1, "body": "doggo" }
/// { "id": 2, "body": "catto" }"#.as_bytes(),
/// "application/x-ndjson",
/// Some("id"),
/// None
/// ).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();
Expand All @@ -760,15 +763,18 @@ impl Index {
payload: T,
content_type: &str,
primary_key: Option<&str>,
csv_delimiter: 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
)
} else {
format!("{}/indexes/{}/documents", self.client.host, self.uid)
};
let mut url = format!("{}/indexes/{}/documents?", self.client.host, self.uid);

if let Some(primary_key) = primary_key {
url = format!("{}primaryKey={}&", url, primary_key)
}

if let Some(csv_delimiter) = csv_delimiter {
url = format!("{}csvDelimiter={}", url, csv_delimiter)
}

stream_request::<(), T, TaskInfo>(
&url,
&self.client.api_key,
Expand Down Expand Up @@ -1779,6 +1785,36 @@ mod tests {
assert_eq!(res.offset, 2);
}

#[meilisearch_test]
async fn test_add_documents_in_csv_with_custom_delimiter(
client: Client,
index: Index,
) -> Result<(), Error> {
#[derive(Serialize, Deserialize, Debug)]
struct Movie {
name: String,
description: String,
}

let task = index
.add_or_replace_unchecked_payload(
r#"name;description
wonderwoman;She's incredible"#
.as_bytes(),
"text/csv",
Some("name"),
Some(";"),
)
.await
.unwrap();

client.wait_for_task(task, None, None).await.unwrap();
let movies = index.get_documents::<Movie>().await.unwrap();

assert_eq!(movies.results.len(), 1);
Ok(())
}

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