Skip to content

Commit 322fe6e

Browse files
bors[bot]sanders41
andauthored
Merge #142
142: Adding delete_index_if_exists method r=curquiza a=sanders41 Relates to meilisearch/integration-guides#107 Co-authored-by: Paul Sanders <[email protected]>
2 parents bb61f70 + 2925eb8 commit 322fe6e

File tree

2 files changed

+84
-13
lines changed

2 files changed

+84
-13
lines changed

src/client.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,21 @@ 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, use the [Index::delete_if_exists] method.
130+
pub async fn delete_index_if_exists(&self, uid: &str) -> Result<bool, Error> {
131+
match self.delete_index(uid).await {
132+
Ok (_) => Ok(true),
133+
Err (Error::MeiliSearchError {
134+
message: _,
135+
error_code: ErrorCode::IndexNotFound,
136+
error_type: _,
137+
error_link: _,
138+
}) => Ok(false),
139+
Err(error) => Err(error),
140+
}
141+
}
142+
128143
/// Delete an index from its UID.
129144
/// To delete an index from the [index object](../indexes/struct.Index.html), use [the delete method](../indexes/struct.Index.html#method.delete).
130145
pub async fn delete_index(&self, uid: &str) -> Result<(), Error> {
@@ -317,4 +332,24 @@ mod tests {
317332
let client = Client::new("http://localhost:7700", "masterKey");
318333
client.get_keys().await.unwrap();
319334
}
335+
336+
#[async_test]
337+
async fn test_delete_if_exits() {
338+
let client = Client::new("http://localhost:7700", "masterKey");
339+
let index_name = "movies_delete_if_exists";
340+
client.create_index(index_name, None).await.unwrap();
341+
let mut index = client.get_index(index_name).await;
342+
assert!(index.is_ok());
343+
let deleted = client.delete_index_if_exists(index_name).await.unwrap();
344+
assert_eq!(deleted, true);
345+
index = client.get_index(index_name).await;
346+
assert!(index.is_err());
347+
}
348+
349+
#[async_test]
350+
async fn test_delete_if_exits_none() {
351+
let client = Client::new("http://localhost:7700", "masterKey");
352+
let deleted = client.delete_index_if_exists("bad").await.unwrap();
353+
assert_eq!(deleted, false);
354+
}
320355
}

src/indexes.rs

Lines changed: 49 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,42 @@ 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 (_) => Ok(true),
111+
Err (Error::MeiliSearchError {
112+
message: _,
113+
error_code: ErrorCode::IndexNotFound,
114+
error_type: _,
115+
error_link: _,
116+
}) => Ok(false),
117+
Err(error) => Err(error),
118+
}
119+
}
120+
85121
/// Search for documents matching a specific query in the index.\
86122
/// See also the [search method](#method.search).
87123
///
@@ -620,16 +656,16 @@ impl<'a> Index<'a> {
620656
}
621657

622658
/// Get the status of an update on the index.
623-
///
659+
///
624660
/// After executing an update, a `Progress` struct is returned,
625661
/// you can use this struct to check on the status of the update.
626-
///
662+
///
627663
/// In some cases, you might not need the status of the update directly,
628664
/// or would rather not wait for it to resolve.
629-
///
630-
/// For these cases, you can get the `update_id` from the `Progress`
665+
///
666+
/// For these cases, you can get the `update_id` from the `Progress`
631667
/// struct and use it to query the index later on.
632-
///
668+
///
633669
/// For example, if a clients updates an entry over an HTTP request,
634670
/// you can respond with the `update_id` and have the client check
635671
/// on the update status later on.
@@ -672,7 +708,7 @@ impl<'a> Index<'a> {
672708
/// UpdateStatus::Failed{content} => content.update_id,
673709
/// UpdateStatus::Processed{content} => content.update_id,
674710
/// };
675-
///
711+
///
676712
/// let update_id = progress.get_update_id();
677713
/// // Get update status from the index, using `update_id`
678714
/// let status = movies.get_update(update_id).await.unwrap();
@@ -702,9 +738,9 @@ impl<'a> Index<'a> {
702738
}
703739

704740
/// Get the status of all updates in a given index.
705-
///
741+
///
706742
/// # Example
707-
///
743+
///
708744
/// ```
709745
/// # use serde::{Serialize, Deserialize};
710746
/// # use std::thread::sleep;
@@ -717,7 +753,7 @@ impl<'a> Index<'a> {
717753
/// # value: String,
718754
/// # kind: String,
719755
/// # }
720-
/// #
756+
/// #
721757
/// # impl document::Document for Document {
722758
/// # type UIDType = usize;
723759
/// #
@@ -729,19 +765,19 @@ impl<'a> Index<'a> {
729765
/// # futures::executor::block_on(async move {
730766
/// let client = Client::new("http://localhost:7700", "masterKey");
731767
/// let movies = client.get_or_create("movies_get_all_updates").await.unwrap();
732-
///
768+
///
733769
/// # movies.add_documents(&[
734770
/// # Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() },
735771
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() },
736772
/// # ], None).await.unwrap();
737773
/// # sleep(Duration::from_secs(1));
738-
///
774+
///
739775
/// # movies.add_documents(&[
740776
/// # Document { id: 0, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string() },
741777
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string() },
742778
/// # ], None).await.unwrap();
743779
/// # sleep(Duration::from_secs(1));
744-
///
780+
///
745781
/// let status = movies.get_all_updates().await.unwrap();
746782
/// assert!(status.len() >= 2);
747783
/// # client.delete_index("movies_get_all_updates").await.unwrap();

0 commit comments

Comments
 (0)