Skip to content

Commit 034ad31

Browse files
meili-botbidoubiwa
authored andcommitted
Add delete_document_with method for Meilisearch v1.2
1 parent 70043d5 commit 034ad31

File tree

2 files changed

+119
-4
lines changed

2 files changed

+119
-4
lines changed

src/documents.rs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::task_info::TaskInfo;
12
use async_trait::async_trait;
23
use serde::{de::DeserializeOwned, Deserialize, Serialize};
34

@@ -301,11 +302,36 @@ impl<'a> DocumentsQuery<'a> {
301302
}
302303
}
303304

305+
#[derive(Debug, Clone, Serialize)]
306+
pub struct DocumentDeletionQuery<'a> {
307+
#[serde(skip_serializing)]
308+
pub index: &'a Index,
309+
310+
/// Filters to apply.
311+
///
312+
/// Read the [dedicated guide](https://docs.meilisearch.com/reference/features/filtering.html) to learn the syntax.
313+
pub filter: &'a str,
314+
}
315+
316+
impl<'a> DocumentDeletionQuery<'a> {
317+
pub fn new(index: &Index) -> DocumentDeletionQuery {
318+
DocumentDeletionQuery { index, filter: "" }
319+
}
320+
321+
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut DocumentDeletionQuery<'a> {
322+
self.filter = filter;
323+
self
324+
}
325+
326+
pub async fn execute<T: DeserializeOwned + 'static>(&self) -> Result<TaskInfo, Error> {
327+
self.index.delete_documents_with(self).await
328+
}
329+
}
330+
304331
#[cfg(test)]
305332
mod tests {
306333
use super::*;
307-
use crate::{client::*, indexes::*};
308-
use ::meilisearch_sdk::documents::IndexConfig;
334+
use crate::{client::*, errors::*, indexes::*};
309335
use meilisearch_test_macro::meilisearch_test;
310336
use serde::{Deserialize, Serialize};
311337

@@ -371,7 +397,6 @@ mod tests {
371397
#[meilisearch_test]
372398
async fn test_get_documents_with_execute(client: Client, index: Index) -> Result<(), Error> {
373399
setup_test_index(&client, &index).await?;
374-
// let documents = index.get_documents(None, None, None).await.unwrap();
375400
let documents = DocumentsQuery::new(&index)
376401
.with_limit(1)
377402
.with_offset(1)
@@ -387,6 +412,41 @@ mod tests {
387412
Ok(())
388413
}
389414

415+
#[meilisearch_test]
416+
async fn test_delete_documents_with(client: Client, index: Index) -> Result<(), Error> {
417+
setup_test_index(&client, &index).await?;
418+
index
419+
.set_filterable_attributes(["id"])
420+
.await
421+
.unwrap()
422+
.wait_for_completion(&client, None, None)
423+
.await
424+
.unwrap();
425+
let mut query = DocumentDeletionQuery::new(&index);
426+
query.with_filter("id = 1");
427+
428+
index
429+
.delete_documents_with(&query)
430+
.await
431+
.unwrap()
432+
.wait_for_completion(&client, None, None)
433+
.await
434+
.unwrap();
435+
let document_result = index.get_document::<MyObject>("1").await;
436+
437+
match document_result {
438+
Ok(_) => panic!("The test was expecting no documents to be returned but got one."),
439+
Err(e) => match e {
440+
Error::Meilisearch(err) => {
441+
assert_eq!(err.error_code, ErrorCode::DocumentNotFound);
442+
}
443+
_ => panic!("The error was expected to be a Meilisearch error, but it was not."),
444+
},
445+
}
446+
447+
Ok(())
448+
}
449+
390450
#[meilisearch_test]
391451
async fn test_get_documents_with_only_one_param(
392452
client: Client,

src/indexes.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
client::Client,
3-
documents::{DocumentQuery, DocumentsQuery, DocumentsResults},
3+
documents::{DocumentDeletionQuery, DocumentQuery, DocumentsQuery, DocumentsResults},
44
errors::Error,
55
request::*,
66
search::*,
@@ -926,6 +926,61 @@ impl Index {
926926
.await
927927
}
928928

929+
/// Delete a selection of documents with filters.
930+
///
931+
/// # Example
932+
///
933+
/// ```
934+
/// # use serde::{Serialize, Deserialize};
935+
/// # use meilisearch_sdk::{client::*, documents::*};
936+
/// #
937+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
938+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
939+
/// #
940+
/// # #[derive(Serialize, Deserialize, Debug)]
941+
/// # struct Movie {
942+
/// # name: String,
943+
/// # id: String,
944+
/// # }
945+
/// #
946+
/// #
947+
/// # futures::executor::block_on(async move {
948+
/// #
949+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY));
950+
/// let index = client.index("delete_documents_with");
951+
/// #
952+
/// # index.set_filterable_attributes(["id"]);
953+
/// # // add some documents
954+
/// # index.add_or_replace(&[Movie{id:String::from("1"), name: String::from("First movie") }, Movie{id:String::from("1"), name: String::from("First movie") }], Some("id")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
955+
///
956+
/// let mut query = DocumentDeletionQuery::new(&index);
957+
/// query.with_filter("id = 1");
958+
/// // delete some documents
959+
/// index.delete_documents_with(&query)
960+
/// .await
961+
/// .unwrap()
962+
/// .wait_for_completion(&client, None, None)
963+
/// .await
964+
/// .unwrap();
965+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
966+
/// # });
967+
/// ```
968+
pub async fn delete_documents_with(
969+
&self,
970+
query: &DocumentDeletionQuery<'_>,
971+
) -> Result<TaskInfo, Error> {
972+
request::<(), &DocumentDeletionQuery, TaskInfo>(
973+
&format!("{}/indexes/{}/documents/delete", self.client.host, self.uid),
974+
self.client.get_api_key(),
975+
Method::Post {
976+
query: (),
977+
body: query,
978+
},
979+
202,
980+
)
981+
.await
982+
}
983+
929984
/// Alias for the [Index::update] method.
930985
pub async fn set_primary_key(
931986
&mut self,

0 commit comments

Comments
 (0)