Guides / Sending and managing data / Send and update your data

Sending Records in Batches

Whether you’re using the API or Algolia dashboard, it’s best to send several records at a time instead of pushing them one by one. This has many benefits: it reduces network calls and speeds up indexing. Batching has a major impact on performance when you have a lot of records, but everyone should send indexing operations in batches whenever possible.

For example, imagine you’re fetching all data from your database and end up with a million records to index. That would be too big to send in one take, because Algolia limits you to 1 GB per request. Plus, sending that much data in a single network call would fail anyway before ever reaching the API. You might go for looping over each record and send them with the addObjects method. The problem is that you would perform a million individual network calls, which would take way too long and saturate your Algolia cluster with as many indexing jobs.

A leaner approach is to split your collection of records into smaller collections, then send each chunk one by one. For optimal indexing performance, aim for a batch size of ~10 MB, which represents between 1,000 or 10,000 records depending on the average record size.

Batching records doesn’t reduce your operations count. Algolia counts indexing operations per record, not per method call, so from a pricing perspective, batching records is no different from indexing records one by one.

Using the API

When using the saveObjects method, the API client automatically chunks your records into batches of 1,000 objects. If you’re using the legacy addObjects method, you need to chunk your records, then loop over each chunk and send it to Algolia.

If you need to send data from large files and handle concurrency in JavaScript, you can also use algolia-cli with the algolia import command.

1
2
3
4
5
6
7
$client = new \AlgoliaSearch\Client('YourApplicationID', 'YourAdminAPIKey');
$index = $client->initIndex('actors');

$records = json_decode(file_get_contents('actors.json'), true);

// Batching is done automatically by the API client
$index->saveObjects($records, ['autoGenerateObjectIDIfNotExist' => true]);

With this approach, you would make 100 API calls instead of 1,000,000. Depending on the size of your records and your network speed, you could create bigger or smaller chunks.

Using the dashboard

You can also send your records in your Algolia dashboard.

Add records manually

  • Go to your dashboard and select your index.
  • Click Manage current index then Add manually.
  • Copy/paste your chunk in the JSON editor, then click Push record.
  • Repeat for all your chunks.

Upload a file

  • Go to your dashboard and select your index.
  • Click Manage current index then Upload file.
  • Either click the file upload area to select the file where your chunk is, or drag it on it.
  • Upload starts automatically.
  • Repeat for all your chunks.

Did you find this page helpful?