Skip to content

Commit fe402ce

Browse files
feat(clients): add replaceAllObjectsWithTransformation (generated)
algolia/api-clients-automation#5013 Co-authored-by: algolia-bot <[email protected]> Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 79b08c2 commit fe402ce

File tree

2 files changed

+76
-7
lines changed

2 files changed

+76
-7
lines changed

lib/Api/IngestionClient.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2842,8 +2842,8 @@ public function chunkedPush(
28422842
$objects,
28432843
$action = 'addObject',
28442844
$waitForTasks = true,
2845-
$referenceIndexName = null,
28462845
$batchSize = 1000,
2846+
$referenceIndexName = null,
28472847
$requestOptions = []
28482848
) {
28492849
$responses = [];
@@ -2852,6 +2852,7 @@ public function chunkedPush(
28522852

28532853
foreach ($objects as $object) {
28542854
$records[] = $object;
2855+
$ok = false;
28552856

28562857
if (sizeof($records) === $batchSize || $count === sizeof($objects) - 1) {
28572858
$responses[] = $this->push($indexName, ['action' => $action, 'records' => $records], false, $referenceIndexName, $requestOptions);
@@ -2869,18 +2870,17 @@ public function chunkedPush(
28692870
$timeoutCalculation = 'Algolia\AlgoliaSearch\Support\Helpers::linearTimeout';
28702871

28712872
foreach ($responses as $response) {
2872-
$this->waitForTask($indexName, $response['taskID']);
28732873
$retry = 0;
28742874

28752875
while ($retry < 50) {
28762876
try {
2877-
$resp = $this->getEvent($response->runID, $response->eventID);
2877+
$this->getEvent($response['runID'], $response['eventID']);
2878+
2879+
$ok = true;
28782880

28792881
break;
28802882
} catch (NotFoundException $e) {
2881-
if (404 === $e->getCode()) {
2882-
return null;
2883-
}
2883+
// just retry
28842884
}
28852885

28862886
++$retry;
@@ -2889,7 +2889,9 @@ public function chunkedPush(
28892889
);
28902890
}
28912891

2892-
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
2892+
if (false === $ok) {
2893+
throw new ExceededRetriesException('Maximum number of retries (50) exceeded.');
2894+
}
28932895
}
28942896
}
28952897

lib/Api/SearchClient.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,73 @@ public function browseSynonyms($indexName, $requestOptions = [])
29582958
return new SynonymIterator($indexName, $this, $requestOptions);
29592959
}
29602960

2961+
/**
2962+
* Helper: Similar to the `replaceAllObjects` method but requires a Push connector (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/) to be created first, in order to transform records before indexing them to Algolia. The `region` must have been passed to the client instantiation method.
2963+
*
2964+
* @param string $indexName the `indexName` to replace `objects` in
2965+
* @param array $objects the array of `objects` to store in the given Algolia `indexName`
2966+
* @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.
2967+
* @param array $requestOptions Request options
2968+
* @param mixed $scopes
2969+
*/
2970+
public function replaceAllObjectsWithTransformation($indexName, $objects, $batchSize = 1000, $scopes = ['settings', 'rules', 'synonyms'], $requestOptions = [])
2971+
{
2972+
if (null == $this->ingestionTransporter) {
2973+
throw new \InvalidArgumentException('`setTransformationRegion` must have been called before calling this method.');
2974+
}
2975+
2976+
$tmpIndexName = $indexName.'_tmp_'.rand(10000000, 99999999);
2977+
2978+
try {
2979+
$copyOperationResponse = $this->operationIndex(
2980+
$indexName,
2981+
[
2982+
'operation' => 'copy',
2983+
'destination' => $tmpIndexName,
2984+
'scope' => $scopes,
2985+
],
2986+
$requestOptions
2987+
);
2988+
2989+
$watchResponses = $this->ingestionTransporter->chunkedPush($tmpIndexName, $objects, 'addObject', true, $batchSize, $indexName, $requestOptions);
2990+
2991+
$this->waitForTask($tmpIndexName, $copyOperationResponse['taskID']);
2992+
2993+
$copyOperationResponse = $this->operationIndex(
2994+
$indexName,
2995+
[
2996+
'operation' => 'copy',
2997+
'destination' => $tmpIndexName,
2998+
'scope' => $scopes,
2999+
],
3000+
$requestOptions
3001+
);
3002+
3003+
$this->waitForTask($tmpIndexName, $copyOperationResponse['taskID']);
3004+
3005+
$moveOperationResponse = $this->operationIndex(
3006+
$tmpIndexName,
3007+
[
3008+
'operation' => 'move',
3009+
'destination' => $indexName,
3010+
],
3011+
$requestOptions
3012+
);
3013+
3014+
$this->waitForTask($tmpIndexName, $moveOperationResponse['taskID']);
3015+
3016+
return [
3017+
'copyOperationResponse' => $copyOperationResponse,
3018+
'watchResponses' => $watchResponses,
3019+
'moveOperationResponse' => $moveOperationResponse,
3020+
];
3021+
} catch (\Throwable $e) {
3022+
$this->deleteIndex($tmpIndexName);
3023+
3024+
throw $e;
3025+
}
3026+
}
3027+
29613028
/**
29623029
* Helper: Replace all objects in an index using a temporary one.
29633030
* See https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation details.

0 commit comments

Comments
 (0)