-
Notifications
You must be signed in to change notification settings - Fork 5
feat: add support for batch update documents #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cb29470
feat: add support for batch update documents
olamothe 2159183
Merge branch 'main' into CDX-432
olamothe 3efac09
use marshal to json element
olamothe 096b9c1
use map for FileContainer
olamothe 06d46cf
use record
olamothe 6a78c0c
Merge branch 'main' into CDX-432
olamothe 74a1b9f
merge
olamothe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record BatchUpdate(List<DocumentBuilder> addOrUpdate, List<DocumentBuilder> delete) { | ||
| public BatchUpdateRecord marshal() { | ||
| return new BatchUpdateRecord( | ||
| this.addOrUpdate.stream().map(documentBuilder -> documentBuilder.marshalJsonObject()).toArray(JsonObject[]::new), | ||
| this.delete.stream().map(documentBuilder -> documentBuilder.marshalJsonObject()).toArray(JsonObject[]::new) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import com.google.gson.JsonObject; | ||
|
|
||
| public record BatchUpdateRecord(JsonObject[] addOrUpdate, JsonObject[] delete) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.coveo.pushapiclient; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| public class FileContainer { | ||
| public String uploadUri; | ||
| public String fileId; | ||
| public Map<String, String> requiredHeaders; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
|
|
||
| import java.io.IOException; | ||
| import java.net.http.HttpResponse; | ||
| import java.util.ArrayList; | ||
| import java.util.Date; | ||
| import java.util.HashMap; | ||
|
|
||
|
|
@@ -35,19 +36,30 @@ 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("[email protected]")) | ||
| .withDeniedPermissions(new UserSecurityIdentityBuilder(new String[]{"[email protected]", "[email protected]"})); | ||
|
|
||
| ArrayList<DocumentBuilder> docToAdd = new ArrayList<>(); | ||
| ArrayList<DocumentBuilder> docToRemove = new ArrayList<>(); | ||
| for (int i = 0; i < 10; i++) { | ||
| 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))); | ||
| } | ||
| for (int i = 10; i < 20; i++) { | ||
| 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))); | ||
| } | ||
|
|
||
| try { | ||
| source.addOrUpdateDocument(sourceId, simpleDoc); | ||
| source.addOrUpdateDocument(sourceId, docWithMetadata); | ||
| source.batchUpdateDocuments(sourceId, new BatchUpdate(docToAdd, docToRemove)); | ||
| source.addOrUpdateDocument(sourceId, docWithSecurity); | ||
| } catch (IOException | InterruptedException e) { | ||
| System.out.println(e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static void testManageIdentities(Source source) { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL about Records. (https://www.baeldung.com/java-record-keyword). It's quite a neat feature.
Do you think there's place in the code where we should have used it? (ngl, I don't think that'd be worth the hassle but just knowing is good :) )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes !
The only problem is that it does not entirely play well with the GSON library we use to encode/decode to JSON.
So I can't apply it everywhere blindly.
Encoding record with GSON should (or seems to) work, decoding records would explode, however.
For example. the
FileContainerclass in this PR could not use record because we need to decode the platform response with it.But, as I'll be moving to write some tests this week, I'll refactor in some places to use record instead of standard classes.