Skip to content

Commit b86ce3a

Browse files
committed
Adding delete if exists
1 parent bb61f70 commit b86ce3a

File tree

2 files changed

+93
-13
lines changed

2 files changed

+93
-13
lines changed

src/client.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,26 @@ impl<'a> Client<'a> {
125125
.into_index(self))
126126
}
127127

128+
/// Delete an index from its UID if it exists.
129+
/// To delete an index if it exists from the [index object](../indexes/struct.Index.html), use
130+
/// [the delete_if_exists method](../indexes/struct.Index.html#method.delete_if_exists).
131+
pub async fn delete_index_if_exists(&self, uid: &str) -> Result<bool, Error> {
132+
match self.delete_index(uid).await {
133+
Ok (_) => return Ok(true),
134+
Err (error) => {
135+
match error {
136+
Error::MeiliSearchError {
137+
message: _,
138+
error_code: ErrorCode::IndexNotFound,
139+
error_type: _,
140+
error_link: _,
141+
} => return Ok(false),
142+
_ => return Err(error),
143+
};
144+
},
145+
};
146+
}
147+
128148
/// Delete an index from its UID.
129149
/// To delete an index from the [index object](../indexes/struct.Index.html), use [the delete method](../indexes/struct.Index.html#method.delete).
130150
pub async fn delete_index(&self, uid: &str) -> Result<(), Error> {
@@ -317,4 +337,24 @@ mod tests {
317337
let client = Client::new("http://localhost:7700", "masterKey");
318338
client.get_keys().await.unwrap();
319339
}
340+
341+
#[async_test]
342+
async fn test_delete_if_exits() {
343+
let client = Client::new("http://localhost:7700", "masterKey");
344+
let index_name = "movies_delete_if_exists";
345+
client.create_index(index_name, None).await.unwrap();
346+
let mut index = client.get_index(index_name).await;
347+
assert!(index.is_ok());
348+
let deleted = client.delete_index_if_exists(index_name).await.unwrap();
349+
assert_eq!(deleted, true);
350+
index = client.get_index(index_name).await;
351+
assert!(index.is_err());
352+
}
353+
354+
#[async_test]
355+
async fn test_delete_if_exits_none() {
356+
let client = Client::new("http://localhost:7700", "masterKey");
357+
let deleted = client.delete_index_if_exists("bad").await.unwrap();
358+
assert_eq!(deleted, false);
359+
}
320360
}

src/indexes.rs

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
client::Client, document::*, errors::Error, progress::*, request::*, search::*,
2+
client::Client, document::*, errors::Error, errors::ErrorCode, progress::*, request::*, search::*,
33
};
44
use serde::{de::DeserializeOwned, Deserialize, Serialize};
55
use serde_json::json;
@@ -82,6 +82,46 @@ impl<'a> Index<'a> {
8282
).await?)
8383
}
8484

85+
/// Delete the index if it exists.
86+
///
87+
/// # Example
88+
///
89+
/// ```
90+
/// # use meilisearch_sdk::{client::*, indexes::*};
91+
/// # futures::executor::block_on(async move {
92+
/// let client = Client::new("http://localhost:7700", "masterKey");
93+
/// # client.create_index("movies", None).await;
94+
///
95+
/// // get the index named "movies" and delete it
96+
/// let movies = client.assume_index("movies");
97+
/// let mut deleted = movies.delete_if_exists().await.unwrap();
98+
/// assert_eq!(deleted, true);
99+
/// let index = client.get_index("movies").await;
100+
/// assert!(index.is_err());
101+
///
102+
/// // get an index that doesn't exist and try to delete it
103+
/// let no_index = client.assume_index("no_index");
104+
/// deleted = no_index.delete_if_exists().await.unwrap();
105+
/// assert_eq!(deleted, false);
106+
/// # });
107+
/// ```
108+
pub async fn delete_if_exists(self) -> Result<bool, Error> {
109+
match self.delete().await {
110+
Ok (_) => return Ok(true),
111+
Err (error) => {
112+
match error {
113+
Error::MeiliSearchError {
114+
message: _,
115+
error_code: ErrorCode::IndexNotFound,
116+
error_type: _,
117+
error_link: _,
118+
} => return Ok(false),
119+
_ => return Err(error),
120+
};
121+
},
122+
};
123+
}
124+
85125
/// Search for documents matching a specific query in the index.\
86126
/// See also the [search method](#method.search).
87127
///
@@ -620,16 +660,16 @@ impl<'a> Index<'a> {
620660
}
621661

622662
/// Get the status of an update on the index.
623-
///
663+
///
624664
/// After executing an update, a `Progress` struct is returned,
625665
/// you can use this struct to check on the status of the update.
626-
///
666+
///
627667
/// In some cases, you might not need the status of the update directly,
628668
/// or would rather not wait for it to resolve.
629-
///
630-
/// For these cases, you can get the `update_id` from the `Progress`
669+
///
670+
/// For these cases, you can get the `update_id` from the `Progress`
631671
/// struct and use it to query the index later on.
632-
///
672+
///
633673
/// For example, if a clients updates an entry over an HTTP request,
634674
/// you can respond with the `update_id` and have the client check
635675
/// on the update status later on.
@@ -672,7 +712,7 @@ impl<'a> Index<'a> {
672712
/// UpdateStatus::Failed{content} => content.update_id,
673713
/// UpdateStatus::Processed{content} => content.update_id,
674714
/// };
675-
///
715+
///
676716
/// let update_id = progress.get_update_id();
677717
/// // Get update status from the index, using `update_id`
678718
/// let status = movies.get_update(update_id).await.unwrap();
@@ -702,9 +742,9 @@ impl<'a> Index<'a> {
702742
}
703743

704744
/// Get the status of all updates in a given index.
705-
///
745+
///
706746
/// # Example
707-
///
747+
///
708748
/// ```
709749
/// # use serde::{Serialize, Deserialize};
710750
/// # use std::thread::sleep;
@@ -717,7 +757,7 @@ impl<'a> Index<'a> {
717757
/// # value: String,
718758
/// # kind: String,
719759
/// # }
720-
/// #
760+
/// #
721761
/// # impl document::Document for Document {
722762
/// # type UIDType = usize;
723763
/// #
@@ -729,19 +769,19 @@ impl<'a> Index<'a> {
729769
/// # futures::executor::block_on(async move {
730770
/// let client = Client::new("http://localhost:7700", "masterKey");
731771
/// let movies = client.get_or_create("movies_get_all_updates").await.unwrap();
732-
///
772+
///
733773
/// # movies.add_documents(&[
734774
/// # Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() },
735775
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() },
736776
/// # ], None).await.unwrap();
737777
/// # sleep(Duration::from_secs(1));
738-
///
778+
///
739779
/// # movies.add_documents(&[
740780
/// # Document { id: 0, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string() },
741781
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string() },
742782
/// # ], None).await.unwrap();
743783
/// # sleep(Duration::from_secs(1));
744-
///
784+
///
745785
/// let status = movies.get_all_updates().await.unwrap();
746786
/// assert!(status.len() >= 2);
747787
/// # client.delete_index("movies_get_all_updates").await.unwrap();

0 commit comments

Comments
 (0)