Algolia doesn’t search directly into your own data source. For data to be searchable, you need to send (or push) it to Algolia’s servers.

This happens right after retrieving your data from your data source (a database, files, an API, etc.) and reformatting it (for cleaning up, applying logic to generate attributes that will be used for custom ranking, etc.). You need to complete this before sending the records to Algolia. If you need more guidance with this step, please read Preparing Your Data.

Once your data is ready, you can push it to Algolia using the addObjects method.

Required Credentials

To push data to Algolia, you need an Application ID and a valid API key. You can find them in your Algolia Dashboard in the API Keys section. These credentials let you connect to Algolia and perform operations on your data.

Application ID

Your Application ID (or App ID) is what Algolia uses to identify your app, where we keep all your indices.

API Key

The Admin API Key is the one you need to create, update, and delete records. This is the most sensitive key, as it provides full control of all your indices and data. Make sure to keep it secret and secure, do not release to anyone, and only use it in back-end applications. For added security, we recommend you don’t even use it directly to handle your indices, but generate more restrictive keys from it and use these keys instead. You can read more about this here.

Setup the API Client

1
2
3
4
5
6
7
8
9
10
11
12
// composer autoload
require __DIR__ . '/vendor/autoload.php';

// if you are not using composer
// require_once 'path/to/algoliasearch.php';

$client = \Algolia\AlgoliaSearch\SearchClient::create(
  'YourApplicationID',
  'YourAdminAPIKey'
);

$index = $client->initIndex('your_index_name');

Fetching your data

The first step is to retrieve your data. You can do this in several ways, depending on the nature of your application. Here are a few potential examples:

From a database

1
2
3
4
5
6
function fetchDataFromDatabase() {
  $actors = // Fetch data from your database
  return $actors;
}

$records = fetchDataFromDatabase();

From a file

You can use this actor dataset to test this out.

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

From the source code directly

You should only use this method only if you have a small amout of data.

1
2
3
4
$records = [
  ['name' => 'Tom Cruise'],
  ['name' => 'Scarlett Johansson']
];

Send the data to Algolia

Once you have your records ready, you can then push them to Algolia using the addObjects method.

1
$index->saveObjects($records, ['autoGenerateObjectIDIfNotExist' => true]);

Batching

For performance reasons, we recommend you send several records at once instead of one by one. If you have many records to index, we recommend you send them in batches.

Once you’re done, don’t forget to configure relevance settings.

Did you find this page helpful?