@@ -6572,24 +6572,6 @@ public <T> List<BatchResponse> chunkedBatch(
65726572 return chunkedBatch (indexName , objects , action , waitForTasks , 1000 , requestOptions );
65736573 }
65746574
6575- /**
6576- * Push a new set of objects and remove all previous ones. Settings, synonyms and query rules are
6577- * untouched. Replace all records in an index without any downtime. See
6578- * https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation
6579- * details.
6580- *
6581- * @param indexName The `indexName` to replace `objects` in.
6582- * @param objects The array of `objects` to store in the given Algolia `indexName`.
6583- * @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
6584- * to `length(objects) / batchSize`.
6585- * @throws AlgoliaRetryException When the retry has failed on all hosts
6586- * @throws AlgoliaApiException When the API sends an http error code
6587- * @throws AlgoliaRuntimeException When an error occurred during the serialization
6588- */
6589- public <T > ReplaceAllObjectsResponse replaceAllObjects (String indexName , Iterable <T > objects , int batchSize ) {
6590- return replaceAllObjects (indexName , objects , batchSize , null );
6591- }
6592-
65936575 /**
65946576 * Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used
65956577 * under the hood, which creates a `batch` requests with at most 1000 objects in it.
@@ -6822,6 +6804,40 @@ public <T> List<BatchResponse> partialUpdateObjects(
68226804 );
68236805 }
68246806
6807+ /**
6808+ * Push a new set of objects and remove all previous ones. Settings, synonyms and query rules are
6809+ * untouched. Replace all records in an index without any downtime. See
6810+ * https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation
6811+ * details.
6812+ *
6813+ * @param indexName The `indexName` to replace `objects` in.
6814+ * @param objects The array of `objects` to store in the given Algolia `indexName`.
6815+ * @throws AlgoliaRetryException When the retry has failed on all hosts
6816+ * @throws AlgoliaApiException When the API sends an http error code
6817+ * @throws AlgoliaRuntimeException When an error occurred during the serialization
6818+ */
6819+ public <T > ReplaceAllObjectsResponse replaceAllObjects (String indexName , Iterable <T > objects ) {
6820+ return replaceAllObjects (indexName , objects , -1 );
6821+ }
6822+
6823+ /**
6824+ * Push a new set of objects and remove all previous ones. Settings, synonyms and query rules are
6825+ * untouched. Replace all records in an index without any downtime. See
6826+ * https://api-clients-automation.netlify.app/docs/add-new-api-client#5-helpers for implementation
6827+ * details.
6828+ *
6829+ * @param indexName The `indexName` to replace `objects` in.
6830+ * @param objects The array of `objects` to store in the given Algolia `indexName`.
6831+ * @param batchSize The size of the chunk of `objects`. The number of `batch` calls will be equal
6832+ * to `length(objects) / batchSize`.
6833+ * @throws AlgoliaRetryException When the retry has failed on all hosts
6834+ * @throws AlgoliaApiException When the API sends an http error code
6835+ * @throws AlgoliaRuntimeException When an error occurred during the serialization
6836+ */
6837+ public <T > ReplaceAllObjectsResponse replaceAllObjects (String indexName , Iterable <T > objects , int batchSize ) {
6838+ return replaceAllObjects (indexName , objects , batchSize , null );
6839+ }
6840+
68256841 /**
68266842 * Push a new set of objects and remove all previous ones. Settings, synonyms and query rules are
68276843 * untouched. Replace all records in an index without any downtime. See
@@ -6847,47 +6863,57 @@ public <T> ReplaceAllObjectsResponse replaceAllObjects(
68476863 Random rnd = new Random ();
68486864 String tmpIndexName = indexName + "_tmp_" + rnd .nextInt (100 );
68496865
6850- // Copy settings, synonyms and rules
6851- UpdatedAtResponse copyOperationResponse = operationIndex (
6852- indexName ,
6853- new OperationIndexParams ()
6854- .setOperation (OperationType .COPY )
6855- .setDestination (tmpIndexName )
6856- .addScope (ScopeType .SETTINGS )
6857- .addScope (ScopeType .RULES )
6858- .addScope (ScopeType .SYNONYMS ),
6859- requestOptions
6860- );
6866+ if (batchSize == -1 ) {
6867+ batchSize = 1000 ;
6868+ }
68616869
6862- // Save new objects
6863- List <BatchResponse > batchResponses = chunkedBatch (tmpIndexName , objects , Action .ADD_OBJECT , true , batchSize , requestOptions );
6870+ try {
6871+ // Copy settings, synonyms and rules
6872+ UpdatedAtResponse copyOperationResponse = operationIndex (
6873+ indexName ,
6874+ new OperationIndexParams ()
6875+ .setOperation (OperationType .COPY )
6876+ .setDestination (tmpIndexName )
6877+ .addScope (ScopeType .SETTINGS )
6878+ .addScope (ScopeType .RULES )
6879+ .addScope (ScopeType .SYNONYMS ),
6880+ requestOptions
6881+ );
68646882
6865- waitForTask (tmpIndexName , copyOperationResponse .getTaskID (), requestOptions );
6883+ // Save new objects
6884+ List <BatchResponse > batchResponses = chunkedBatch (tmpIndexName , objects , Action .ADD_OBJECT , true , batchSize , requestOptions );
68666885
6867- copyOperationResponse = operationIndex (
6868- indexName ,
6869- new OperationIndexParams ()
6870- .setOperation (OperationType .COPY )
6871- .setDestination (tmpIndexName )
6872- .addScope (ScopeType .SETTINGS )
6873- .addScope (ScopeType .RULES )
6874- .addScope (ScopeType .SYNONYMS ),
6875- requestOptions
6876- );
6877- waitForTask (tmpIndexName , copyOperationResponse .getTaskID (), requestOptions );
6886+ waitForTask (tmpIndexName , copyOperationResponse .getTaskID (), requestOptions );
68786887
6879- // Move temporary index to source index
6880- UpdatedAtResponse moveOperationResponse = operationIndex (
6881- tmpIndexName ,
6882- new OperationIndexParams ().setOperation (OperationType .MOVE ).setDestination (indexName ),
6883- requestOptions
6884- );
6885- waitForTask (tmpIndexName , moveOperationResponse .getTaskID (), requestOptions );
6888+ copyOperationResponse = operationIndex (
6889+ indexName ,
6890+ new OperationIndexParams ()
6891+ .setOperation (OperationType .COPY )
6892+ .setDestination (tmpIndexName )
6893+ .addScope (ScopeType .SETTINGS )
6894+ .addScope (ScopeType .RULES )
6895+ .addScope (ScopeType .SYNONYMS ),
6896+ requestOptions
6897+ );
6898+ waitForTask (tmpIndexName , copyOperationResponse .getTaskID (), requestOptions );
6899+
6900+ // Move temporary index to source index
6901+ UpdatedAtResponse moveOperationResponse = operationIndex (
6902+ tmpIndexName ,
6903+ new OperationIndexParams ().setOperation (OperationType .MOVE ).setDestination (indexName ),
6904+ requestOptions
6905+ );
6906+ waitForTask (tmpIndexName , moveOperationResponse .getTaskID (), requestOptions );
6907+
6908+ return new ReplaceAllObjectsResponse ()
6909+ .setCopyOperationResponse (copyOperationResponse )
6910+ .setBatchResponses (batchResponses )
6911+ .setMoveOperationResponse (moveOperationResponse );
6912+ } catch (Exception e ) {
6913+ deleteIndex (tmpIndexName );
68866914
6887- return new ReplaceAllObjectsResponse ()
6888- .setCopyOperationResponse (copyOperationResponse )
6889- .setBatchResponses (batchResponses )
6890- .setMoveOperationResponse (moveOperationResponse );
6915+ throw e ;
6916+ }
68916917 }
68926918
68936919 /**
0 commit comments