1
1
use crate :: {
2
- client:: Client , document:: * , errors:: Error , progress:: * , request:: * , search:: * ,
2
+ client:: Client , document:: * , errors:: Error , errors :: ErrorCode , progress:: * , request:: * , search:: * ,
3
3
} ;
4
4
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
5
5
use serde_json:: json;
@@ -82,6 +82,46 @@ impl<'a> Index<'a> {
82
82
) . await ?)
83
83
}
84
84
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
+
85
125
/// Search for documents matching a specific query in the index.\
86
126
/// See also the [search method](#method.search).
87
127
///
@@ -620,16 +660,16 @@ impl<'a> Index<'a> {
620
660
}
621
661
622
662
/// Get the status of an update on the index.
623
- ///
663
+ ///
624
664
/// After executing an update, a `Progress` struct is returned,
625
665
/// you can use this struct to check on the status of the update.
626
- ///
666
+ ///
627
667
/// In some cases, you might not need the status of the update directly,
628
668
/// 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`
631
671
/// struct and use it to query the index later on.
632
- ///
672
+ ///
633
673
/// For example, if a clients updates an entry over an HTTP request,
634
674
/// you can respond with the `update_id` and have the client check
635
675
/// on the update status later on.
@@ -672,7 +712,7 @@ impl<'a> Index<'a> {
672
712
/// UpdateStatus::Failed{content} => content.update_id,
673
713
/// UpdateStatus::Processed{content} => content.update_id,
674
714
/// };
675
- ///
715
+ ///
676
716
/// let update_id = progress.get_update_id();
677
717
/// // Get update status from the index, using `update_id`
678
718
/// let status = movies.get_update(update_id).await.unwrap();
@@ -702,9 +742,9 @@ impl<'a> Index<'a> {
702
742
}
703
743
704
744
/// Get the status of all updates in a given index.
705
- ///
745
+ ///
706
746
/// # Example
707
- ///
747
+ ///
708
748
/// ```
709
749
/// # use serde::{Serialize, Deserialize};
710
750
/// # use std::thread::sleep;
@@ -717,7 +757,7 @@ impl<'a> Index<'a> {
717
757
/// # value: String,
718
758
/// # kind: String,
719
759
/// # }
720
- /// #
760
+ /// #
721
761
/// # impl document::Document for Document {
722
762
/// # type UIDType = usize;
723
763
/// #
@@ -729,19 +769,19 @@ impl<'a> Index<'a> {
729
769
/// # futures::executor::block_on(async move {
730
770
/// let client = Client::new("http://localhost:7700", "masterKey");
731
771
/// let movies = client.get_or_create("movies_get_all_updates").await.unwrap();
732
- ///
772
+ ///
733
773
/// # movies.add_documents(&[
734
774
/// # Document { id: 0, kind: "title".into(), value: "The Social Network".to_string() },
735
775
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Sorcerer's Stone".to_string() },
736
776
/// # ], None).await.unwrap();
737
777
/// # sleep(Duration::from_secs(1));
738
- ///
778
+ ///
739
779
/// # movies.add_documents(&[
740
780
/// # Document { id: 0, kind: "title".into(), value: "Harry Potter and the Chamber of Secrets".to_string() },
741
781
/// # Document { id: 1, kind: "title".into(), value: "Harry Potter and the Prisoner of Azkaban".to_string() },
742
782
/// # ], None).await.unwrap();
743
783
/// # sleep(Duration::from_secs(1));
744
- ///
784
+ ///
745
785
/// let status = movies.get_all_updates().await.unwrap();
746
786
/// assert!(status.len() >= 2);
747
787
/// # client.delete_index("movies_get_all_updates").await.unwrap();
0 commit comments