Skip to content

Commit 5d834cb

Browse files
authored
Add swap indexes for v0.30.0 (#382)
* Add swap indexes for v0.30.0 * Implement swap indexes * Add indexSwap detail * Use tuples as type for the indexes field * Add tests on index swap * Create better documentation for indexes swap * Fix clippy errors * Fix missing bracket * Fix swap doc test * Make doc more clear
1 parent 4e998c7 commit 5d834cb

File tree

2 files changed

+119
-2
lines changed

2 files changed

+119
-2
lines changed

src/client.rs

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
tasks::{Task, TasksCancelQuery, TasksDeleteQuery, TasksResults, TasksSearchQuery},
88
utils::async_sleep,
99
};
10-
use serde::Deserialize;
10+
use serde::{Deserialize, Serialize};
1111
use serde_json::{json, Value};
1212
use std::{collections::HashMap, time::Duration};
1313
use time::OffsetDateTime;
@@ -19,6 +19,11 @@ pub struct Client {
1919
pub(crate) api_key: String,
2020
}
2121

22+
#[derive(Debug, Clone, Serialize, Deserialize)]
23+
pub struct SwapIndexes {
24+
pub indexes: (String, String),
25+
}
26+
2227
impl Client {
2328
/// Create a client using the specified server.
2429
/// Don't put a '/' at the end of the host.
@@ -329,6 +334,56 @@ impl Client {
329334
self.list_all_indexes_raw_with(indexes_query).await
330335
}
331336

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+
332387
/// Get stats of all indexes.
333388
///
334389
/// # Example
@@ -961,6 +1016,59 @@ mod tests {
9611016
use std::mem;
9621017
use time::OffsetDateTime;
9631018

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+
9641072
#[meilisearch_test]
9651073
async fn test_methods_has_qualified_version_as_header() {
9661074
let mock_server_url = &mockito::server_url();

src/tasks.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use time::OffsetDateTime;
44

55
use crate::{
66
client::Client, errors::Error, errors::MeilisearchError, indexes::Index, settings::Settings,
7-
task_info::TaskInfo,
7+
task_info::TaskInfo, SwapIndexes,
88
};
99

1010
#[derive(Debug, Clone, Deserialize)]
@@ -32,6 +32,9 @@ pub enum TaskType {
3232
DumpCreation {
3333
details: Option<DumpCreation>,
3434
},
35+
IndexSwap {
36+
details: Option<IndexSwap>,
37+
},
3538
TaskCancelation {
3639
details: Option<TaskCancelation>,
3740
},
@@ -92,6 +95,12 @@ pub struct DumpCreation {
9295
pub dump_uid: Option<String>,
9396
}
9497

98+
#[derive(Debug, Clone, Deserialize)]
99+
#[serde(rename_all = "camelCase")]
100+
pub struct IndexSwap {
101+
pub swaps: Vec<SwapIndexes>,
102+
}
103+
95104
#[derive(Debug, Clone, Deserialize)]
96105
#[serde(rename_all = "camelCase")]
97106
pub struct TaskCancelation {

0 commit comments

Comments
 (0)