@@ -7,7 +7,7 @@ use crate::{
7
7
tasks:: { Task , TasksCancelQuery , TasksDeleteQuery , TasksResults , TasksSearchQuery } ,
8
8
utils:: async_sleep,
9
9
} ;
10
- use serde:: Deserialize ;
10
+ use serde:: { Deserialize , Serialize } ;
11
11
use serde_json:: { json, Value } ;
12
12
use std:: { collections:: HashMap , time:: Duration } ;
13
13
use time:: OffsetDateTime ;
@@ -19,6 +19,11 @@ pub struct Client {
19
19
pub ( crate ) api_key : String ,
20
20
}
21
21
22
+ #[ derive( Debug , Clone , Serialize , Deserialize ) ]
23
+ pub struct SwapIndexes {
24
+ pub indexes : ( String , String ) ,
25
+ }
26
+
22
27
impl Client {
23
28
/// Create a client using the specified server.
24
29
/// Don't put a '/' at the end of the host.
@@ -329,6 +334,56 @@ impl Client {
329
334
self . list_all_indexes_raw_with ( indexes_query) . await
330
335
}
331
336
337
+ /// Swaps a list of two [Index]'es.
338
+ ///
339
+ /// # Example
340
+ ///
341
+ /// ```
342
+ /// # use meilisearch_sdk::{client::*, indexes::*};
343
+ /// #
344
+ /// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
345
+ /// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
346
+ /// #
347
+ /// # futures::executor::block_on(async move {
348
+ /// // Create the client
349
+ /// let client = Client::new(MEILISEARCH_URL, MEILISEARCH_API_KEY);
350
+ ///
351
+ /// let task_index_1 = client.create_index("swap_index_1", None).await.unwrap();
352
+ /// let task_index_2 = client.create_index("swap_index_2", None).await.unwrap();
353
+ ///
354
+ /// // Wait for the task to complete
355
+ /// task_index_2.wait_for_completion(&client, None, None).await.unwrap();
356
+ ///
357
+ /// let task = client
358
+ /// .swap_indexes([&SwapIndexes {
359
+ /// indexes: (
360
+ /// "swap_index_1".to_string(),
361
+ /// "swap_index_2".to_string(),
362
+ /// ),
363
+ /// }])
364
+ /// .await
365
+ /// .unwrap();
366
+ ///
367
+ /// # client.index("swap_index_1").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
368
+ /// # client.index("swap_index_2").delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
369
+ /// # });
370
+ /// ```
371
+ pub async fn swap_indexes (
372
+ & self ,
373
+ indexes : impl IntoIterator < Item = & SwapIndexes > ,
374
+ ) -> Result < TaskInfo , Error > {
375
+ request :: < ( ) , Vec < & SwapIndexes > , TaskInfo > (
376
+ & format ! ( "{}/swap-indexes" , self . host) ,
377
+ & self . api_key ,
378
+ Method :: Post {
379
+ query : ( ) ,
380
+ body : indexes. into_iter ( ) . collect ( ) ,
381
+ } ,
382
+ 202 ,
383
+ )
384
+ . await
385
+ }
386
+
332
387
/// Get stats of all indexes.
333
388
///
334
389
/// # Example
@@ -961,6 +1016,59 @@ mod tests {
961
1016
use std:: mem;
962
1017
use time:: OffsetDateTime ;
963
1018
1019
+ #[ derive( Debug , Serialize , Deserialize , PartialEq ) ]
1020
+ struct Document {
1021
+ id : String ,
1022
+ }
1023
+
1024
+ #[ meilisearch_test]
1025
+ async fn test_swapping_two_indexes ( client : Client ) {
1026
+ let index_1 = client. index ( "test_swapping_two_indexes_1" ) ;
1027
+ let index_2 = client. index ( "test_swapping_two_indexes_2" ) ;
1028
+
1029
+ let t0 = index_1
1030
+ . add_documents (
1031
+ & [ Document {
1032
+ id : "1" . to_string ( ) ,
1033
+ } ] ,
1034
+ None ,
1035
+ )
1036
+ . await
1037
+ . unwrap ( ) ;
1038
+
1039
+ index_2
1040
+ . add_documents (
1041
+ & [ Document {
1042
+ id : "2" . to_string ( ) ,
1043
+ } ] ,
1044
+ None ,
1045
+ )
1046
+ . await
1047
+ . unwrap ( ) ;
1048
+
1049
+ t0. wait_for_completion ( & client, None , None ) . await . unwrap ( ) ;
1050
+
1051
+ let task = client
1052
+ . swap_indexes ( [ & SwapIndexes {
1053
+ indexes : (
1054
+ "test_swapping_two_indexes_1" . to_string ( ) ,
1055
+ "test_swapping_two_indexes_2" . to_string ( ) ,
1056
+ ) ,
1057
+ } ] )
1058
+ . await
1059
+ . unwrap ( ) ;
1060
+ task. wait_for_completion ( & client, None , None ) . await . unwrap ( ) ;
1061
+
1062
+ let document = index_1. get_document ( "2" ) . await . unwrap ( ) ;
1063
+
1064
+ assert_eq ! (
1065
+ Document {
1066
+ id: "2" . to_string( )
1067
+ } ,
1068
+ document
1069
+ ) ;
1070
+ }
1071
+
964
1072
#[ meilisearch_test]
965
1073
async fn test_methods_has_qualified_version_as_header ( ) {
966
1074
let mock_server_url = & mockito:: server_url ( ) ;
0 commit comments