diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index a836eeb1..649435fb 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -11,6 +11,8 @@ create_an_index_1: |- client.create_index('movies', {'primaryKey': 'id'}) update_an_index_1: |- client.index('movies').update(primary_key='id') +compact_index_1: |- + client.index('movies').compact() delete_an_index_1: |- client.delete_index('movies') // OR diff --git a/meilisearch/index.py b/meilisearch/index.py index 27db2a2f..0207f1d5 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -2333,3 +2333,17 @@ def _build_url( if primary_key is None and csv_delimiter is None: return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}" return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{parse.urlencode(parameters)}" + + def compact(self) -> TaskInfo: + """ + Trigger the compaction of the index. + This is an asynchronous operation in Meilisearch. + + Returns + ------- + task_info: TaskInfo + Contains information to track the progress of the compaction task. + """ + path = f"{self.config.paths.index}/{self.uid}/compact" + task = self.http.post(path) + return TaskInfo(**task) diff --git a/tests/index/test_index.py b/tests/index/test_index.py index 655477d2..397b6e0c 100644 --- a/tests/index/test_index.py +++ b/tests/index/test_index.py @@ -219,3 +219,18 @@ def test_delete_index(client): client.wait_for_task(deleted.task_uid) with pytest.raises(MeilisearchApiError): client.get_index(uid=common.INDEX_UID) + + +@pytest.mark.usefixtures("indexes_sample") +def test_index_compact(client): + """Tests the compaction of an index.""" + index = client.index(common.INDEX_UID) + # Get stats before compaction + stats_before = index.get_stats() + + task_info = index.compact() + client.wait_for_task(task_info.task_uid) + stats_after = index.get_stats() + + assert stats_before.number_of_documents == stats_after.number_of_documents + assert stats_after.is_indexing is False