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
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,5 @@ export_post_1: |-
indexes.put("*", ExportIndexFilter.builder().overrideSettings(true).build());
ExportRequest request = ExportRequest.builder().url("TARGET_INSTANCE_URL").indexes(indexes).build();
client.export(request);
compact_index_1: |-
client.index("INDEX_NAME").compact();
14 changes: 14 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -1295,4 +1295,18 @@ public TaskInfo updateEmbeddersSettings(Map<String, Embedder> embedders)
public TaskInfo resetEmbeddersSettings() throws MeilisearchException {
return this.settingsHandler.resetEmbedders(this.uid);
}

/**
* Compacts the database for this index to reclaim unused space
*
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/compact">API specification</a>
*/
public TaskInfo compact() throws MeilisearchException {
return this.config.httpClient.post(
new URLBuilder("/indexes").addSubroute(this.uid).addSubroute("/compact").getURL(),
null,
TaskInfo.class);
}
}
31 changes: 31 additions & 0 deletions src/test/java/com/meilisearch/integration/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,35 @@ public void testTransientFieldExclusion() throws MeilisearchException {
InaccessibleObjectException.class, () -> gsonWithTransient.toJson(test));
Assertions.assertDoesNotThrow(() -> gson.toJson(test));
}

@Test
public void testCompactWithDocuments() throws Exception {
String indexUid = "testCompactWithDocuments";
Index index = createEmptyIndex(indexUid, this.primaryKey);

TaskInfo addTask =
index.addDocuments(
"[{"
+ "\"id\": 1,"
+ "\"title\": \"Document1\","
+ "\"description\": \"Test document 1\""
+ "},"
+ "{"
+ "\"id\": 2,"
+ "\"title\": \"Document2\","
+ "\"description\": \"Test document 2\""
+ "}]");
index.waitForTask(addTask.getTaskUid());

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)));

assertThat(index.getDocument("1", Movie.class).getTitle(), is(equalTo("Document1")));
assertThat(index.getDocument("2", Movie.class).getTitle(), is(equalTo("Document2")));
}
}