Skip to content

Conversation

@panchasarapavan
Copy link
Contributor

@panchasarapavan panchasarapavan commented Nov 7, 2025

Pull Request

Related issue

Fixes #900

What does this PR do?

  • Adds the compact API for indexes.

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

Summary by CodeRabbit

  • New Features

    • Added index compaction to optimize storage and reclaim unused space, exposed via the client API.
    • Included a compact-index code sample demonstrating how to trigger compaction.
  • Tests

    • Added an integration test verifying compaction completes successfully and documents remain accessible afterward.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 7, 2025

Walkthrough

Adds index compaction support: a new public compact() method on Index that POSTs to /indexes/{uid}/compact, an integration test exercising compaction, and a new code sample demonstrating client.index("INDEX_NAME").compact().

Changes

Cohort / File(s) Change Summary
API Implementation
src/main/java/com/meilisearch/sdk/Index.java
Added public TaskInfo compact() which sends HTTP POST to /indexes/{uid}/compact with an empty body and returns the resulting TaskInfo, propagating MeilisearchException on error.
Integration Tests
src/test/java/com/meilisearch/integration/ClientTest.java
Added testCompactWithDocuments that creates an index, adds documents, waits for indexing, triggers compact(), waits for and validates the compact task (enqueued → completed with type indexCompaction and SUCCEEDED), and verifies documents remain retrievable.
Code Samples
.code-samples.meilisearch.yaml
Added compact_index_1 example showing client.index("INDEX_NAME").compact() usage (additive change).

Sequence Diagram(s)

sequenceDiagram
    autonumber
    participant Test as Integration Test
    participant SDK as Index.compact()
    participant API as Meilisearch API
    Note over SDK,API: Compact request flow (new)
    Test->>SDK: call compact()
    SDK->>API: POST /indexes/{uid}/compact
    API-->>SDK: 202 Accepted (TaskInfo)
    SDK-->>Test: returns TaskInfo (task uid)
    Note right of Test: Test polls tasks endpoint to observe status changes
    Test->>API: GET /tasks/{taskUid}
    API-->>Test: {status: enqueued -> succeeded, type: indexCompaction}
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Attention points:
    • Verify HTTP client usage and error propagation in Index.compact() follows existing patterns.
    • Confirm integration test polling/waiting handles async task lifecycle and timeouts robustly.
    • Ensure the new code-sample entry conforms to repository formatting and keys.

Poem

🐇 I nibbled bytes and cleared the stack,
One tiny POST, then no looking back.
Index trimmed and tidy, compact and spry,
I hop with joy — the tasks fly by! ✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: adding a compact API for indexes, which is the primary objective of the PR.
Linked Issues check ✅ Passed All three coding requirements from issue #900 are met: compact() method added to Index class, test case testCompactWithDocuments added, and code sample compact_index_1 added to .code-samples.meilisearch.yaml.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the compact API feature; no unrelated modifications to other functionality or files were introduced.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/main/java/com/meilisearch/sdk/Index.java (1)

1298-1312: Consider adding leading slash for consistency.

The implementation looks correct overall. However, at line 1309, addSubroute("compact") is called without a leading slash, while a similar method at line 1256 uses addSubroute("/similar") with a leading slash. While URLBuilder may handle both patterns, maintaining consistency improves code readability and maintainability.

Apply this diff for consistency:

     public TaskInfo compact() throws MeilisearchException {
         return this.config.httpClient.post(
-            new URLBuilder("/indexes").addSubroute(this.uid).addSubroute("compact").getURL(),
+            new URLBuilder("/indexes").addSubroute(this.uid).addSubroute("/compact").getURL(),
             null,
             TaskInfo.class);
     }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2c4ef64 and f601846.

📒 Files selected for processing (3)
  • .code-samples.meilisearch.yaml (1 hunks)
  • src/main/java/com/meilisearch/sdk/Index.java (1 hunks)
  • src/test/java/com/meilisearch/integration/ClientTest.java (1 hunks)
🔇 Additional comments (2)
.code-samples.meilisearch.yaml (1)

855-856: LGTM!

The code sample correctly demonstrates the compact API usage and follows the same pattern as other samples in this file.

src/test/java/com/meilisearch/integration/ClientTest.java (1)

349-378: LGTM!

The test provides comprehensive coverage of the compact API:

  • Creates an index and adds documents
  • Invokes the compact operation
  • Validates task status transitions (ENQUEUED → SUCCEEDED)
  • Confirms task type is "indexCompaction"
  • Verifies data integrity post-compaction

The test follows the established patterns in this file and includes all necessary assertions.

@Strift
Copy link
Contributor

Strift commented Nov 10, 2025

Hi @panchasarapavan, and thank you for your PR 🙌

Can you look into the failing tests?

If that helps, CONTRIBUTING.md explains how to run tests locally.

@panchasarapavan
Copy link
Contributor Author

@Strift I am looking into them. I had ran them locally. Will check why they are failing here.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/test/java/com/meilisearch/integration/ClientTest.java (1)

349-378: Consider adding a test case for compacting an empty index.

The current test validates compaction with documents. Adding a complementary test for compacting an empty index would improve coverage and ensure the API handles both scenarios gracefully.

Example test structure:

@Test
public void testCompactEmptyIndex() throws Exception {
    String indexUid = "testCompactEmptyIndex";
    Index index = createEmptyIndex(indexUid, this.primaryKey);
    
    TaskInfo compactTask = index.compact();
    client.waitForTask(compactTask.getTaskUid());
    Task completedCompactTask = client.getTask(compactTask.getTaskUid());
    
    assertThat(compactTask.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
    assertThat(completedCompactTask.getType(), is(equalTo("indexCompaction")));
    assertThat(completedCompactTask.getStatus(), is(equalTo(TaskStatus.SUCCEEDED)));
}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d3e5052 and 26230f7.

📒 Files selected for processing (2)
  • src/main/java/com/meilisearch/sdk/Index.java (1 hunks)
  • src/test/java/com/meilisearch/integration/ClientTest.java (1 hunks)
🔇 Additional comments (2)
src/main/java/com/meilisearch/sdk/Index.java (1)

1299-1311: LGTM! Well-implemented compact API.

The compact() method is correctly implemented following the established patterns in the codebase. The Javadoc is clear, the endpoint construction is accurate, and the method signature is consistent with other asynchronous operations that return TaskInfo.

src/test/java/com/meilisearch/integration/ClientTest.java (1)

349-378: LGTM! Comprehensive integration test for index compaction.

The test is well-structured and follows existing patterns in the codebase. It correctly validates the compact operation by checking task status, task type, and data integrity post-compaction.

@panchasarapavan
Copy link
Contributor Author

@Strift i have updated the pr. Fixed the failing tests.

@Strift Strift added the enhancement New feature or request label Nov 11, 2025
@Strift
Copy link
Contributor

Strift commented Nov 11, 2025

Thanks for the swift update!

Let's get this merged 🚀

@Strift Strift merged commit e9f143c into meilisearch:main Nov 11, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v1.23] Add support for compacting database indexes

2 participants