Skip to content

Commit 608fd29

Browse files
authored
feat(php): add chunkedBatch method (#2796)
1 parent 1e072bf commit 608fd29

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

playground/php/src/search.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@
4040
//
4141
//$client->waitForTask($indexName, $response['taskID']);
4242

43-
//$newGuys = [
44-
// ['objectID' => "3", 'name' => 'Hubert'],
45-
// ['objectID' => "4", 'name' => 'Bob'],
46-
// ['objectID' => "5", 'name' => $env['SEARCH_QUERY']],
47-
//];
48-
//
49-
//$response = $client->replaceAllObjects($indexName, $newGuys);
50-
//
43+
$newGuys = [
44+
['objectID' => "3", 'name' => 'Hubert'],
45+
['objectID' => "4", 'name' => 'Bob'],
46+
['objectID' => "5", 'name' => $env['SEARCH_QUERY']],
47+
];
48+
49+
$response = $client->replaceAllObjects($indexName, $newGuys, 2);
50+
5151
//var_dump(
5252
// $client->search([
5353
// 'requests' => [

templates/php/api.mustache

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -410,12 +410,12 @@ use {{invokerPackage}}\Support\Helpers;
410410
/**
411411
* Helper: Replace all objects in an index using a temporary one.
412412
*
413-
* @param string $indexName Index name
414-
* @param array $objects Objects to index
413+
* @param string $indexName The `indexName` to replace `objects` in.
414+
* @param array $objects The array of `objects` to store in the given Algolia `indexName`.
415+
* @param array $batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
415416
* @param array $requestOptions Request options
416-
*
417417
*/
418-
public function replaceAllObjects($indexName, $objects, $requestOptions = [])
418+
public function replaceAllObjects($indexName, $objects, $batchSize = 1000, $requestOptions = [])
419419
{
420420
$tmpIndex = $indexName.'_tmp_'.uniqid('php_', true);
421421
@@ -432,23 +432,8 @@ use {{invokerPackage}}\Support\Helpers;
432432
433433
$this->waitForTask($indexName, $copyResponse['taskID']);
434434
435-
// Send records (batched)
436-
$requests = [];
437-
438-
foreach ($objects as $record) {
439-
$requests[] = [
440-
'action' => 'addObject',
441-
'body' => $record
442-
];
443-
}
444-
445-
$batchResponse = $this->batch(
446-
$tmpIndex,
447-
['requests' => $requests],
448-
$requestOptions
449-
);
450-
451-
$this->waitForTask($tmpIndex, $batchResponse['taskID']);
435+
// Index objects in chunks
436+
$this->chunkedBatch($tmpIndex, $objects, 'addObject', true, $batchSize, $requestOptions);
452437
453438
// Move temporary index to production
454439
$moveResponse = $this->operationIndex(
@@ -463,6 +448,53 @@ use {{invokerPackage}}\Support\Helpers;
463448
$this->waitForTask($tmpIndex, $moveResponse['taskID']);
464449
}
465450

451+
/**
452+
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
453+
*
454+
* @param string $indexName The `indexName` to replace `objects` in.
455+
* @param array $objects The array of `objects` to store in the given Algolia `indexName`.
456+
* @param array $action The `batch` `action` to perform on the given array of `objects`, defaults to `addObject`.
457+
* @param array $waitForTasks Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
458+
* @param array $batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal to `length(objects) / batchSize`. Defaults to 1000.
459+
* @param array $requestOptions Request options
460+
*/
461+
public function chunkedBatch(
462+
$indexName,
463+
$objects,
464+
$action = 'addObject',
465+
$waitForTasks = true,
466+
$batchSize = 1000,
467+
$requestOptions = []
468+
) {
469+
$responses = [];
470+
$requests = [];
471+
$count = 0;
472+
473+
foreach ($objects as $object) {
474+
$requests[] = [
475+
'action' => $action,
476+
'body' => $object,
477+
];
478+
$count++;
479+
480+
if ($count === $batchSize) {
481+
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
482+
$requests = [];
483+
$count = 0;
484+
}
485+
}
486+
487+
if (!empty($requests)) {
488+
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
489+
}
490+
491+
if ($waitForTasks && !empty($responses)) {
492+
foreach ($responses as $response) {
493+
$this->waitForTask($indexName, $response['taskID']);
494+
}
495+
}
496+
}
497+
466498
/**
467499
* Helper: Generate a secured API Key
468500
*

0 commit comments

Comments
 (0)