Skip to content

Commit 2c4ef64

Browse files
ernestorbemxbrunoocasalicurquiza
authored
Add support for export API (#882)
* Add support for export API * Follow CodeRabbit review suggestions * Fix ExportRequestTest tests * Update export API example --------- Co-authored-by: Bruno Casali <[email protected]> Co-authored-by: Clémentine <[email protected]>
1 parent 81cf1e2 commit 2c4ef64

File tree

6 files changed

+195
-0
lines changed

6 files changed

+195
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,3 +847,8 @@ update_localized_attribute_settings_1: |-
847847
);
848848
reset_localized_attribute_settings_1: |-
849849
client.index("INDEX_NAME").resetLocalizedAttributesSettings();
850+
export_post_1: |-
851+
Map<String, ExportIndexFilter> indexes = new HashMap<>();
852+
indexes.put("*", ExportIndexFilter.builder().overrideSettings(true).build());
853+
ExportRequest request = ExportRequest.builder().url("TARGET_INSTANCE_URL").indexes(indexes).build();
854+
client.export(request);

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,18 @@ public TaskInfo createSnapshot() throws MeilisearchException {
218218
return config.httpClient.post("/snapshots", "", TaskInfo.class);
219219
}
220220

221+
/**
222+
* Triggers the export of documents between Meilisearch instances.
223+
*
224+
* @param request Export request parameters
225+
* @return Meilisearch API response as TaskInfo
226+
* @throws MeilisearchException if an error occurs
227+
* @see <a href="https://www.meilisearch.com/docs/reference/api/export">API specification</a>
228+
*/
229+
public TaskInfo export(ExportRequest request) throws MeilisearchException {
230+
return config.httpClient.post("/export", request, TaskInfo.class);
231+
}
232+
221233
/**
222234
* Gets the status and availability of a Meilisearch instance
223235
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.meilisearch.sdk;
2+
3+
import lombok.*;
4+
import org.json.JSONObject;
5+
6+
@Builder
7+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
8+
@NoArgsConstructor(access = AccessLevel.PACKAGE)
9+
@Getter
10+
@Setter
11+
public class ExportIndexFilter {
12+
private String filter;
13+
@Builder.Default private boolean overrideSettings = false;
14+
15+
/**
16+
* Method that returns the JSON String of the ExportIndexFilter
17+
*
18+
* @return JSON String of the ExportIndexFilter query
19+
*/
20+
@Override
21+
public String toString() {
22+
JSONObject jsonObject = new JSONObject();
23+
if (this.filter != null) {
24+
jsonObject.put("filter", this.filter);
25+
}
26+
if (this.overrideSettings) {
27+
jsonObject.put("overrideSettings", true);
28+
}
29+
30+
return jsonObject.toString();
31+
}
32+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.meilisearch.sdk;
2+
3+
import java.util.Map;
4+
import lombok.*;
5+
import org.json.JSONObject;
6+
7+
@Builder
8+
@AllArgsConstructor(access = AccessLevel.PACKAGE)
9+
@NoArgsConstructor(access = AccessLevel.PACKAGE)
10+
@Getter
11+
@Setter
12+
public class ExportRequest {
13+
private String url;
14+
private String apiKey;
15+
private String payloadSize;
16+
private Map<String, ExportIndexFilter> indexes;
17+
18+
/**
19+
* Method that returns the JSON String of the ExportRequest
20+
*
21+
* @return JSON String of the ExportRequest query
22+
*/
23+
@Override
24+
public String toString() {
25+
JSONObject jsonObject =
26+
new JSONObject()
27+
.put("url", this.url)
28+
.putOpt("apiKey", this.apiKey)
29+
.putOpt("payloadSize", this.payloadSize)
30+
.putOpt("indexes", this.indexes);
31+
return jsonObject.toString();
32+
}
33+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77
import com.google.gson.*;
88
import com.meilisearch.integration.classes.AbstractIT;
99
import com.meilisearch.integration.classes.TestData;
10+
import com.meilisearch.sdk.ExportIndexFilter;
11+
import com.meilisearch.sdk.ExportRequest;
1012
import com.meilisearch.sdk.Index;
1113
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
1214
import com.meilisearch.sdk.exceptions.MeilisearchException;
1315
import com.meilisearch.sdk.model.*;
1416
import com.meilisearch.sdk.utils.Movie;
1517
import java.lang.reflect.InaccessibleObjectException;
1618
import java.lang.reflect.Modifier;
19+
import java.util.HashMap;
20+
import java.util.Map;
1721
import org.junit.jupiter.api.*;
1822

1923
@Tag("integration")
@@ -309,6 +313,22 @@ public void testCreateSnapshot() throws Exception {
309313
assertThat(snapshot.getType(), is(equalTo("snapshotCreation")));
310314
}
311315

316+
/** Test call to initiate export */
317+
@Test
318+
public void testExport() throws Exception {
319+
Map<String, ExportIndexFilter> indexes = new HashMap<>();
320+
indexes.put("*", ExportIndexFilter.builder().filter("genres = action").build());
321+
322+
ExportRequest payload =
323+
ExportRequest.builder().url(getMeilisearchHost()).indexes(indexes).build();
324+
TaskInfo task = client.export(payload);
325+
client.waitForTask(task.getTaskUid());
326+
Task exportTask = client.getTask(task.getTaskUid());
327+
328+
assertThat(task.getStatus(), is(equalTo(TaskStatus.ENQUEUED)));
329+
assertThat(exportTask.getType(), is(equalTo("export")));
330+
}
331+
312332
/**
313333
* Test the exclusion of transient fields.
314334
*
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.meilisearch.sdk;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
import org.json.JSONObject;
10+
import org.junit.jupiter.api.Test;
11+
12+
public class ExportRequestTest {
13+
14+
@Test
15+
void toStringSimpleExportIndexFilter() {
16+
ExportIndexFilter filter = ExportIndexFilter.builder().build();
17+
JSONObject json = new JSONObject(filter.toString());
18+
assertThat(json.has("overrideSettings"), is(false));
19+
assertThat(json.has("filter"), is(false));
20+
}
21+
22+
@Test
23+
void toStringExportIndexFilterWithOverride() {
24+
ExportIndexFilter filter = ExportIndexFilter.builder().overrideSettings(true).build();
25+
JSONObject json = new JSONObject(filter.toString());
26+
assertThat(json.getBoolean("overrideSettings"), is(true));
27+
assertThat(json.has("filter"), is(false));
28+
}
29+
30+
@Test
31+
void toStringExportIndexFilterWithFilter() {
32+
ExportIndexFilter filter = ExportIndexFilter.builder().filter("status = 'active'").build();
33+
JSONObject json = new JSONObject(filter.toString());
34+
assertThat(json.getString("filter"), is(equalTo("status = 'active'")));
35+
assertThat(json.has("overrideSettings"), is(false));
36+
}
37+
38+
@Test
39+
void toStringSimpleExportRequest() {
40+
ExportRequest request =
41+
ExportRequest.builder().url("http://localhost:7711").payloadSize("123 MiB").build();
42+
JSONObject json = new JSONObject(request.toString());
43+
assertThat(json.getString("url"), is(equalTo("http://localhost:7711")));
44+
assertThat(json.getString("payloadSize"), is(equalTo("123 MiB")));
45+
assertThat(json.has("apiKey"), is(false));
46+
assertThat(json.has("indexes"), is(false));
47+
}
48+
49+
@Test
50+
void toStringExportRequestWithIndexes() {
51+
Map<String, ExportIndexFilter> indexes = new HashMap<>();
52+
indexes.put("*", ExportIndexFilter.builder().overrideSettings(true).build());
53+
54+
ExportRequest request =
55+
ExportRequest.builder()
56+
.url("http://localhost:7711")
57+
.payloadSize("123 MiB")
58+
.indexes(indexes)
59+
.build();
60+
61+
JSONObject json = new JSONObject(request.toString());
62+
63+
assertThat(json.getString("url"), is(equalTo("http://localhost:7711")));
64+
assertThat(json.getString("payloadSize"), is(equalTo("123 MiB")));
65+
assertThat(json.has("apiKey"), is(false));
66+
JSONObject indexesJson = json.getJSONObject("indexes");
67+
JSONObject starIndex = indexesJson.getJSONObject("*");
68+
assertThat(starIndex.has("filter"), is(false));
69+
assertThat(starIndex.getBoolean("overrideSettings"), is(true));
70+
}
71+
72+
@Test
73+
void gettersExportRequest() {
74+
Map<String, ExportIndexFilter> indexes = new HashMap<>();
75+
indexes.put(
76+
"myindex",
77+
ExportIndexFilter.builder().filter("id > 10").overrideSettings(false).build());
78+
79+
ExportRequest request =
80+
ExportRequest.builder()
81+
.url("http://localhost:7711")
82+
.apiKey("mykey")
83+
.payloadSize("50 MiB")
84+
.indexes(indexes)
85+
.build();
86+
87+
assertThat(request.getUrl(), is(equalTo("http://localhost:7711")));
88+
assertThat(request.getApiKey(), is(equalTo("mykey")));
89+
assertThat(request.getPayloadSize(), is(equalTo("50 MiB")));
90+
assertThat(request.getIndexes().get("myindex").getFilter(), is(equalTo("id > 10")));
91+
assertThat(request.getIndexes().get("myindex").isOverrideSettings(), is(false));
92+
}
93+
}

0 commit comments

Comments
 (0)