Skip to content

feat(index): add compact() method for index compaction#2133

Open
mixelburg wants to merge 3 commits intomeilisearch:mainfrom
mixelburg:feat/compact-database
Open

feat(index): add compact() method for index compaction#2133
mixelburg wants to merge 3 commits intomeilisearch:mainfrom
mixelburg:feat/compact-database

Conversation

@mixelburg
Copy link
Copy Markdown
Contributor

@mixelburg mixelburg commented Mar 2, 2026

Summary

Adds support for the index compaction API introduced in Meilisearch v1.23 (reference).

Changes

  • Added compact() method to the Index class — calls POST /indexes/{indexUid}/compact
  • Added tests/compact.test.ts with permission, auth denial, and URL construction test cases
  • Added compact_index_1 code sample to .code-samples.meilisearch.yaml

Usage

// Trigger compaction and wait for it to complete
const task = await client.index('movies').compact().waitTask();
console.log(task.status); // 'succeeded'

Closes

Closes #2066

Summary by CodeRabbit

  • New Features

    • Added the ability to compact indexes to optimize storage and improve performance.
  • Documentation

    • Added a code sample demonstrating how to run an index compaction.
  • Tests

    • Added end-to-end tests for index compaction, covering permission scenarios, error handling, and URL/host behavior.

Closes meilisearch#2066

Implements the `POST /indexes/{indexUid}/compact` endpoint introduced
in Meilisearch v1.23. Compaction reorganizes the index database to
reclaim space and improve read performance.

Example:
```ts
await client.index('movies').compact().waitTask();
```
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 2, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9d22dc1 and f656c8e.

📒 Files selected for processing (1)
  • .code-samples.meilisearch.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • .code-samples.meilisearch.yaml

📝 Walkthrough

Walkthrough

Adds a new Index.compact() API to enqueue a compaction task for an index, includes end-to-end tests for permissions, URL handling, and a new code sample demonstrating client.index('movies').compact().

Changes

Cohort / File(s) Summary
Code Sample
.code-samples.meilisearch.yaml
Added compact_index_1 example that calls client.index('movies').compact() to demonstrate the compact endpoint.
Index Compaction Method
src/indexes.ts
Added public compact(): EnqueuedTaskPromise to Index that POSTs to indexes/{uid}/compact via existing HTTP-with-task client.
Tests
tests/compact.test.ts
New E2E tests covering Master/Admin success, Search/no-key failures with expected error codes, and host/URL formation error cases for the compact endpoint.

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Client as Client\n(rpc caller)
    participant Index as Index.compact()\n(SDK method)
    participant HTTP as HTTP Client\n(post request)
    participant Server as MeiliSearch\n(API & Task queue)
    Client->>Index: client.index('movies').compact()
    Index->>HTTP: POST /indexes/movies/compact
    HTTP->>Server: HTTP request (with API key)
    Server-->>HTTP: 202 Accepted + enqueued task payload
    HTTP-->>Index: EnqueuedTaskPromise
    Index-->>Client: return EnqueuedTaskPromise
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A tiny hop, a compaction call,
I bundle bits so indexes fall small.
Tests peep, keys check, hosts tidy and neat,
The SDK hums, compact and sweet. ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title clearly and concisely describes the main change: adding a compact() method to the Index class for index compaction.
Linked Issues check ✅ Passed The PR successfully addresses all coding requirements from issue #2066: adds the compact() method to interact with POST /indexes/{indexUid}/compact, includes test cases for permissions and error handling, and adds the compact_index_1 code sample.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the compact() functionality as specified in issue #2066; no out-of-scope modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v1.23] Add support for compacting database indexes

1 participant