11use crate :: {
2- client:: Client , document:: * , errors:: Error , progress:: * , request:: * , search:: * ,
2+ client:: Client , document:: * , errors:: Error , errors :: ErrorCode , progress:: * , request:: * , search:: * ,
33} ;
44use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
55use 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