Skip to content

Commit

Permalink
Merge pull request #860 from lonvia/update-in-batches
Browse files Browse the repository at this point in the history
Run update in batches
  • Loading branch information
lonvia authored Jan 13, 2025
2 parents 919784f + b980373 commit a7544c8
Showing 1 changed file with 19 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ public void create(PhotonDoc doc, int objectId) {
.index(PhotonIndex.NAME)
.id(doc.getUid(objectId))
.document(doc)));
++todoDocuments;

if (++todoDocuments > 10000) {
updateDocuments();
}
}

@Override
Expand All @@ -34,7 +37,10 @@ public void delete(long docId, int objectId) {
.delete(d -> d
.index(PhotonIndex.NAME)
.id(PhotonDoc.makeUid(docId, objectId))));
++todoDocuments;

if (++todoDocuments > 10000) {
updateDocuments();
}
}

@Override
Expand All @@ -58,21 +64,19 @@ public void finish() {
}

private void updateDocuments() {
if (todoDocuments == 0) {
return;
}
if (todoDocuments > 0) {
try {
var response = client.bulk(bulkRequest.build());

try {
var response = client.bulk(bulkRequest.build());

if (response.errors()) {
LOGGER.error("Errors during bulk update.");
if (response.errors()) {
LOGGER.error("Errors during bulk update.");
}
} catch (IOException e) {
LOGGER.error("IO error during bulk update", e);
}
} catch (IOException e) {
LOGGER.error("IO error during bulk update", e);
}

bulkRequest = new BulkRequest.Builder();
todoDocuments = 0;
bulkRequest = new BulkRequest.Builder();
todoDocuments = 0;
}
}
}

0 comments on commit a7544c8

Please sign in to comment.