API Reference / API Methods / Indexing / Add objects
Required API Key: any key with the addObject ACL
Method signature
$index->saveObjects(array objects, [
  "autoGenerateObjectIDIfNotExist" => boolean
  // any other requestOptions
]

// add a single object
$index->saveObject(array object, [
  "autoGenerateObjectIDIfNotExist" => boolean
  // any other requestOptions
]

About this method

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

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

Add new objects to an index.

This method allows you to create records on your index by sending one or more objects. Each object contains a set of attributes and values, which represents a full record on an index.

If the index targeted by this operation doesn’t exist yet, it’s automatically created.

There is no limit to the number of objects that can be passed, but a size limit of 1 GB on the total request. For performance reasons, it’s recommended to push batches of ~10 MB of payload.

Batching records allows you to reduce the number of network calls required for multiple operations. But note that each indexed object counts as a single indexing operation.

When adding 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.

Note: This method also has a singular version.

Examples

Add objects with automatic objectID assignments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$res = $index->saveObjects(
  [
    [
      'firstname' => 'Jimmie',
      'lastname'  => 'Barninger'
    ],
    [
      'firstname' => 'Warren',
      'lastname'  => 'Speach'
    ]
  ],
  [
    'autoGenerateObjectIDIfNotExist' => true
  ]
);

Add objects with manual objectID assignments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$index->saveObjects(
  [
    [
      'objectID' => 'myID1',
      'firstname' => 'Jimmie',
      'lastname'  => 'Barninger'
    ],
    [
      'objectID' => 'myID2',
      'firstname' => 'Warren',
      'lastname'  => 'Speach'
    ]
  ]
);

Add a single object

1
2
3
4
5
6
7
$index->saveObject(
  [
    'objectID' => 'myID',
    'firstname' => 'Jimmie',
    'lastname'  => 'Barninger'
  ]
);

Add objects and send extra HTTP headers

1
2
3
4
5
6
$objects = [/* objects */];

$res = $index->saveObjects($objects, [
  'autoGenerateObjectIDIfNotExist' => true,
  'X-Forwarded-For' => '94.228.178.246'
]);

Parameters

objects
type: list of object
Required

A schemaless set of key/value pairs representing index attributes.

autoGenerateObjectIDIfNotExist
type: boolean
default: false
Optional

When false, if any of the objects doesn’t contain an objectID, the method throws an error. When true, if any of the objects doesn’t contain an objectID, the engine automatically assigns one.

requestOptions
type: key/value mapping
default: No request options
Optional

A mapping of requestOptions to send along with the query. In addition to sending extra HTTP headers or setting timeouts, you can use requestOptions to set autoGenerateObjectIDIfNotExist, when using this method.

objects âž” object

The object is schemaless, so it can be any set of key/value pairs, where each key is an attribute from your index.

Some attributes have special considerations:

The value you provide for objectIDs can be an integer or a string but will in any case be converted to string by the engine.

You can create the ID yourself or Algolia will generate one. Which means that you are not required to send an objectID.

  1. If you send an objectID:
    • If the objectID doesn’t already exist in the index, the record will be created
    • If the objectID already exists, the record will be replaced (same functionality as updating object)
  2. If you do not send an objectID:
    • Algolia will automatically create an objectID that you will be able to access in the response.

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

Add objects

1
2
3
4
5
6
7
{
  "objectIDs": [
    "myObjectID1",
    "myObjectID2"
  ],
  "taskID": 678,
}

Add object

1
2
3
4
{
  "objectID": "myObjectID1",
  "taskID": 678,
}
objectIDs
list

List of objectIDs of the saved objects in order. This property is only returned when using add objects.

objectID
string

The objectID of the saved object. This property is only returned when using add object.

taskID
integer

The taskID used with the waitTask method.

Did you find this page helpful?