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.

Required API Key: any key with the depends on operations performed inside the batch ACL

Method signature
client.Batch(
  []BatchOperationIndexed operations
)
client.Batch(
  []BatchOperationIndexed operations,
  RequestOptions  requestOptions
)

About this method # A

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 # A

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
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)

Batch write operations and send extra HTTP headers#

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"}),
)

Parameters # A

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 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 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 # A

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?
Go API clients v3