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
4 changes: 3 additions & 1 deletion .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,9 @@ rename_an_index_1: |-
-X PATCH 'MEILISEARCH_URL/indexes/INDEX_A' \
-H 'Content-Type: application/json' \
--data-binary '{ "uid": "INDEX_B" }'

compact_index_1: |-
curl \
-X POST 'MEILISEARCH_URL/indexes/INDEX_UID/compact'

### Code samples for experimental features
experimental_get_metrics_1: |-
Expand Down
3 changes: 2 additions & 1 deletion docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@
"reference/api/metrics",
"reference/api/logs",
"reference/api/export",
"reference/api/webhooks"
"reference/api/webhooks",
"reference/api/compact"
]
},
{
Expand Down
44 changes: 44 additions & 0 deletions reference/api/compact.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: Compact
description: Use the `/compact` route to reduce database fragmentation.
---

import { RouteHighlighter } from '/snippets/route_highlighter.mdx'

import CodeSamplesCompactIndex1 from '/snippets/samples/code_samples_compact_index_1.mdx';

Index fragmentation occurs naturally as you use Meilisearch and can lead to decreased performance over time. `/compact` reorganizes the database and prunes unused space, which may lead to improved indexing and search speeds.

Meilisearch Cloud monitors database fragmentation and compacts indexes as needed. Self-hosted users may have to build a pipeline to periodically compact indexes and fix performance degradation.

<Tip>
Fragmentation is directly related to the number of indexing operations Meilisearch performs. Common indexing operations include adding and updating documents, as well as changes to index settings.

To estimate your index's fragmentation, query the `/stats` route. If the ratio between `databaseSize` and `usedDatabaseSize` is bigger than 30%, compacting your indexes may improve performance.

If you update documents in your indexes a few times per day, you may benefit from checking fragmentation and compacting your database once per week. If indexing load is very high, compacting indexes multiple times per week may be necessary to ensure optimal performance.
</Tip>

## Compact database

<RouteHighlighter method="POST" path="/indexes/{index_uid}/compact" />

Compact the specified index.

During compaction, Meilisearch must temporarily duplicate the database. Ensure you have at least twice the current size of your database in free disk space when compacting an index.

### Example

<CodeSamplesCompactIndex1 />

#### Response: `202 Accepted`

```json
{
"taskUid": 1,
"indexUid": "INDEX_NAME",
"status": "enqueued",
"type": "IndexCompaction",
"enqueuedAt": "2025-01-01T00:00:00.000000Z"
}
```