Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(php): add chunkedBatch method #2796

Merged
merged 4 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading