Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -712,10 +712,16 @@ distinct_attribute_guide_1: |-
.set_distinct_attribute("product_id")
.await
.unwrap();
compact_index_1: |-
let task: TaskInfo = client
.index("INDEX_UID")
.compact()
.await
.unwrap();
field_properties_guide_searchable_1: |-
let searchable_attributes = [
"title",
"overvieww",
"overview",
"genres"
];

Expand All @@ -727,7 +733,7 @@ field_properties_guide_searchable_1: |-
field_properties_guide_displayed_1: |-
let displayed_attributes = [
"title",
"overvieww",
"overview",
"genres",
"release_date"
];
Expand Down
61 changes: 61 additions & 0 deletions src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,55 @@ impl<Http: HttpClient> Index<Http> {
Ok(self.primary_key.as_deref())
}

/// Compact this index to reduce disk usage.
///
/// Triggers a compaction task for the current index. Once completed, the
/// index data is compacted on disk.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, tasks::Task};
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
/// # let index = client
/// # .create_index("compact_example", None)
/// # .await
/// # .unwrap()
/// # .wait_for_completion(&client, None, None)
/// # .await
/// # .unwrap()
/// # .try_make_index(&client)
/// # .unwrap();
///
/// let task = index
/// .compact()
/// .await
/// .unwrap()
/// .wait_for_completion(&client, None, None)
/// .await
/// .unwrap();
///
/// assert!(matches!(task, Task::Succeeded { .. }));
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
/// # });
/// ```
pub async fn compact(&self) -> Result<TaskInfo, Error> {
self.client
.http_client
.request::<(), (), TaskInfo>(
&format!("{}/indexes/{}/compact", self.client.host, self.uid),
Method::Post {
query: (),
body: (),
},
202,
)
.await
}

/// Get a [Task] from a specific [Index] to keep track of [asynchronous operations](https://www.meilisearch.com/docs/learn/advanced/asynchronous_operations).
///
/// # Example
Expand Down Expand Up @@ -2441,4 +2490,16 @@ mod tests {
}
Ok(())
}

#[meilisearch_test]
async fn test_compact_index_succeeds(client: Client, index: Index) -> Result<(), Error> {
let task = index
.compact()
.await?
.wait_for_completion(&client, None, None)
.await?;

assert!(task.is_success());
Ok(())
}
}
7 changes: 7 additions & 0 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ pub enum TaskType {
SnapshotCreation {
details: Option<SnapshotCreation>,
},
IndexCompaction {
details: Option<IndexCompaction>,
},
}

#[derive(Debug, Clone, Deserialize)]
Expand Down Expand Up @@ -93,6 +96,10 @@ pub struct IndexDeletion {
#[serde(rename_all = "camelCase")]
pub struct SnapshotCreation {}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IndexCompaction {}

#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DumpCreation {
Expand Down
Loading