Skip to content

Commit 72db507

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 50a4027 commit 72db507

File tree

1 file changed

+171
-0
lines changed

1 file changed

+171
-0
lines changed

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`.

0 commit comments

Comments
 (0)