Skip to content

Commit a7544c8

Browse files
authored
Merge pull request #860 from lonvia/update-in-batches
Run update in batches
2 parents 919784f + b980373 commit a7544c8

File tree

1 file changed

+19
-15
lines changed
  • app/opensearch/src/main/java/de/komoot/photon/opensearch

1 file changed

+19
-15
lines changed

app/opensearch/src/main/java/de/komoot/photon/opensearch/Updater.java

+19-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public void create(PhotonDoc doc, int objectId) {
2525
.index(PhotonIndex.NAME)
2626
.id(doc.getUid(objectId))
2727
.document(doc)));
28-
++todoDocuments;
28+
29+
if (++todoDocuments > 10000) {
30+
updateDocuments();
31+
}
2932
}
3033

3134
@Override
@@ -34,7 +37,10 @@ public void delete(long docId, int objectId) {
3437
.delete(d -> d
3538
.index(PhotonIndex.NAME)
3639
.id(PhotonDoc.makeUid(docId, objectId))));
37-
++todoDocuments;
40+
41+
if (++todoDocuments > 10000) {
42+
updateDocuments();
43+
}
3844
}
3945

4046
@Override
@@ -58,21 +64,19 @@ public void finish() {
5864
}
5965

6066
private void updateDocuments() {
61-
if (todoDocuments == 0) {
62-
return;
63-
}
67+
if (todoDocuments > 0) {
68+
try {
69+
var response = client.bulk(bulkRequest.build());
6470

65-
try {
66-
var response = client.bulk(bulkRequest.build());
67-
68-
if (response.errors()) {
69-
LOGGER.error("Errors during bulk update.");
71+
if (response.errors()) {
72+
LOGGER.error("Errors during bulk update.");
73+
}
74+
} catch (IOException e) {
75+
LOGGER.error("IO error during bulk update", e);
7076
}
71-
} catch (IOException e) {
72-
LOGGER.error("IO error during bulk update", e);
73-
}
7477

75-
bulkRequest = new BulkRequest.Builder();
76-
todoDocuments = 0;
78+
bulkRequest = new BulkRequest.Builder();
79+
todoDocuments = 0;
80+
}
7781
}
7882
}

0 commit comments

Comments
 (0)