API Reference / API Methods / Indexing / Custom batch
Required API Key: any key with the depends on operations performed inside the batch ACL
Method signature
$client->multipleBatch(array operations)
$client->multipleBatch(array operations, array requestOptions)

About this method

We released a new version of the PHP API client in public beta. Read the beta documentation for more information.

We released a new version of the JavaScript API client in public beta. Read the beta documentation for more information.

We released a new version of the Java API client in public beta. Read the beta documentation for more information.

You’re currently reading the JavaScript API client v4 documentation. Check the migration guide to learn how to upgrade from v3 to v4. You can still access the v3 documentation.

You’re currently reading the Ruby API client v2 documentation. Check the migration guide to learn how to upgrade from v1 to v2. You can still access the v1 documentation.

Perform several indexing operations in one API call.

This method enables you to batch multiple different indexing operations in one API call, like add or delete objects, potentially targeting multiple indices.

You can 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 objects then adding 20 new objects 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 doing an atomic reindexing using a temporary index.

When batching of a large numbers of objects, or large sizes, be aware of the rate limit. You’ll know you’ve reached the rate limit when you start receiving errors on your indexing operations. This can only be resolved if you wait before sending any further indexing operations.

Examples

Read the Algolia CLI documentation for more information.

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'
      ]
    ]
  ]
);

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'
]);

Parameters

operations
type: list of
Required

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 requestOptions to send along with the query.

operations âž” operation

action
type: string
Required

Actions that need to be performed. It can be one of the following values:

  • addObject: Add an object.
  • updateObject: Add or replace an existing object. You must set the objectID attribute to indicate the object to update.
  • partialUpdateObject: Partially update an object. You must set the objectID attribute to indicate the object to update.
  • partialUpdateObjectNoCreate: Same as partialUpdateObject, except that the object isn’t created if the object designated by objectID doesn’t exist.
  • deleteObject: Delete an object. You must set the objectID attribute to indicate the object to delete.
indexName
type: string
Required

Index name to target.

body
type: object
Required

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. Since each language encapsulates this response inside objects specific to that language and/or implementation, the actual type in your language might differ from what’s written here. You can view the response in the logs (using the getLogs method).

JSON format

1
2
3
4
5
6
7
8
9
{
  "objectIDs": [
    "myObjectID1",
    "myObjectID2"
  ],
  "taskID": {
    "index1": 29866710291
  }
}
objectIDs
list

List of objectIDs affected by the batch of operations.

taskID
list

A list of taskIDs to use with the waitTask method. One for each index.

Did you find this page helpful?