Tracking each record with a unique identifier
To perform incremental updates, you need to declare a unique identifier for each record so you can track what data matches what record. This identifier should map to a “key” you store on your side. For instance, if you’re selling books and you have one Algolia record per book, you could use the ISBN. You need to store the unique identifier in theobjectID
attribute so you can leverage it to perform updates or deletions.
Adding records
Whenever you add new data in your data source, you can add them to Algolia with a regular call tosaveObjects
.
Note that every record has a custom
objectID
.Updating records
There are two ways to incrementally update records in Algolia:- Fully replacing the old record with a new one
- Updating only a subset of the existing record with the changed data
Replacing the old record
When saving a record in Algolia, if a record with the specifiedobjectID
already exists in the index, the engine replaces it. You can replace an existing record by using the method and specifying the objectID
. This technique is useful when you’re updating data in a single place in your system, or when you don’t know what changed.
Note that every record contains the
objectID
and you can use it to find the record that you want to update.Updating a subset of the record
Sometimes, you may want to update a subset of the attributes of a record and leave the rest untouched. This is useful when you update data in multiple places. With the books example, you may have two different systems to handle metadata and to manage stock, both updating the index, with no knowledge about the rest of the data. For this, you can use thepartialUpdateObjects
method and only pass the changed data. Anything you don’t specify remains untouched.
Every record has a unique
objectID
. You can use it to find the record that you want to update.Deleting records
Whenever you delete data in your data source, you can delete the corresponding record from Algolia withdeleteObjects
.
Every record has a unique
objectID
. You can use it to find the record that you want to delete.Delete by query
Sometimes, you may need to delete all records matching a certain filter. Back to the book example, if you stop selling books from a specific publisher, you might want to delete all records matching thispublisher
in your index. To do this, you can use the deleteBy
method.
The
deleteBy
method is an expensive operation for the engine. For better performance, use the deleteObjects
method instead.Any attribute you’re using to delete by needs to be in your
searchableAttributes
.