Required ACL:depends on operations performed inside the batchThis method lets you batch multiple indexing operations in one API request,
such as adding or deleting records, potentially targeting multiple indices.Benefits:
Reduced latency. Only one network trip is required for multiple operations.
Data integrity. All operations inside the batch are executed atomically.
Instead of deleting 30 records then adding 20 new records in two operations,
batching lets you combine both tasks in a single operation.
This removes the time during which an index
is in an inconsistent state and could be a great alternative to atomic
reindexing using a temporary index.
When updating large numbers of records, be aware of the rate limitations on these processes and the impact on your analytics data.
You’ll know you’ve reached the rate limit when you start receiving errors.
This can only be resolved if you wait before sending any further indexing operations.
Examples
Batch write operations
List<BatchOperation<Contact>> operations = new List<BatchOperation<Contact>> { new BatchOperation<Contact> { Action = BatchActionType.AddObject, IndexName = "index1", Body = new Contact { FirstName = "Jimmie", LastName = "Barninger" } }, new BatchOperation<Contact> { Action = BatchActionType.UpdateObject, IndexName = "index1", Body = new Contact { ObjectID = "myID2", FirstName = "Max", LastName = "Barninger" } }, new BatchOperation<Contact> { Action = BatchActionType.PartialUpdateObject, IndexName = "index1", Body = new Contact { ObjectID = "myID3", LastName = "McFarway" } }, new BatchOperation<Contact> { Action = BatchActionType.PartialUpdateObjectNoCreate, IndexName = "index1", Body = new Contact { ObjectID = "myID4", LastName = "Warren" } }, new BatchOperation<Contact> { Action = BatchActionType.DeleteObject, IndexName = "index2", Body = new Contact { ObjectID = "myID5" } } };client.MultipleBatch(operations);// Asynchronousclient.MultipleBatchAsync(operations);
List<BatchOperation<Contact>> operations =Arrays.asList( new BatchOperation<>( "index1", ActionEnum.ADD_OBJECT, new Contact() .setFirstName("Jimmie") .setLastName("Barninger")), new BatchOperation<>( "index1", ActionEnum.UPDATE_OBJECT, new Contact() .setObjectID("myID2") .setFirstName("Max") .setLastName("Barninger")), new BatchOperation<>( "index1", ActionEnum.PARTIAL_UPDATE_OBJECT, new Contact() .setObjectID("myID3") .setLastName("McFarway")), new BatchOperation<>( "index1", ActionEnum.PARTIAL_UPDATE_OBJECT_NO_CREATE, new Contact() .setObjectID("myID4") .setFirstName("Warren")), new BatchOperation<>( "index2", ActionEnum.DELETE_OBJECT, new Contact() .setObjectID("myID5")));// Async versionclient.multipleBatchAsync(operations);// Async versionclient.multipleBatchAsync(operations);
List<BatchOperation<Contact>> operations =Arrays.asList( new BatchOperation<>( "index1", ActionEnum.ADD_OBJECT, new Contact() .setFirstName("Jimmie") .setLastName("Barninger")), new BatchOperation<>( "index2", ActionEnum.ADD_OBJECT, new Contact() .setFirstName("Warren") .setLastName("Speach")));// Async versionclient.multipleBatchAsync(operations, new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123"));// Async versionclient.multipleBatchAsync(operations, new RequestOptions().addExtraHeader("X-Algolia-User-ID", "user123"));
client.execute { batch( index into "index1" `object` Contact("Jimmie", "Barninger"), index into "index2" `object` Contact("Warren", "Speach") ) options RequestOptions(extraHeaders = Some( Map("X-Algolia-User-ID" => "user123")) )}
var requestOptions = RequestOptions()requestOptions.headers["X-Algolia-User-ID"] = "user123"struct Contact: Codable { let objectID: ObjectID let firstname: String let lastname: String}let operations: [(IndexName, BatchOperation)] = [ ("index1", .add(Contact(objectID: "myID1", firstname: "Jimmie", lastname: "Barninger"))), ("index2", .add(Contact(objectID: "myID2", firstname: "Warren", lastname: "Speach")))]client.multipleBatchObjects(operations: operations, requestOptions: requestOptions) { result in if case .success(let response) = result { print("Response: \(response)") }}
Parameters
operation[]
required
List of operations.
Show child attributes
string
required
Actions that need to be performed. It can be one of the following values:
addObject: add a record.
updateObject: add or replace an existing record. You must set the objectID attribute to indicate the record to update.
partialUpdateObject: partially update a record. You must set the objectID attribute to indicate the record to update.
partialUpdateObjectNoCreate: same as partialUpdateObject, except that the record isn’t created if the objectID doesn’t exist.
deleteObject: delete a record. You must set the objectID attribute to indicate the record to delete.
object
required
The JSON object with the information you need for
the selected action.For example, deleteObject:
{ "objectID": "myID1"}
string
required
Index name to target.
object
A mapping of request options to send along with the query.
Response
string[]
List of objectIDs affected by the batch of operations.
string[]
A list of task IDs to use with the waitTask method.
One for each index.
Response as JSON
This section shows the JSON response returned by the API.
Each API client wraps this response in language-specific objects, so the structure may vary.
To view the response, use the getLogs method.
Don’t rely on the order of properties—JSON objects don’t preserve key order.