Skip to content

Commit 0fb4d40

Browse files
authored
Merge branch 'main' into feat/go-api-key
2 parents 40aa102 + 6a69064 commit 0fb4d40

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

clients/algoliasearch-client-php/lib/Api/SearchClient.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2806,11 +2806,12 @@ public function browseSynonyms($indexName, $requestOptions = [])
28062806
/**
28072807
* Helper: Replace all objects in an index using a temporary one.
28082808
*
2809-
* @param string $indexName Index name
2810-
* @param array $objects Objects to index
2809+
* @param string $indexName the `indexName` to replace `objects` in
2810+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
2811+
* @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.
28112812
* @param array $requestOptions Request options
28122813
*/
2813-
public function replaceAllObjects($indexName, $objects, $requestOptions = [])
2814+
public function replaceAllObjects($indexName, $objects, $batchSize = 1000, $requestOptions = [])
28142815
{
28152816
$tmpIndex = $indexName.'_tmp_'.uniqid('php_', true);
28162817

@@ -2827,23 +2828,8 @@ public function replaceAllObjects($indexName, $objects, $requestOptions = [])
28272828

28282829
$this->waitForTask($indexName, $copyResponse['taskID']);
28292830

2830-
// Send records (batched)
2831-
$requests = [];
2832-
2833-
foreach ($objects as $record) {
2834-
$requests[] = [
2835-
'action' => 'addObject',
2836-
'body' => $record,
2837-
];
2838-
}
2839-
2840-
$batchResponse = $this->batch(
2841-
$tmpIndex,
2842-
['requests' => $requests],
2843-
$requestOptions
2844-
);
2845-
2846-
$this->waitForTask($tmpIndex, $batchResponse['taskID']);
2831+
// Index objects in chunks
2832+
$this->chunkedBatch($tmpIndex, $objects, 'addObject', true, $batchSize, $requestOptions);
28472833

28482834
// Move temporary index to production
28492835
$moveResponse = $this->operationIndex(
@@ -2858,6 +2844,53 @@ public function replaceAllObjects($indexName, $objects, $requestOptions = [])
28582844
$this->waitForTask($tmpIndex, $moveResponse['taskID']);
28592845
}
28602846

2847+
/**
2848+
* Helper: Chunks the given `objects` list in subset of 1000 elements max in order to make it fit in `batch` requests.
2849+
*
2850+
* @param string $indexName the `indexName` to replace `objects` in
2851+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
2852+
* @param array $action the `batch` `action` to perform on the given array of `objects`, defaults to `addObject`
2853+
* @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
2854+
* @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.
2855+
* @param array $requestOptions Request options
2856+
*/
2857+
public function chunkedBatch(
2858+
$indexName,
2859+
$objects,
2860+
$action = 'addObject',
2861+
$waitForTasks = true,
2862+
$batchSize = 1000,
2863+
$requestOptions = []
2864+
) {
2865+
$responses = [];
2866+
$requests = [];
2867+
$count = 0;
2868+
2869+
foreach ($objects as $object) {
2870+
$requests[] = [
2871+
'action' => $action,
2872+
'body' => $object,
2873+
];
2874+
++$count;
2875+
2876+
if ($count === $batchSize) {
2877+
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
2878+
$requests = [];
2879+
$count = 0;
2880+
}
2881+
}
2882+
2883+
if (!empty($requests)) {
2884+
$responses[] = $this->batch($indexName, ['requests' => $requests], $requestOptions);
2885+
}
2886+
2887+
if ($waitForTasks && !empty($responses)) {
2888+
foreach ($responses as $response) {
2889+
$this->waitForTask($indexName, $response['taskID']);
2890+
}
2891+
}
2892+
}
2893+
28612894
/**
28622895
* Helper: Generate a secured API Key.
28632896
*

0 commit comments

Comments
 (0)