Skip to content

Commit 76ae433

Browse files
committed
feat: Add support for compacting database indexes.
1 parent fb13de1 commit 76ae433

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,3 +219,28 @@ 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+
assert client.get_index(uid=common.INDEX_UID)
228+
index: Index = client.index("movies")
229+
# Get stats before compaction
230+
stats_before = index.get_stats()
231+
print(
232+
"Before compaction:",
233+
f"number_of_documents={stats_before.number_of_documents}, "
234+
f"is_indexing={stats_before.is_indexing}"
235+
)
236+
237+
task_info = index.compact()
238+
client.wait_for_task(task_info.task_uid)
239+
stats_after = index.get_stats()
240+
print(
241+
"After compaction:",
242+
f"number_of_documents={stats_after.number_of_documents}, "
243+
f"is_indexing={stats_after.is_indexing}"
244+
)
245+
assert stats_before.number_of_documents == stats_after.number_of_documents
246+
assert stats_after.is_indexing is False

0 commit comments

Comments
 (0)