🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
API client / Methods / Indexing
Required API Key: any key with the addObject ACL
Method signature
$index->partialUpdateObjects(array objects)
$index->partialUpdateObjects(array objects, [
  'createIfNotExists' => createIfNotExists
  // + any requestOptions
])

// update a single object
$index->partialUpdateObject(array object)
$index->partialUpdateObject(array object, [
  'createIfNotExists' => bool
  // + any 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

Update one or more attributes of an existing object.

This method lets you update only a part of an existing object by adding new attributes or updating existing ones. It requires an objectID:

  • If the objectID exists, Algolia replaces the attributes
  • If the objectID is specified but doesn’t exist, Algolia creates a new record
  • If the objectID isn’t specified, the method returns an error

You can partially update several objects in a single method call.

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

When updating many or large objects, be aware of the rate limit.

This method doesn’t replace the whole record** but adds, removes, or updates the attributes you pass. The remaining attributes remain untouched. This is different from saveObjects which replaces the whole record.

You can’t individually update nested attributes. Specifying a nested attribute treats it as a replacement of its first-level ancestor. To change nested attributes, you need to use the saveObjects method. You can retrieve the object’s data by using the getObjects method.

Built-in operations

To update an attribute without pushing the entire record, you can use these built-in operations. These operations can be helpful if you don’t have access to your initial data.

  • Increment: increment a numeric attribute
  • Decrement: decrement a numeric attribute
  • Add: append a number or string element to an array attribute
  • Remove: remove all matching number or string elements from an array attribute made of numbers or strings
  • AddUnique: add a number or string element to an array attribute made of numbers or strings only if it’s not already present
  • IncrementFrom: increment a numeric integer attribute only if the provided value matches the current value, and otherwise ignore the whole object update. For example, if you pass an IncrementFrom value of 2 for the version attribute, but the current value of the attribute is 1, the engine ignores the update. If the object doesn’t exist, the engine only creates it if you pass an IncrementFrom value of 0.
  • IncrementSet : increment a numeric integer attribute only if the provided value is greater than the current value, and otherwise ignore the whole object update. For example, if you pass an IncrementSet value of 2 for the version attribute, and the current value of the attribute is 1, the engine updates the object. If the object doesn’t exist yet, the engine only creates it if you pass an IncrementSet value that’s greater than 0.

For Remove and AddUnique: the operation will be silently ignored if the array attribute has any nested object or array.

You can specify an operation by providing an object with the attribute to update as the key and its value being an object with the following properties:

  • _operation: the operation to apply on the attribute
  • value: the right-hand side argument to the operation, for example, increment or decrement step, value to add or remove

Only the IncrementFrom and IncrementSet operations guarantee idempotent record updates. The other built-in operations aren’t idempotent and can cause unexpected side-effects, unless you use them in combination with the idempotent operations. For example, if you’re using the Increment or Decrement operations in a concurrent or multi-threaded environment, you may trigger it more than once and end up with wrong data in your records.

This method also has a singular version.

Examples

Read the Algolia CLI documentation for more information.

Partially update multiple objects using one API call

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

Partially update multiple objects using one API call and send extra HTTP headers

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

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

Update only the city attribute of an existing object

1
2
3
4
5
6
$index->partialUpdateObject(
  [
    'city'     => 'San Francisco',
    'objectID' => 'myID'
  ]
);

Add a new state attribute to an existing object

1
2
3
4
5
6
$index->partialUpdateObject(
  [
    'state'    => 'California',
    'objectID' => 'myID'
  ]
);

Increment an existing numeric attribute

This example updates the count attribute and increment its value.

1
2
3
4
5
6
7
8
9
$index->partialUpdateObject(
  [
    'count'    => [
      '_operation' => 'Increment',
      'value'      => 2
    ],
    'objectID' => 'myID'
  ]
);

Add a new value to an existing array attribute

This example updates the _tags attribute and adds an item to the list.

1
2
3
4
5
6
7
8
9
$index->partialUpdateObject(
  [
    '_tags'    => [
      '_operation' => 'AddUnique',
      'value'      => 'public'
    ],
    'objectID' => 'myID'
  ]
);

Update only if the value matches a numeric attribute

This example updates the version attribute using the IncrementFrom built-in operation. This ensures that this attribute is only incremented if the provided value matches.

1
2
3
4
5
6
7
8
9
$index->partialUpdateObject(
  [
    'version'    => [
      '_operation' => 'IncrementFrom',
      'value'      => 2
    ],
    'objectID' => 'myID'
  ]
);

Update only if the value is greater than a numeric attribute

This example updates the lastmodified attribute using the IncrementSet built-in operation to ensure that only the highest increment value is set.

1
2
3
4
5
6
7
8
9
$index->partialUpdateObject(
  [
    'lastmodified'    => [
      '_operation' => 'IncrementSet',
      'value'      => 1593431913
    ],
    'objectID' => 'myID'
  ]
);

Parameters

objects
type: list of object
Required

A list of objects to partially update.

objectID
type: integer
Required

objectID to update.

createIfNotExists
type: boolean
default: true (false for .NET, Java, Ruby, PHP, Python, and JavaScript)
Optional

If true, updating a nonexistent object will create a new object with the objectID and the attributes defined in the object. If false, attempting to update a nonexistent object is ignored (no error is generated).

In the Java, JavaScript, PHP, and .NET API clients, this setting defaults to false.

autoGenerateObjectIDIfNotExist
type: boolean
default: false
Optional

If true, the engine assigns an objectID to any object without objectID. If false, the method returns an error if any objects don’t contain an objectID.

In the Java, JavaScript, PHP, and .NET API clients, this setting defaults to false.

requestOptions
type: key-value mapping
default: ""
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 and createIfNotExists, when using this method.

objects âž” object

An object that matches some or all objects.

The object needs to contain an objectID. If you supply an unknown objectID:

  • If createIfNotExists is true, the method creates a new record with the supplied objectID and attributes.
  • If createIfNotExists is false, the method ignores the new record (without sending back an error).

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
{
  "objectIDs": [
    "myObjectID1",
    "myObjectID2"
  ],
  "taskID": 678,
}
objectIDs
list

List of objectIDs of the objects to be updated.

taskID
integer

The taskID used with the waitTask method.

Did you find this page helpful?