diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 2976520b..bc705954 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -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" ]; @@ -727,7 +733,7 @@ field_properties_guide_searchable_1: |- field_properties_guide_displayed_1: |- let displayed_attributes = [ "title", - "overvieww", + "overview", "genres", "release_date" ]; diff --git a/src/indexes.rs b/src/indexes.rs index febf25fa..b45bf5de 100644 --- a/src/indexes.rs +++ b/src/indexes.rs @@ -1325,6 +1325,55 @@ impl Index { 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 { + 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 @@ -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(()) + } } diff --git a/src/tasks.rs b/src/tasks.rs index c06b21d7..8f51c31c 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -45,6 +45,9 @@ pub enum TaskType { SnapshotCreation { details: Option, }, + IndexCompaction { + details: Option, + }, } #[derive(Debug, Clone, Deserialize)] @@ -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 {