Skip to content

Commit d725a3c

Browse files
Merge #728
728: Add support for compacting database indexes r=curquiza a=kumarUjjawal # Pull Request ## Related issue Fixes #727 ## What does this PR do? - I noticed a typo in the `.code-samples.meilisearch.yaml` so fixed that as well. - ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added ability to trigger index compaction and monitor its completion as an async task. * **Tests** * Added a test to verify index compaction completes successfully. * **Documentation** * Added a compact-index code sample. * Fixed typos in field properties guide examples (corrected "overview" in searchable and displayed attributes). <sub>✏️ Tip: You can customize this high-level summary in your review settings.</sub> <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Kumar Ujjawal <[email protected]>
2 parents cb04d6d + 1bc40e9 commit d725a3c

File tree

3 files changed

+76
-2
lines changed

3 files changed

+76
-2
lines changed

.code-samples.meilisearch.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,16 @@ distinct_attribute_guide_1: |-
719719
.set_distinct_attribute("product_id")
720720
.await
721721
.unwrap();
722+
compact_index_1: |-
723+
let task: TaskInfo = client
724+
.index("INDEX_UID")
725+
.compact()
726+
.await
727+
.unwrap();
722728
field_properties_guide_searchable_1: |-
723729
let searchable_attributes = [
724730
"title",
725-
"overvieww",
731+
"overview",
726732
"genres"
727733
];
728734
@@ -734,7 +740,7 @@ field_properties_guide_searchable_1: |-
734740
field_properties_guide_displayed_1: |-
735741
let displayed_attributes = [
736742
"title",
737-
"overvieww",
743+
"overview",
738744
"genres",
739745
"release_date"
740746
];

src/indexes.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,55 @@ impl<Http: HttpClient> Index<Http> {
13251325
Ok(self.primary_key.as_deref())
13261326
}
13271327

1328+
/// Compact this index to reduce disk usage.
1329+
///
1330+
/// Triggers a compaction task for the current index. Once completed, the
1331+
/// index data is compacted on disk.
1332+
///
1333+
/// # Example
1334+
///
1335+
/// ```
1336+
/// # use meilisearch_sdk::{client::*, indexes::*, tasks::Task};
1337+
/// # let MEILISEARCH_URL = option_env!("MEILISEARCH_URL").unwrap_or("http://localhost:7700");
1338+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1339+
/// # tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap().block_on(async {
1340+
/// # let client = Client::new(MEILISEARCH_URL, Some(MEILISEARCH_API_KEY)).unwrap();
1341+
/// # let index = client
1342+
/// # .create_index("compact_example", None)
1343+
/// # .await
1344+
/// # .unwrap()
1345+
/// # .wait_for_completion(&client, None, None)
1346+
/// # .await
1347+
/// # .unwrap()
1348+
/// # .try_make_index(&client)
1349+
/// # .unwrap();
1350+
///
1351+
/// let task = index
1352+
/// .compact()
1353+
/// .await
1354+
/// .unwrap()
1355+
/// .wait_for_completion(&client, None, None)
1356+
/// .await
1357+
/// .unwrap();
1358+
///
1359+
/// assert!(matches!(task, Task::Succeeded { .. }));
1360+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1361+
/// # });
1362+
/// ```
1363+
pub async fn compact(&self) -> Result<TaskInfo, Error> {
1364+
self.client
1365+
.http_client
1366+
.request::<(), (), TaskInfo>(
1367+
&format!("{}/indexes/{}/compact", self.client.host, self.uid),
1368+
Method::Post {
1369+
query: (),
1370+
body: (),
1371+
},
1372+
202,
1373+
)
1374+
.await
1375+
}
1376+
13281377
/// Get a [Task] from a specific [Index] to keep track of [asynchronous operations](https://www.meilisearch.com/docs/learn/advanced/asynchronous_operations).
13291378
///
13301379
/// # Example
@@ -2441,4 +2490,16 @@ mod tests {
24412490
}
24422491
Ok(())
24432492
}
2493+
2494+
#[meilisearch_test]
2495+
async fn test_compact_index_succeeds(client: Client, index: Index) -> Result<(), Error> {
2496+
let task = index
2497+
.compact()
2498+
.await?
2499+
.wait_for_completion(&client, None, None)
2500+
.await?;
2501+
2502+
assert!(task.is_success());
2503+
Ok(())
2504+
}
24442505
}

src/tasks.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ pub enum TaskType {
4545
SnapshotCreation {
4646
details: Option<SnapshotCreation>,
4747
},
48+
IndexCompaction {
49+
details: Option<IndexCompaction>,
50+
},
4851
}
4952

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

99+
#[derive(Debug, Clone, Deserialize)]
100+
#[serde(rename_all = "camelCase")]
101+
pub struct IndexCompaction {}
102+
96103
#[derive(Debug, Clone, Deserialize)]
97104
#[serde(rename_all = "camelCase")]
98105
pub struct DumpCreation {

0 commit comments

Comments
 (0)