Skip to content

Commit

Permalink
feat(php): add chunkedBatch method (#2796)
Browse files Browse the repository at this point in the history
  • Loading branch information
damcou authored Feb 28, 2024
1 parent 1e072bf commit 608fd29
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 29 deletions.
16 changes: 8 additions & 8 deletions playground/php/src/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
//
//$client->waitForTask($indexName, $response['taskID']);

//$newGuys = [
// ['objectID' => "3", 'name' => 'Hubert'],
// ['objectID' => "4", 'name' => 'Bob'],
// ['objectID' => "5", 'name' => $env['SEARCH_QUERY']],
//];
//
//$response = $client->replaceAllObjects($indexName, $newGuys);
//
$newGuys = [
['objectID' => "3", 'name' => 'Hubert'],
['objectID' => "4", 'name' => 'Bob'],
['objectID' => "5", 'name' => $env['SEARCH_QUERY']],
];

$response = $client->replaceAllObjects($indexName, $newGuys, 2);

//var_dump(
// $client->search([
// 'requests' => [
Expand Down
74 changes: 53 additions & 21 deletions templates/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ use {{invokerPackage}}\Support\Helpers;
/**
* Helper: Replace all objects in an index using a temporary one.
*
* @param string $indexName Index name
* @param array $objects Objects to index
* @param string $indexName The `indexName` to replace `objects` in.
* @param array $objects The array of `objects` to store in the given Algolia `indexName`.
* @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.
* @param array $requestOptions Request options
*
*/
public function replaceAllObjects($indexName, $objects, $requestOptions = [])
public function replaceAllObjects($indexName, $objects, $batchSize = 1000, $requestOptions = [])
{
$tmpIndex = $indexName.'_tmp_'.uniqid('php_', true);
Expand All @@ -432,23 +432,8 @@ use {{invokerPackage}}\Support\Helpers;
$this->waitForTask($indexName, $copyResponse['taskID']);
// Send records (batched)
$requests = [];
foreach ($objects as $record) {
$requests[] = [
'action' => 'addObject',
'body' => $record
];
}

$batchResponse = $this->batch(
$tmpIndex,
['requests' => $requests],
$requestOptions
);

$this->waitForTask($tmpIndex, $batchResponse['taskID']);
// Index objects in chunks
$this->chunkedBatch($tmpIndex, $objects, 'addObject', true, $batchSize, $requestOptions);
// Move temporary index to production
$moveResponse = $this->operationIndex(
Expand All @@ -463,6 +448,53 @@ use {{invokerPackage}}\Support\Helpers;
$this->waitForTask($tmpIndex, $moveResponse['taskID']);
}

/**
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
*
* @param string $indexName The `indexName` to replace `objects` in.
* @param array $objects The array of `objects` to store in the given Algolia `indexName`.
* @param array $action The `batch` `action` to perform on the given array of `objects`, defaults to `addObject`.
* @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.
* @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.
* @param array $requestOptions Request options
*/
public function chunkedBatch(
$indexName,
$objects,
$action = 'addObject',
$waitForTasks = true,
$batchSize = 1000,
$requestOptions = []
) {
$responses = [];
$requests = [];
$count = 0;
foreach ($objects as $object) {
$requests[] = [
'action' => $action,
'body' => $object,
];
$count++;
if ($count === $batchSize) {
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
$requests = [];
$count = 0;
}
}

if (!empty($requests)) {
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
}

if ($waitForTasks && !empty($responses)) {
foreach ($responses as $response) {
$this->waitForTask($indexName, $response['taskID']);
}
}
}

/**
* Helper: Generate a secured API Key
*
Expand Down

0 comments on commit 608fd29

Please sign in to comment.