🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Guides / Sending and managing data / Send and update your data

Once you’ve first indexed your data, you need to keep your index up to date as the data changes.

One way of doing so is via incremental updates, by tracking changes and forwarding them to Algolia. Whenever you add, edit, or delete data from your source, you want to update the corresponding records so they reflect the latest state of your data.

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 the objectID 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 to saveObjects.

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

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 specified objectID 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.

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

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 the partialUpdateObjects method and only pass the changed data. Anything you don’t specify remains untouched.

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

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 with deleteObjects.

1
$index->deleteObjects(["myID1", "myID2"]);

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 this publisher 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.

1
2
3
4
5
$index->deleteBy([
  'filters' => 'category:cars',
  'aroundLatLng' => '40.71, -74.01'
  /* add any filter parameters */
]);

Any attribute you’re using to delete by needs to be in your searchableAttributes.

Did you find this page helpful?