Icon indexing white

Synchronization

Last updated 15 November 2017

Overview

After the initial import of the data, you’ll need to keep your index up-to-date with the latest additions and changes on your application or website.

You can update your records one by one in realtime or batch your work. Records can be updated as a complete item or you can just modify only a subset of its attributes. Additionally, its possible to make changes via a full re-index without generating inconsistencies on your search during the process.

Incremental Updates

Once your initial data has been indexed, you will want to configure your application to sync your database changes such as additions, updates, and deletions with your indices in Algolia.

Save Objects

To save your new or updated records, you’ll need to use the Update objects method. When updating an existing object, you can choose to replace all its attributes, replace a subset of its attributes, or apply an operation to some of its attributes. In order to target the correct record to update, you will need to include the respective objectID.

var objects = [{
  firstname: 'Jimmie',
  lastname: 'Carter',
  objectID: 'myID1'
}, {
  firstname: 'Warren',
  lastname: 'Speach',
  objectID: 'myID2'
}];

index.saveObjects(objects, function(err, content) {
  console.log(content);
});

Partially Update Objects

Additionally, you can apply partial updates to objects using the Partial update objects method. You have the option to apply a variety of operations, including: setting an attribute value, adding or removing an element from an array, adding an element to an array if it doesn’t exist, or incrementing or decrementing an attribute.

In this example, we perform a partial update to solve a misspelling of Jimmy’s name.

index.partialUpdateObject({
  firstname: 'Jimmy',
  objectID: 'myID1'
}, function(err, content) {
  console.log(content);
});

Purging Records

In order to keep your data size as small as possible, we recommend purging records that are no longer necessary for search (for example, out of stock items).

Delete Objects by ObjectID

Records can be deleted by specifying their objectID in the Delete objects method.

index.deleteObjects(['myID1', 'myID2'], function(err, content) {
  console.log(content);
});

Delete Objects by Query

Alternatively, it’s possible to delete all the records that match a certain query. For example, we might want to delete all records from years prior to 2017.

index.deleteByQuery('', {
    filters: 'year < 2017'
  }, function(err) {
  if (!err) {
    console.log('success');
  }
});

Calling this method with an empty query and no parameters will result in the index being cleared.

Atomic Re-indexing

If you need to re-index without causing downtime for your search, you will need to perform an atomic reindex. To achieve this, you will need to create a new, temporary index, and copy over the settings and synonyms from the main index. Then, you will need to push the new set of data to this temporary index, after which you can leverage the Move index method.

More details and code examples can be found in our atomic re-indexing tutorial.

How Often to Send Updates

The appropriate frequency for sending Algolia updates depends on how often new data is added to your site and how quickly that data needs to be searchable.

For example, for an e-commerce shop:

  • You’ll want to update in real-time the price changes, or the availability of a product
  • You don’t necessarily need to update the number of sales that you use for the ranking, so you can send them by batches periodically every hour/day/week

You’ll need to find a balance between having the information in the search as fast as possible, and minimizing the number of operations (because it has an impact on the pricing / performance).

What’s Next

Continue building your Algolia knowledge with these concepts:

Here are a few tutorials you might be interested in:

© Algolia - Privacy Policy