Skip to content

Commit f601846

Browse files
Added the compact api for indexes
1 parent 2c4ef64 commit f601846

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,3 +852,5 @@ export_post_1: |-
852852
indexes.put("*", ExportIndexFilter.builder().overrideSettings(true).build());
853853
ExportRequest request = ExportRequest.builder().url("TARGET_INSTANCE_URL").indexes(indexes).build();
854854
client.export(request);
855+
compact_index_1: |-
856+
client.index("INDEX_NAME").compact();

src/main/java/com/meilisearch/sdk/Index.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,4 +1295,19 @@ public TaskInfo updateEmbeddersSettings(Map<String, Embedder> embedders)
12951295
public TaskInfo resetEmbeddersSettings() throws MeilisearchException {
12961296
return this.settingsHandler.resetEmbedders(this.uid);
12971297
}
1298+
1299+
1300+
/**
1301+
* Compacts the database for this index to reclaim unused space
1302+
*
1303+
* @return TaskInfo instance
1304+
* @throws MeilisearchException if an error occurs
1305+
* @see <a href="https://www.meilisearch.com/docs/reference/api/compact">API specification</a>
1306+
*/
1307+
public TaskInfo compact() throws MeilisearchException {
1308+
return this.config.httpClient.post(
1309+
new URLBuilder("/indexes").addSubroute(this.uid).addSubroute("compact").getURL(),
1310+
null,
1311+
TaskInfo.class);
1312+
}
12981313
}

src/test/java/com/meilisearch/integration/ClientTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,4 +345,35 @@ public void testTransientFieldExclusion() throws MeilisearchException {
345345
InaccessibleObjectException.class, () -> gsonWithTransient.toJson(test));
346346
Assertions.assertDoesNotThrow(() -> gson.toJson(test));
347347
}
348+
349+
@Test
350+
public void testCompactWithDocuments() throws Exception {
351+
String indexUid = "testCompactWithDocuments";
352+
Index index = createEmptyIndex(indexUid, this.primaryKey);
353+
354+
TaskInfo addTask =
355+
index.addDocuments(
356+
"[{"
357+
+ "\"id\": 1,"
358+
+ "\"title\": \"Document1\","
359+
+ "\"description\": \"Test document 1\""
360+
+ "},"
361+
+ "{"
362+
+ "\"id\": 2,"
363+
+ "\"title\": \"Document2\","
364+
+ "\"description\": \"Test document 2\""
365+
+ "}]");
366+
index.waitForTask(addTask.getTaskUid());
367+
368+
TaskInfo compactTask = index.compact();
369+
client.waitForTask(compactTask.getTaskUid());
370+
Task completedCompactTask = client.getTask(compactTask.getTaskUid());
371+
372+
assertThat(compactTask.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
373+
assertThat(completedCompactTask.getType(), is(equalTo("indexCompaction")));
374+
assertThat(completedCompactTask.getStatus(), is(equalTo(TaskStatus.SUCCEEDED)));
375+
376+
assertThat(index.getDocument("1", Movie.class).getTitle(), is(equalTo("Document1")));
377+
assertThat(index.getDocument("2", Movie.class).getTitle(), is(equalTo("Document2")));
378+
}
348379
}

0 commit comments

Comments
 (0)