This is documentation for v3 of the PHP API clients, which is not the latest version.
To see the documentation for the latest version, see
PHP v4.
This is documentation for v2 of the Ruby API clients, which is not the latest version.
To see the documentation for the latest version, see
Ruby v3.
This is documentation for v4 of the JavaScript API clients, which is not the latest version.
To see the documentation for the latest version, see
JavaScript v5.
This is documentation for v3 of the Python API clients, which is not the latest version.
To see the documentation for the latest version, see
Python v4.
This is documentation for v8 of the Swift API clients, which is not the latest version.
To see the documentation for the latest version, see
Swift v9.
This is documentation for v2 of the Kotlin API clients, which is not the latest version.
To see the documentation for the latest version, see
Kotlin v3.
This is documentation for v6 of the C# API clients, which is not the latest version.
To see the documentation for the latest version, see
C# v7.
This is documentation for v3 of the Java API clients, which is not the latest version.
To see the documentation for the latest version, see
Java v4.
This is documentation for v3 of the Go API clients, which is not the latest version.
To see the documentation for the latest version, see
Go v4.
This is documentation for v1 of the Scala API clients, which is not the latest version.
To see the documentation for the latest version, see
Scala v2.
Required API Key:
any key with the
depends on operations performed inside the batch
ACL
About this method
Perform several indexing operations in one API call.
This method enables you to batch multiple different indexing operations in one API call,
like adding or deleting records,
potentially targeting multiple indices.
Use this method to:
Reduce latency - only one network trip is required for multiple operations
Ensure data integrity - all operations inside the batch will be 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
$res = $client -> multipleBatch (
[
[
'action' => 'addObject' ,
'indexName' => 'index1' ,
'body' => [
'firstname' => 'Jimmie' ,
'lastname' => 'Barninger'
]
],
[
'action' => 'updateObject' ,
'indexName' => 'index1' ,
'body' => [
'objectID' => 'myID2' ,
'firstname' => 'Max' ,
'lastname' => 'Barninger'
]
],
[
'action' => 'partialUpdateObject' ,
'indexName' => 'index1' ,
'body' => [
'objectID' => 'myID3' ,
'lastname' => 'McFarway'
]
],
[
'action' => 'partialUpdateObjectNoCreate' ,
'indexName' => 'index1' ,
'body' => [
'objectID' => 'myID4' ,
'firstname' => 'Warren'
]
],
[
'action' => 'deleteObject' ,
'indexName' => 'index2' ,
'body' => [
'objectID' => 'myID5'
]
]
]
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
res = client . batch ([
{
'action' : 'addObject' ,
'indexName' : 'index1' ,
'body' : {
'firstname' : 'Jimmie' ,
'lastname' : 'Barninger'
}
},
{
'action' : 'updateObject' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID2' ,
'firstname' : 'Max' ,
'lastname' : 'Barninger'
}
}
{
'action' : 'partialUpdateObject' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID3' ,
'lastname' : 'McFarway'
}
}
{
'action' : 'partialUpdateObjectNoCreate' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID4' ,
'firstname' : 'Warren'
}
}
{
'action' : 'deleteObject' ,
'indexName' : 'index2' ,
'body' : {
'objectID' : 'myID5'
}
}
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
client . multipleBatch ([
{
action : ' addObject ' ,
indexName : ' index1 ' ,
body : {
firstname : ' Jimmie ' ,
lastname : ' Barninger '
}
},
{
action : ' updateObject ' ,
indexName : ' index1 ' ,
body : {
objectID : ' myID2 ' ,
firstname : ' Max ' ,
lastname : ' Barninger '
}
},
{
action : ' partialUpdateObject ' ,
indexName : ' index1 ' ,
body : {
objectID : ' myID3 ' ,
lastname : ' McFarway '
}
},
{
action : ' partialUpdateObjectNoCreate ' ,
indexName : ' index1 ' ,
body : {
objectID : ' myID4 ' ,
firstname : ' Warren '
}
},
{
action : ' deleteObject ' ,
indexName : ' index2 ' ,
body : {
objectID : ' myID5 '
}
}
]). then (({ objectIDs }) => {
console . log ( objectIDs );
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
client . multiple_batch ([
{
'action' : 'addObject' ,
'indexName' : 'index1' ,
'body' : {
'firstname' : 'Jimmie' ,
'lastname' : 'Barninger'
}
},
{
'action' : 'updateObject' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID2' ,
'firstname' : 'Max' ,
'lastname' : 'Barninger'
}
},
{
'action' : 'partialUpdateObject' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID3' ,
'lastname' : 'McFarway'
}
},
{
'action' : 'partialUpdateObjectNoCreate' ,
'indexName' : 'index1' ,
'body' : {
'objectID' : 'myID4' ,
'firstname' : 'Warren'
}
},
{
'action' : 'deleteObject' ,
'indexName' : 'index2' ,
'body' : {
'objectID' : 'myID5'
}
}
])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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" ))),
( "index1" , . update ( objectID : "myID2" , Contact ( objectID : "myID2" , firstname : "Max" , lastname : "Barninger" ))),
( "index1" , . partialUpdate ( objectID : "myID3" , [ "lastname" : "McFarway" ] as JSON , createIfNotExists : true )),
( "index1" , . partialUpdate ( objectID : "myID4" , [ "firstname" : "Warren" ] as JSON , createIfNotExists : false )),
( "index2" , . delete ( objectID : "myID5" ))
]
client . multipleBatchObjects ( operations : operations ) { result in
if case . success ( let response ) = result {
print ( "Response: \( response ) " )
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
@Serializable
data class Contact (
val firstname : String ,
val lastname : String ,
override val objectID : ObjectID
) : Indexable
val operations = listOf (
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . AddObject (
json {
"firstname" to "Jimmie"
"lastname" to "Barninger"
}
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . AddObject . from (
serializer = Contact . serializer (),
data = Contact ( "Jimmie" , "Barninger" , ObjectID ( "myID1" ))
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
serializer = Contact . serializer (),
data = Contact ( "Max" , "Barninger" , ObjectID ( "myID2" ))
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
objectID = ObjectID ( "myID3" ),
partial = Partial . Update ( Attribute ( "firstname" ), "McFarway" )
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
objectID = ObjectID ( "myID4" ),
partial = Partial . Update ( Attribute ( "firstname" ), "Warren" ),
createIfNotExists = false
)
),
BatchOperationIndex (
indexName = indexName2 ,
operation = BatchOperation . DeleteObject ( ObjectID ( "myID5" ))
)
)
client . multipleBatchObjects ( operations )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 );
// Asynchronous
client . MultipleBatchAsync ( operations );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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 version
client . multipleBatchAsync ( operations );
// Async version
client . multipleBatchAsync ( operations );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
type Contact struct {
ObjectID string `json:"objectID,omitempty"`
Firstname string `json:"firstname,omitempty"`
Lastname string `json:"lastname,omitempty"`
}
operations := [] search . BatchOperationIndexed {
{
IndexName : "index1" ,
BatchOperation : search . BatchOperation {
Action : search . AddObject ,
Body : Contact { Firstname : "Jimmie" , Lastname : "Barninger" },
},
},
{
IndexName : "index1" ,
BatchOperation : search . BatchOperation {
Action : search . UpdateObject ,
Body : Contact { ObjectID : "myID2" , Firstname : "Max" , Lastname : "Barninger" },
},
},
{
IndexName : "index1" ,
BatchOperation : search . BatchOperation {
Action : search . PartialUpdateObject ,
Body : Contact { ObjectID : "myID3" , Lastname : "McFarway" },
},
},
{
IndexName : "index1" ,
BatchOperation : search . BatchOperation {
Action : search . PartialUpdateObjectNoCreate ,
Body : Contact { ObjectID : "myID4" , Firstname : "Warren" },
},
},
{
IndexName : "index2" ,
BatchOperation : search . BatchOperation {
Action : search . DeleteObject ,
Body : Contact { ObjectID : "myID5" },
},
},
}
res , err := client . MultipleBatch ( operations )
1
2
3
4
5
6
7
8
9
client . execute {
batch (
index into "index1" `object` Contact ( "" , "Jimmie" , "Barninger" ),
index into "index1" `object` ContactWithObjectID ( "myID2" , "Max" , "Barninger" ),
partialUpdate from "index1" `object` ContactWithObjectID ( "myID3" , "" , "McFarway" ),
partialUpdate from "index1" `object` ContactWithObjectID ( "myID4" , "Warren" , "" ) createIfNotExists false ,
delete from "index2" objectId "myID5"
)
}
Batch write operations and send extra HTTP headers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$operations = [
[
'action' => 'addObject' ,
'indexName' => 'index1' ,
'body' => [
'firstname' => 'Jimmie' ,
'lastname' => 'Barninger'
]
],
[
'action' => 'addObject' ,
'indexName' => 'index2' ,
'body' => [
'firstname' => 'Warren' ,
'lastname' => 'Speach'
]
]
];
$res = $client -> multipleBatch ( $operations , [
'X-Forwarded-For' => '94.228.178.246'
]);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
operations = [
{
'action' : 'addObject' ,
'indexName' : 'index1' ,
'body' : { 'firstname' : 'Jimmie' , 'lastname' : 'Barninger' }
},
{
'action' : 'addObject' ,
'indexName' : 'index2' ,
'body' : { 'firstname' : 'Warren' , 'lastname' : 'Speach' }
}
]
res = client . batch ( operations , {
headers: {
'X-Forwarded-For' : '94.228.178.246'
}
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
client . multipleBatch ([
{
action : ' addObject ' ,
indexName : ' index1 ' ,
body : { firstname : ' Jimmie ' , lastname : ' Barninger ' }
},
{
action : ' addObject ' ,
indexName : ' index2 ' ,
body : { firstname : ' Warren ' , lastname : ' Speach ' }
}
], {
headers : {
' X-Forwarded-For ' : ' 94.228.178.246 '
}
}). then (({ objectIDs }) => {
console . log ( objectIDs );
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
operations = [
{
'action' : 'addObject' ,
'indexName' : 'index1' ,
'body' : { 'firstname' : 'Jimmie' , 'lastname' : 'Barninger' }
},
{
'action' : 'addObject' ,
'indexName' : 'index2' ,
'body' : { 'firstname' : 'Warren' , 'lastname' : 'Speach' }
}
]
res = client . multiple_batch ( operations , {
'X-Forwarded-For' : '94.228.178.246'
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 ) " )
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
@Serializable
data class Contact (
val firstname : String ,
val lastname : String ,
override val objectID : ObjectID
) : Indexable
val operations = listOf (
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . AddObject (
json {
"firstname" to "Jimmie"
"lastname" to "Barninger"
}
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . AddObject . from (
serializer = Contact . serializer (),
data = Contact ( "Jimmie" , "Barninger" , ObjectID ( "myID1" ))
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
serializer = Contact . serializer (),
data = Contact ( "Max" , "Barninger" , ObjectID ( "myID2" ))
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
objectID = ObjectID ( "myID3" ),
partial = Partial . Update ( Attribute ( "firstname" ), "McFarway" )
)
),
BatchOperationIndex (
indexName = indexName1 ,
operation = BatchOperation . PartialUpdateObject . from (
objectID = ObjectID ( "myID4" ),
partial = Partial . Update ( Attribute ( "firstname" ), "Warren" ),
createIfNotExists = false
)
),
BatchOperationIndex (
indexName = indexName2 ,
operation = BatchOperation . DeleteObject ( ObjectID ( "myID5" ))
)
)
val requestOptions = requestOptions {
headerAlgoliaUserId ( UserID ( "user123" ))
}
client . multipleBatchObjects ( operations , requestOptions )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
RequestOptions requestOptions = new RequestOptions
{
Headers = new Dictionary < string , string >{ { "X-Algolia-User-ID" , "user123" } }
};
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 . AddObject ,
IndexName = "index1" ,
Body = new Contact { FirstName = "Warren" , LastName = "Speach" }
}
};
client . MultipleBatch ( operations , requestOptions );
// Asynchronous
client . MultipleBatchAsync ( operations , requestOptions );
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 version
client . multipleBatchAsync ( operations ,
new RequestOptions (). addExtraHeader ( "X-Algolia-User-ID" , "user123" ));
// Async version
client . multipleBatchAsync ( operations ,
new RequestOptions (). addExtraHeader ( "X-Algolia-User-ID" , "user123" ));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
contact := Contact {
Firstname : "Jimmie" ,
Lastname : "Barninger" ,
}
operation := search . BatchOperation { Action : search . AddObject , Body : contact }
operations := [] search . BatchOperationIndexed {
{ IndexName : "prodIndex" , BatchOperation : operation },
{ IndexName : "devIndex" , BatchOperation : operation },
}
res , err := client . MultipleBatch (
operations ,
opt . ExtraHeaders ( map [ string ] string { "X-Algolia-User-ID" : "userID2" }),
)
1
2
3
4
5
6
7
8
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" ))
)
}
Parameters
operations
List of operation .
Each operation is described by:
action : type of operation
indexName : name of the index targeted by this operation
body : arguments to the operation (depends on the type of the operation)
requestOptions
type: key-value mapping
default: No requestOptions
Optional
A mapping of request options to send along with the query.
operations âž”
operation
action
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.
indexName
Index name to target.
body
The JSON object containing the information you need for
the selected action.
For example, deleteObject
would be:
An object with the following format
1
2
3
{
"objectID": "myID1"
}
Response
This section shows the JSON response returned by the API.
Each API client encapsulates this response inside objects specific to the programming language,
so that the actual response might be different.
You can view the response by using the getLogs
method.
Don’t rely on the order of attributes in the response, as JSON doesn’t guarantee the ordering of keys in objects.
1
2
3
4
5
6
7
8
9
{
"objectIDs" : [
"myObjectID1" ,
"myObjectID2"
],
"taskID" : {
"index1" : 29866710291
}
}
Field
Description
objectIDs
List of objectIDs affected by the batch of operations.
taskID
A list of taskIDs to use with the waitTask method.
One for each index.
Did you find this page helpful?
© Algolia · Privacy Policy · Cookie settings