Skip to content

Commit 8f8ce8d

Browse files
feat(clients): add replaceAllObjectsWithTransformation (#5013) (generated) [skip ci]
Co-authored-by: Clément Vannicatte <[email protected]>
1 parent 947a4ca commit 8f8ce8d

File tree

37 files changed

+1249
-57
lines changed

37 files changed

+1249
-57
lines changed

clients/algoliasearch-client-go/algolia/ingestion/api_ingestion.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/algoliasearch-client-go/algolia/search/api_search.go

Lines changed: 109 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

clients/algoliasearch-client-java/algoliasearch/src/main/java/com/algolia/api/SearchClient.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.algolia.model.search.*;
1313
import com.algolia.utils.*;
1414
import com.fasterxml.jackson.core.type.TypeReference;
15+
import com.fasterxml.jackson.databind.ObjectMapper;
1516
import java.nio.charset.Charset;
1617
import java.security.InvalidKeyException;
1718
import java.security.NoSuchAlgorithmException;
@@ -7316,6 +7317,176 @@ public <T> ReplaceAllObjectsResponse replaceAllObjects(
73167317
}
73177318
}
73187319

7320+
/**
7321+
* Helper: Similar to the `saveObjects` method but requires a Push connector
7322+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7323+
* to be created first, in order to transform records before indexing them to Algolia. The
7324+
* `region` must have been passed to the client instantiation method.
7325+
*
7326+
* @param indexName The `indexName` to replace `objects` in.
7327+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
7328+
* @throws AlgoliaRetryException When the retry has failed on all hosts
7329+
* @throws AlgoliaApiException When the API sends an http error code
7330+
* @throws AlgoliaRuntimeException When an error occurred during the serialization
7331+
*/
7332+
public <T> ReplaceAllObjectsWithTransformationResponse replaceAllObjectsWithTransformation(String indexName, Iterable<T> objects) {
7333+
return replaceAllObjectsWithTransformation(indexName, objects, -1);
7334+
}
7335+
7336+
/**
7337+
* Helper: Similar to the `saveObjects` method but requires a Push connector
7338+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7339+
* to be created first, in order to transform records before indexing them to Algolia. The
7340+
* `region` must have been passed to the client instantiation method.
7341+
*
7342+
* @param indexName The `indexName` to replace `objects` in.
7343+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
7344+
* @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
7345+
* to `length(objects) / batchSize`.
7346+
* @throws AlgoliaRetryException When the retry has failed on all hosts
7347+
* @throws AlgoliaApiException When the API sends an http error code
7348+
* @throws AlgoliaRuntimeException When an error occurred during the serialization
7349+
*/
7350+
public <T> ReplaceAllObjectsWithTransformationResponse replaceAllObjectsWithTransformation(
7351+
String indexName,
7352+
Iterable<T> objects,
7353+
int batchSize
7354+
) {
7355+
return replaceAllObjectsWithTransformation(indexName, objects, batchSize, null, null);
7356+
}
7357+
7358+
/**
7359+
* Helper: Similar to the `saveObjects` method but requires a Push connector
7360+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7361+
* to be created first, in order to transform records before indexing them to Algolia. The
7362+
* `region` must have been passed to the client instantiation method.
7363+
*
7364+
* @param indexName The `indexName` to replace `objects` in.
7365+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
7366+
* @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
7367+
* to `length(objects) / batchSize`.
7368+
* @param scopes The `scopes` to keep from the index. Defaults to ['settings', 'rules',
7369+
* 'synonyms'].
7370+
* @throws AlgoliaRetryException When the retry has failed on all hosts
7371+
* @throws AlgoliaApiException When the API sends an http error code
7372+
* @throws AlgoliaRuntimeException When an error occurred during the serialization
7373+
*/
7374+
public <T> ReplaceAllObjectsWithTransformationResponse replaceAllObjectsWithTransformation(
7375+
String indexName,
7376+
Iterable<T> objects,
7377+
int batchSize,
7378+
List<ScopeType> scopes
7379+
) {
7380+
return replaceAllObjectsWithTransformation(indexName, objects, batchSize, scopes, null);
7381+
}
7382+
7383+
/**
7384+
* Helper: Similar to the `saveObjects` method but requires a Push connector
7385+
* (https://www.algolia.com/doc/guides/sending-and-managing-data/send-and-update-your-data/connectors/push/)
7386+
* to be created first, in order to transform records before indexing them to Algolia. The
7387+
* `region` must have been passed to the client instantiation method.
7388+
*
7389+
* @param indexName The `indexName` to replace `objects` in.
7390+
* @param objects The array of `objects` to store in the given Algolia `indexName`.
7391+
* @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
7392+
* to `length(objects) / batchSize`.
7393+
* @param scopes The `scopes` to keep from the index. Defaults to ['settings', 'rules',
7394+
* 'synonyms'].
7395+
* @param requestOptions The requestOptions to send along with the query, they will be merged with
7396+
* the transporter requestOptions. (optional)
7397+
* @throws AlgoliaRetryException When the retry has failed on all hosts
7398+
* @throws AlgoliaApiException When the API sends an http error code
7399+
* @throws AlgoliaRuntimeException When an error occurred during the serialization
7400+
*/
7401+
public <T> ReplaceAllObjectsWithTransformationResponse replaceAllObjectsWithTransformation(
7402+
String indexName,
7403+
Iterable<T> objects,
7404+
int batchSize,
7405+
List<ScopeType> scopes,
7406+
RequestOptions requestOptions
7407+
) {
7408+
if (this.ingestionTransporter == null) {
7409+
throw new AlgoliaRuntimeException("`setTransformationRegion` must have been called before calling this method.");
7410+
}
7411+
7412+
Random rnd = new Random();
7413+
String tmpIndexName = indexName + "_tmp_" + rnd.nextInt(100);
7414+
7415+
if (batchSize == -1) {
7416+
batchSize = 1000;
7417+
}
7418+
7419+
if (scopes == null) {
7420+
scopes = new ArrayList<ScopeType>() {
7421+
{
7422+
add(ScopeType.SETTINGS);
7423+
add(ScopeType.RULES);
7424+
add(ScopeType.SYNONYMS);
7425+
}
7426+
};
7427+
}
7428+
7429+
try {
7430+
// Copy settings, synonyms and rules
7431+
UpdatedAtResponse copyOperationResponse = operationIndex(
7432+
indexName,
7433+
new OperationIndexParams().setOperation(OperationType.COPY).setDestination(tmpIndexName).setScope(scopes),
7434+
requestOptions
7435+
);
7436+
7437+
// Save new objects
7438+
List<WatchResponse> watchResponses =
7439+
this.ingestionTransporter.chunkedPush(
7440+
tmpIndexName,
7441+
objects,
7442+
com.algolia.model.ingestion.Action.ADD_OBJECT,
7443+
true,
7444+
batchSize,
7445+
indexName,
7446+
requestOptions
7447+
);
7448+
7449+
waitForTask(tmpIndexName, copyOperationResponse.getTaskID(), requestOptions);
7450+
7451+
copyOperationResponse = operationIndex(
7452+
indexName,
7453+
new OperationIndexParams().setOperation(OperationType.COPY).setDestination(tmpIndexName).setScope(scopes),
7454+
requestOptions
7455+
);
7456+
waitForTask(tmpIndexName, copyOperationResponse.getTaskID(), requestOptions);
7457+
7458+
// Move temporary index to source index
7459+
UpdatedAtResponse moveOperationResponse = operationIndex(
7460+
tmpIndexName,
7461+
new OperationIndexParams().setOperation(OperationType.MOVE).setDestination(indexName),
7462+
requestOptions
7463+
);
7464+
waitForTask(tmpIndexName, moveOperationResponse.getTaskID(), requestOptions);
7465+
7466+
return new ReplaceAllObjectsWithTransformationResponse()
7467+
.setCopyOperationResponse(copyOperationResponse)
7468+
.setWatchResponses(ingestionResponseToSearchResponse(watchResponses))
7469+
.setMoveOperationResponse(moveOperationResponse);
7470+
} catch (Exception e) {
7471+
deleteIndex(tmpIndexName);
7472+
7473+
throw e;
7474+
}
7475+
}
7476+
7477+
private List<com.algolia.model.search.WatchResponse> ingestionResponseToSearchResponse(
7478+
List<com.algolia.model.ingestion.WatchResponse> responses
7479+
) {
7480+
try {
7481+
ObjectMapper mapper = new ObjectMapper();
7482+
String json = mapper.writeValueAsString(responses);
7483+
7484+
return mapper.readValue(json, new TypeReference<List<com.algolia.model.search.WatchResponse>>() {});
7485+
} catch (Exception e) {
7486+
throw new AlgoliaRuntimeException("ingestion WatchResponse cannot be converted to a search WatchResponse");
7487+
}
7488+
}
7489+
73197490
/**
73207491
* Helper: Generates a secured API key based on the given `parent_api_key` and given
73217492
* `restrictions`.

clients/algoliasearch-client-php/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

0 commit comments

Comments
 (0)