From 6f1c451e1bce39b32c167e5ebaf1165e4dc5db90 Mon Sep 17 00:00:00 2001 From: Olivier Lamothe Date: Thu, 1 Jul 2021 10:52:52 -0400 Subject: [PATCH 1/2] feat: support single document deletion --- .../com/coveo/pushapiclient/PlatformClient.java | 13 +++++++++++-- src/main/java/com/coveo/pushapiclient/Source.java | 4 ++++ .../java/com/coveo/testlocally/TestingLocally.java | 9 +++++++-- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 3f6535ef..50a0057a 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -126,8 +126,17 @@ public HttpResponse pushDocument(String sourceId, String documentJSON, S return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } - public void deleteDocument(String sourceId, String documentId, Boolean deleteChildren) { - // TODO + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { + String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/documents?documentId=%s&deleteChildren=%s", sourceId, documentId, deleteChildren)); + + HttpRequest request = HttpRequest.newBuilder() + .headers(headers) + .DELETE() + .uri(uri) + .build(); + + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } public void createFileContainer() { diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index 84813148..56c74324 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -37,4 +37,8 @@ public HttpResponse manageSecurityIdentities(String securityProviderId, public HttpResponse addOrUpdateDocument(String sourceId, DocumentBuilder docBuilder) throws IOException, InterruptedException { return this.platformClient.pushDocument(sourceId, docBuilder.marshal(), docBuilder.getDocument().uri); } + + public HttpResponse deleteDocument(String sourceId, String documentId, Boolean deleteChildren) throws IOException, InterruptedException { + return this.platformClient.deleteDocument(sourceId, documentId, deleteChildren); + } } diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java index afdb857d..acf6f9aa 100644 --- a/src/main/java/com/coveo/testlocally/TestingLocally.java +++ b/src/main/java/com/coveo/testlocally/TestingLocally.java @@ -38,8 +38,13 @@ public static void testPushDocument(String sourceId, Source source) { System.out.println(doc.marshal()); System.out.println(docWithMetadata.marshal()); try { - source.addOrUpdateDocument(sourceId, doc); - source.addOrUpdateDocument(sourceId, docWithMetadata); + HttpResponse resAddSimpleDoc = source.addOrUpdateDocument(sourceId, doc); + HttpResponse resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata); + HttpResponse resDelete = source.deleteDocument(sourceId, doc.getDocument().uri, true); + + System.out.println(resAddSimpleDoc.statusCode()); + System.out.println(resAddOrUpdateMetadata.statusCode()); + System.out.println(resDelete.statusCode()); } catch (IOException | InterruptedException e) { System.out.println(e); } From bac4af76bd62b04d601e280aab0899d873341d52 Mon Sep 17 00:00:00 2001 From: Olivier Lamothe Date: Mon, 5 Jul 2021 13:21:13 -0400 Subject: [PATCH 2/2] fix merge --- src/main/java/com/coveo/testlocally/TestingLocally.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/coveo/testlocally/TestingLocally.java b/src/main/java/com/coveo/testlocally/TestingLocally.java index ec98c599..8d16ccc7 100644 --- a/src/main/java/com/coveo/testlocally/TestingLocally.java +++ b/src/main/java/com/coveo/testlocally/TestingLocally.java @@ -36,7 +36,7 @@ public static void testPushDocument(String sourceId, Source source) { put("my_field_3", 1234); put("my_field_4", new String[]{"a", "b", "c"}); }}); - DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2") + DocumentBuilder docWithSecurity = new DocumentBuilder("https://perdu.com/2", "the title 2") .withData("this is searchable also") .withAllowAnonymousUsers(false) .withAllowedPermissions(new UserSecurityIdentityBuilder("olamothe@coveo.com")) @@ -52,9 +52,9 @@ public static void testPushDocument(String sourceId, Source source) { } try { - HttpResponse resAddSimpleDoc = source.addOrUpdateDocument(sourceId, doc); + HttpResponse resAddSimpleDoc = source.addOrUpdateDocument(sourceId, simpleDoc); HttpResponse resAddOrUpdateMetadata = source.addOrUpdateDocument(sourceId, docWithMetadata); - HttpResponse resDelete = source.deleteDocument(sourceId, doc.getDocument().uri, true); + HttpResponse resDelete = source.deleteDocument(sourceId, simpleDoc.getDocument().uri, true); HttpResponse resBatch = source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove)); @@ -63,7 +63,7 @@ public static void testPushDocument(String sourceId, Source source) { System.out.println(resDelete.statusCode()); System.out.println(resBatch.statusCode()); - + } catch (IOException | InterruptedException e) { System.out.println(e); }