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
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 15 additions & 0 deletions tests/index/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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