Skip to content

Commit 3a97fad

Browse files
authored
fix(clients): chunked batch helper (#3154)
1 parent 7899ba0 commit 3a97fad

File tree

6 files changed

+1322
-332
lines changed

6 files changed

+1322
-332
lines changed

playground/python/app/search.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ async def main():
1616

1717
try:
1818
resp = await client.replace_all_objects(
19-
index_name="test-flag",
20-
objects=[{"name": f"John Doe{i}", "objectID": f"fff2bd4d-bb17-4e21-a0c4-0a8ea5e363f2{i}" } for i in range(1001)],
19+
index_name="test_replace_all_objects",
20+
objects=[{"name": f"John Doe{i}", "objectID": f"fff2bd4d-bb17-4e21-a0c4-0a8ea5e363f2{i}" } for i in range(33)],
21+
batch_size=10
2122
)
2223

2324
print(resp)

playground/python/poetry.lock

Lines changed: 1308 additions & 320 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

templates/go/search_helpers.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func (c *APIClient) ChunkedBatch(indexName string, objects []map[string]any, act
414414
for i, obj := range objects {
415415
requests = append(requests, *NewBatchRequest(*action, obj))
416416
417-
if i%*batchSize == 0 {
417+
if len(requests) == *batchSize || i == len(objects)-1 {
418418
resp, err := c.Batch(c.NewApiBatchRequest(indexName, NewBatchWriteParams(requests)))
419419
if err != nil {
420420
return nil, err

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,10 @@ async chunkedBatch({ indexName, objects, action = 'addObject', waitForTasks, bat
279279
let requests: Array<BatchRequest> = [];
280280
const responses: Array<BatchResponse> = [];
281281
282-
for (const [i, obj] of objects.entries()) {
282+
const objectEntries = objects.entries();
283+
for (const [i, obj] of objectEntries) {
283284
requests.push({action, body: obj});
284-
if (i % batchSize === 0) {
285+
if (requests.length === batchSize || i === objects.length-1) {
285286
responses.push(await this.batch({indexName, batchWriteParams: {requests}}, requestOptions));
286287
requests = [];
287288
}
@@ -303,7 +304,7 @@ async chunkedBatch({ indexName, objects, action = 'addObject', waitForTasks, bat
303304
* @param replaceAllObjects - The `replaceAllObjects` object.
304305
* @param replaceAllObjects.indexName - The `indexName` to replace `objects` in.
305306
* @param replaceAllObjects.objects - The array of `objects` to store in the given Algolia `indexName`.
306-
* @param replaceAllObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
307+
* @param replaceAllObjects.batchSize - The size of the chunk of `objects`. The number of `batch` calls will be equal to `objects.length / batchSize`. Defaults to 1000.
307308
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
308309
*/
309310
async replaceAllObjects(

templates/php/api.mustache

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,13 @@ use {{invokerPackage}}\Support\Helpers;
476476
'action' => $action,
477477
'body' => $object,
478478
];
479-
$count++;
480479
481-
if ($count === $batchSize) {
480+
if (sizeof($requests) === $batchSize || $count === sizeof($objects)-1) {
482481
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
483482
$requests = [];
484-
$count = 0;
485483
}
484+
485+
$count++;
486486
}
487487

488488
if (!empty($requests)) {
@@ -560,4 +560,4 @@ use {{invokerPackage}}\Support\Helpers;
560560
);
561561
}
562562
}
563-
{{/operations}}
563+
{{/operations}}

templates/python/search_helpers.mustache

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
responses: List[BatchResponse] = []
224224
for i, obj in enumerate(objects):
225225
requests.append(BatchRequest(action=action, body=obj))
226-
if i % batch_size == 0:
226+
if len(requests) == batch_size or i == len(objects) - 1:
227227
responses.append(
228228
await self.batch(
229229
index_name=index_name,
@@ -292,4 +292,4 @@
292292
"copy_operation_response": copy_operation_response,
293293
"batch_responses": batch_responses,
294294
"move_operation_response": move_operation_response,
295-
}
295+
}

0 commit comments

Comments
 (0)