API client / Methods / Indexing
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)

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.

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

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

Parameter Description
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

Parameter Description
action
type: 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.
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. 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.

JSON format

1
2
3
4
5
6
7
8
9
{
  "objectIDs": [
    "myObjectID1",
    "myObjectID2"
  ],
  "taskID": {
    "index1": 29866710291
  }
}
Field Description
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?