Skip to content

Commit bb98f67

Browse files
authored
feat: add support for batch update documents (#8)
https://coveord.atlassian.net/browse/CDX-432
1 parent 4c33800 commit bb98f67

File tree

7 files changed

+94
-9
lines changed

7 files changed

+94
-9
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.coveo.pushapiclient;
2+
3+
import com.google.gson.JsonObject;
4+
5+
import java.util.List;
6+
7+
public record BatchUpdate(List<DocumentBuilder> addOrUpdate, List<DocumentBuilder> delete) {
8+
public BatchUpdateRecord marshal() {
9+
return new BatchUpdateRecord(
10+
this.addOrUpdate.stream().map(documentBuilder -> documentBuilder.marshalJsonObject()).toArray(JsonObject[]::new),
11+
this.delete.stream().map(documentBuilder -> documentBuilder.marshalJsonObject()).toArray(JsonObject[]::new)
12+
);
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.coveo.pushapiclient;
2+
3+
import com.google.gson.JsonObject;
4+
5+
public record BatchUpdateRecord(JsonObject[] addOrUpdate, JsonObject[] delete) {
6+
}

src/main/java/com/coveo/pushapiclient/DocumentBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,17 @@ public DocumentBuilder withAllowAnonymousUsers(Boolean allowAnonymous) {
152152
}
153153

154154
public String marshal() {
155+
return this.marshalJsonObject().toString();
156+
}
157+
158+
public JsonObject marshalJsonObject() {
155159
JsonObject jsonDocument = new Gson().toJsonTree(this.document).getAsJsonObject();
156160
this.document.metadata.forEach((key, value) -> {
157161
jsonDocument.add(key, new Gson().toJsonTree(value));
158162
});
159163
jsonDocument.remove("metadata");
160-
return jsonDocument.toString();
161-
164+
jsonDocument.addProperty("documentId", this.document.uri);
165+
return jsonDocument;
162166
}
163167

164168
private String dateFormat(DateTime dt) {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.coveo.pushapiclient;
2+
3+
import java.util.Map;
4+
5+
public class FileContainer {
6+
public String uploadUri;
7+
public String fileId;
8+
public Map<String, String> requiredHeaders;
9+
}

src/main/java/com/coveo/pushapiclient/PlatformClient.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,47 @@ public void deleteDocument(String sourceId, String documentId, Boolean deleteChi
130130
// TODO
131131
}
132132

133-
public void createFileContainer() {
134-
// TODO
133+
public HttpResponse<String> createFileContainer() throws IOException, InterruptedException {
134+
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
135+
URI uri = URI.create(this.getBasePushURL() + "/files");
136+
137+
HttpRequest request = HttpRequest.newBuilder()
138+
.headers(headers)
139+
.uri(uri)
140+
.POST(HttpRequest.BodyPublishers.ofString(""))
141+
.build();
142+
143+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
135144
}
136145

137-
public void uploadContentToFileContainer(String sourceId, Object fileContainer) {
138-
// TODO
146+
public HttpResponse<String> uploadContentToFileContainer(String sourceId, FileContainer fileContainer, BatchUpdateRecord batchUpdate) throws IOException, InterruptedException {
147+
String[] headers = fileContainer.requiredHeaders.entrySet()
148+
.stream()
149+
.flatMap(entry -> Stream.of(entry.getKey(), entry.getValue()))
150+
.toArray(String[]::new);
151+
URI uri = URI.create(fileContainer.uploadUri);
152+
153+
154+
HttpRequest request = HttpRequest.newBuilder()
155+
.headers(headers)
156+
.uri(uri)
157+
.PUT(HttpRequest.BodyPublishers.ofString(new Gson().toJson(batchUpdate)))
158+
.build();
159+
160+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
139161
}
140162

141-
public void pushFileContainerContent(String sourceId, Object fileContainer) {
142-
// TODO
163+
public HttpResponse<String> pushFileContainerContent(String sourceId, FileContainer fileContainer) throws IOException, InterruptedException {
164+
String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader());
165+
URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents/batch?fileId=%s", sourceId, fileContainer.fileId));
166+
167+
HttpRequest request = HttpRequest.newBuilder()
168+
.headers(headers)
169+
.uri(uri)
170+
.PUT(HttpRequest.BodyPublishers.ofString(""))
171+
.build();
172+
173+
return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString());
143174
}
144175

145176
private String getBaseSourceURL() {

src/main/java/com/coveo/pushapiclient/Source.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coveo.pushapiclient;
22

3+
import com.google.gson.Gson;
4+
35
import java.io.IOException;
46
import java.net.http.HttpResponse;
57

@@ -37,4 +39,11 @@ public HttpResponse<String> manageSecurityIdentities(String securityProviderId,
3739
public HttpResponse<String> addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException {
3840
return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri);
3941
}
42+
43+
public HttpResponse<String> batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) throws IOException, InterruptedException {
44+
HttpResponse<String> resFileContainer = this.platformClient.createFileContainer();
45+
FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class);
46+
this.platformClient.uploadContentToFileContainer(sourceId, fileContainer, batchUpdate.marshal());
47+
return this.platformClient.pushFileContainerContent(sourceId, fileContainer);
48+
}
4049
}

src/main/java/com/coveo/testlocally/TestingLocally.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.IOException;
88
import java.net.http.HttpResponse;
9+
import java.util.ArrayList;
910
import java.util.Date;
1011
import java.util.HashMap;
1112

@@ -35,19 +36,30 @@ public static void testPushDocument(String sourceId, Source source) {
3536
put("my_field_3", 1234);
3637
put("my_field_4", new String[]{"a", "b", "c"});
3738
}});
38-
DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2")
39+
DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2")
3940
.withData("this is searchable also")
4041
.withAllowAnonymousUsers(false)
4142
.withAllowedPermissions(new UserSecurityIdentityBuilder("[email protected]"))
4243
.withDeniedPermissions(new UserSecurityIdentityBuilder(new String[]{"[email protected]", "[email protected]"}));
4344

45+
ArrayList<DocumentBuilder> docToAdd = new ArrayList<>();
46+
ArrayList<DocumentBuilder> docToRemove = new ArrayList<>();
47+
for (int i = 0; i < 10; i++) {
48+
docToAdd.add(new DocumentBuilder(String.format("https://perdu.com/%s", i), String.format("the title %s", i)).withData(String.format("this is searchable %s", i)));
49+
}
50+
for (int i = 10; i < 20; i++) {
51+
docToRemove.add(new DocumentBuilder(String.format("https://perdu.com/%s", i), String.format("the title %s", i)).withData(String.format("this is searchable %s", i)));
52+
}
53+
4454
try {
4555
source.addOrUpdateDocument(sourceId, simpleDoc);
4656
source.addOrUpdateDocument(sourceId, docWithMetadata);
57+
source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove));
4758
source.addOrUpdateDocument(sourceId, docWithSecurity);
4859
} catch (IOException | InterruptedException e) {
4960
System.out.println(e);
5061
}
62+
5163
}
5264

5365
public static void testManageIdentities(Source source) {

0 commit comments

Comments
 (0)