Skip to content

Commit 42917f1

Browse files
authored
Merge pull request #1161 from awais786/adding-compact
feat: Add support for compacting database indexes.
2 parents fb13de1 + ec8613e commit 42917f1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ create_an_index_1: |-
1111
client.create_index('movies', {'primaryKey': 'id'})
1212
update_an_index_1: |-
1313
client.index('movies').update(primary_key='id')
14+
compact_index_1: |-
15+
client.index('movies').compact()
1416
delete_an_index_1: |-
1517
client.delete_index('movies')
1618
// OR

meilisearch/index.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,3 +2333,17 @@ def _build_url(
23332333
if primary_key is None and csv_delimiter is None:
23342334
return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}"
23352335
return f"{self.config.paths.index}/{self.uid}/{self.config.paths.document}?{parse.urlencode(parameters)}"
2336+
2337+
def compact(self) -> TaskInfo:
2338+
"""
2339+
Trigger the compaction of the index.
2340+
This is an asynchronous operation in Meilisearch.
2341+
2342+
Returns
2343+
-------
2344+
task_info: TaskInfo
2345+
Contains information to track the progress of the compaction task.
2346+
"""
2347+
path = f"{self.config.paths.index}/{self.uid}/compact"
2348+
task = self.http.post(path)
2349+
return TaskInfo(**task)

tests/index/test_index.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,18 @@ def test_delete_index(client):
219219
client.wait_for_task(deleted.task_uid)
220220
with pytest.raises(MeilisearchApiError):
221221
client.get_index(uid=common.INDEX_UID)
222+
223+
224+
@pytest.mark.usefixtures("indexes_sample")
225+
def test_index_compact(client):
226+
"""Tests the compaction of an index."""
227+
index = client.index(common.INDEX_UID)
228+
# Get stats before compaction
229+
stats_before = index.get_stats()
230+
231+
task_info = index.compact()
232+
client.wait_for_task(task_info.task_uid)
233+
stats_after = index.get_stats()
234+
235+
assert stats_before.number_of_documents == stats_after.number_of_documents
236+
assert stats_after.is_indexing is False

0 commit comments

Comments
 (0)