In this tutorial, we’ll review how you can keep your data in sync with Algolia by updating incrementally your records.

Putting an objectID in each records

In order to perform incremental updates after the initial indexing you will need declare a unique ID for each record. This ID should map to a “key” that you store on your side (for example, a SKU or PID).

The unique ID needs to be stored in the objectID attribute and can be leveraged to perform updates or deletions. If you don’t specify a custom objectID on creation, Algolia will generate it for you.

Initializing the client and index

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/algolia/folder/autoload.php';

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

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

Add records

Objects can be added using the following method:

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

Notice that an objectID is specified for each record.

Additional methods for importing data

There are many ways to push data to Algolia, the full extent of which bare covered in our importing with the API tutorial.

Updating records

There are two ways to update a record in Algolia.

By replacing the old record

To replace the content of an object that is already indexed in Algolia, you will need to save the new version of the object:

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'
    ]
  ]
);

Notice that the objectID in the new version of the object matches the initial objectID.

By updating only a subset of the record

In some cases you may only want to update a subset of the attributes of a record. To accomplish this, you will need to use the Partial update objects method:

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

Notice that the objectID in the new version of the object matches the initial objectID.

Deleting records

To delete an object, you will need to delete the object using its objectID:

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

Notice that the objectID in the new version of the object matches the initial objectID.

Delete by query

In some cases you may need to delete all the records that match a certain search query. To accomplish this, you will need to leverage the Delete by method. Don’t forget that any attributes you’re querying to delete by will need to be included in your searchable attributes settings.

Did you find this page helpful?