Api clients / JavaScript / V3 / Methods

Custom Batch | JavaScript API Client V3 (Deprecated)

Deprecated content
This documentation is for a deprecated version of JavaScript API client. Some features and settings may be missing or their usage may have changed. Refer to the documentation for the latest version of JavaScript API client for up-to-date information.
Required API Key: any key with the depends on operations performed inside the batch ACL
Method signature
client.batch(array operations, callback)

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 add or delete objects, potentially targeting multiple indices.

You would 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. Meaning that instead of deleting 30 objects then adding 20 new objects in two operations, we do both operations in one go. This will remove 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 our 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

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`](/doc/api-client/getting-started/request-options/) 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 is not created if the object designated by objectID does not 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

In this section we document the JSON response returned by the API. Each language will encapsulate this response inside objects specific to the language and/or the implementation. So the actual type in your language might differ from what is documented.

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?