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