1
- use std:: { collections:: HashMap , time:: Duration } ;
2
-
3
- use serde:: Deserialize ;
1
+ use serde:: { Deserialize , Serialize } ;
4
2
use serde_json:: { json, Value } ;
3
+ use std:: { collections:: HashMap , time:: Duration } ;
5
4
use time:: OffsetDateTime ;
6
5
7
6
use crate :: {
@@ -10,7 +9,7 @@ use crate::{
10
9
key:: { Key , KeyBuilder , KeyUpdater , KeysQuery , KeysResults } ,
11
10
request:: * ,
12
11
task_info:: TaskInfo ,
13
- tasks:: { Task , TasksQuery , TasksResults } ,
12
+ tasks:: { Task , TasksCancelQuery , TasksDeleteQuery , TasksResults , TasksSearchQuery } ,
14
13
utils:: async_sleep,
15
14
} ;
16
15
@@ -21,6 +20,11 @@ pub struct Client {
21
20
pub ( crate ) api_key : String ,
22
21
}
23
22
23
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
24
+ pub struct SwapIndexes {
25
+ pub indexes : ( String , String ) ,
26
+ }
27
+
24
28
impl Client {
25
29
/// Create a client using the specified server.
26
30
/// Don't put a '/' at the end of the host.
@@ -331,6 +335,56 @@ impl Client {
331
335
self . list_all_indexes_raw_with ( indexes_query) . await
332
336
}
333
337
338
+ /// Swaps a list of two [Index]'es.
339
+ ///
340
+ /// # Example
341
+ ///
342
+ /// ```
343
+ /// # use meilisearch_sdk::{client::*, indexes::*};
344
+ /// #
345
+ /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
346
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
347
+ /// #
348
+ /// # futures::executor::block_on(async move {
349
+ /// // Create the client
350
+ /// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
351
+ ///
352
+ /// let task_index_1 = client.create_index("swap_index_1", None).await.unwrap();
353
+ /// let task_index_2 = client.create_index("swap_index_2", None).await.unwrap();
354
+ ///
355
+ /// // Wait for the task to complete
356
+ /// task_index_2.wait_for_completion(&client, None, None).await.unwrap();
357
+ ///
358
+ /// let task = client
359
+ /// .swap_indexes([&SwapIndexes {
360
+ /// indexes: (
361
+ /// "swap_index_1".to_string(),
362
+ /// "swap_index_2".to_string(),
363
+ /// ),
364
+ /// }])
365
+ /// .await
366
+ /// .unwrap();
367
+ ///
368
+ /// # client.index("swap_index_1").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
369
+ /// # client.index("swap_index_2").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
370
+ /// # });
371
+ /// ```
372
+ pub async fn swap_indexes (
373
+ & self ,
374
+ indexes : impl IntoIterator < Item = & SwapIndexes > ,
375
+ ) -> Result < TaskInfo , Error > {
376
+ request :: < ( ) , Vec < & SwapIndexes > , TaskInfo > (
377
+ & format ! ( "{}/swap-indexes" , self . host) ,
378
+ & self . api_key ,
379
+ Method :: Post {
380
+ query : ( ) ,
381
+ body : indexes. into_iter ( ) . collect ( ) ,
382
+ } ,
383
+ 202 ,
384
+ )
385
+ . await
386
+ }
387
+
334
388
/// Get stats of all indexes.
335
389
///
336
390
/// # Example
@@ -756,16 +810,16 @@ impl Client {
756
810
/// # futures::executor::block_on(async move {
757
811
/// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
758
812
///
759
- /// let mut query = tasks::TasksQuery ::new(&client);
760
- /// query.with_index_uid (["get_tasks_with"]);
813
+ /// let mut query = tasks::TasksSearchQuery ::new(&client);
814
+ /// query.with_index_uids (["get_tasks_with"]);
761
815
/// let tasks = client.get_tasks_with(&query).await.unwrap();
762
816
/// # });
763
817
/// ```
764
818
pub async fn get_tasks_with (
765
819
& self ,
766
- tasks_query : & TasksQuery < ' _ > ,
820
+ tasks_query : & TasksSearchQuery < ' _ > ,
767
821
) -> Result < TasksResults , Error > {
768
- let tasks = request :: < & TasksQuery , ( ) , TasksResults > (
822
+ let tasks = request :: < & TasksSearchQuery , ( ) , TasksResults > (
769
823
& format ! ( "{}/tasks" , self . host) ,
770
824
& self . api_key ,
771
825
Method :: Get { query : tasks_query } ,
@@ -776,6 +830,77 @@ impl Client {
776
830
Ok ( tasks)
777
831
}
778
832
833
+ /// Cancel tasks with filters [TasksCancelQuery]
834
+ ///
835
+ /// # Example
836
+ ///
837
+ /// ```
838
+ /// # use meilisearch_sdk::*;
839
+ /// #
840
+ /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
841
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
842
+ /// #
843
+ /// # futures::executor::block_on(async move {
844
+ /// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
845
+ ///
846
+ /// let mut query = tasks::TasksCancelQuery::new(&client);
847
+ /// query.with_index_uids(["movies"]);
848
+ ///
849
+ /// let res = client.cancel_tasks_with(&query).await.unwrap();
850
+ /// # });
851
+ /// ```
852
+ pub async fn cancel_tasks_with (
853
+ & self ,
854
+ filters : & TasksCancelQuery < ' _ > ,
855
+ ) -> Result < TaskInfo , Error > {
856
+ let tasks = request :: < & TasksCancelQuery , ( ) , TaskInfo > (
857
+ & format ! ( "{}/tasks/cancel" , self . host) ,
858
+ & self . api_key ,
859
+ Method :: Post {
860
+ query : filters,
861
+ body : ( ) ,
862
+ } ,
863
+ 200 ,
864
+ )
865
+ . await ?;
866
+
867
+ Ok ( tasks)
868
+ }
869
+
870
+ /// Delete tasks with filters [TasksDeleteQuery]
871
+ ///
872
+ /// # Example
873
+ ///
874
+ /// ```
875
+ /// # use meilisearch_sdk::*;
876
+ /// #
877
+ /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
878
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
879
+ /// #
880
+ /// # futures::executor::block_on(async move {
881
+ /// # let client = client::Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
882
+ ///
883
+ /// let mut query = tasks::TasksDeleteQuery::new(&client);
884
+ /// query.with_index_uids(["movies"]);
885
+ ///
886
+ /// let res = client.delete_tasks_with(&query).await.unwrap();
887
+ /// # });
888
+ /// ```
889
+ pub async fn delete_tasks_with (
890
+ & self ,
891
+ filters : & TasksDeleteQuery < ' _ > ,
892
+ ) -> Result < TaskInfo , Error > {
893
+ let tasks = request :: < & TasksDeleteQuery , ( ) , TaskInfo > (
894
+ & format ! ( "{}/tasks" , self . host) ,
895
+ & self . api_key ,
896
+ Method :: Delete { query : filters } ,
897
+ 200 ,
898
+ )
899
+ . await ?;
900
+
901
+ Ok ( tasks)
902
+ }
903
+
779
904
/// Get all tasks from the server.
780
905
///
781
906
/// # Example
@@ -791,7 +916,6 @@ impl Client {
791
916
/// let tasks = client.get_tasks().await.unwrap();
792
917
///
793
918
/// # assert!(tasks.results.len() > 0);
794
- /// # client.index("get_tasks").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
795
919
/// # });
796
920
/// ```
797
921
pub async fn get_tasks ( & self ) -> Result < TasksResults , Error > {
@@ -894,8 +1018,62 @@ mod tests {
894
1018
use crate :: {
895
1019
client:: * ,
896
1020
key:: { Action , KeyBuilder } ,
1021
+ tasks:: TasksSearchQuery ,
897
1022
} ;
898
1023
1024
+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1025
+ struct Document {
1026
+ id : String ,
1027
+ }
1028
+
1029
+ #[ meilisearch_test]
1030
+ async fn test_swapping_two_indexes ( client : Client ) {
1031
+ let index_1 = client. index ( "test_swapping_two_indexes_1" ) ;
1032
+ let index_2 = client. index ( "test_swapping_two_indexes_2" ) ;
1033
+
1034
+ let t0 = index_1
1035
+ . add_documents (
1036
+ & [ Document {
1037
+ id : "1" . to_string ( ) ,
1038
+ } ] ,
1039
+ None ,
1040
+ )
1041
+ . await
1042
+ . unwrap ( ) ;
1043
+
1044
+ index_2
1045
+ . add_documents (
1046
+ & [ Document {
1047
+ id : "2" . to_string ( ) ,
1048
+ } ] ,
1049
+ None ,
1050
+ )
1051
+ . await
1052
+ . unwrap ( ) ;
1053
+
1054
+ t0. wait_for_completion ( & client, None , None ) . await . unwrap ( ) ;
1055
+
1056
+ let task = client
1057
+ . swap_indexes ( [ & SwapIndexes {
1058
+ indexes : (
1059
+ "test_swapping_two_indexes_1" . to_string ( ) ,
1060
+ "test_swapping_two_indexes_2" . to_string ( ) ,
1061
+ ) ,
1062
+ } ] )
1063
+ . await
1064
+ . unwrap ( ) ;
1065
+ task. wait_for_completion ( & client, None , None ) . await . unwrap ( ) ;
1066
+
1067
+ let document = index_1. get_document ( "2" ) . await . unwrap ( ) ;
1068
+
1069
+ assert_eq ! (
1070
+ Document {
1071
+ id: "2" . to_string( )
1072
+ } ,
1073
+ document
1074
+ ) ;
1075
+ }
1076
+
899
1077
#[ meilisearch_test]
900
1078
async fn test_methods_has_qualified_version_as_header ( ) {
901
1079
let mock_server_url = & mockito:: server_url ( ) ;
@@ -971,15 +1149,15 @@ mod tests {
971
1149
#[ meilisearch_test]
972
1150
async fn test_get_tasks ( client : Client ) {
973
1151
let tasks = client. get_tasks ( ) . await . unwrap ( ) ;
974
- assert ! ( tasks. results . len ( ) >= 2 ) ;
1152
+ assert ! ( tasks. limit == 20 ) ;
975
1153
}
976
1154
977
1155
#[ meilisearch_test]
978
1156
async fn test_get_tasks_with_params ( client : Client ) {
979
- let query = TasksQuery :: new ( & client) ;
1157
+ let query = TasksSearchQuery :: new ( & client) ;
980
1158
let tasks = client. get_tasks_with ( & query) . await . unwrap ( ) ;
981
1159
982
- assert ! ( tasks. results . len ( ) >= 2 ) ;
1160
+ assert ! ( tasks. limit == 20 ) ;
983
1161
}
984
1162
985
1163
#[ meilisearch_test]
0 commit comments