Indexing

Prerequisite

Once you configured what entities you want to index in Algolia, you are ready to send data.

In the following section, we consider this Post entity and the following configuration.

algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
    - name: comments
      class: App\Entity\Comment

Indexing manually

Via CLI

Once your indices config is ready, you can use the built-in console command to batch import all existing data.

# Import all indices
php bin/console search:import

# Choose what indices to reindex by passing the index name
php bin/console search:import --indices=posts,comments

Before re-indexing everything, you may want to clear the index first, see how to remove data.

Programmatically

To index any entities in your code, you will need to use the IndexManager service. You need to pass it the objects to index and their ObjectManager. Objects can be a single entity, an array of entities or even an array of different entities as long as they are using the same ObjectManager.

<?php
$indexManager->index($post, $entityManager);

$indexManager->index($posts, $entityManager);

$indexManager->index($postsAndComments, $entityManager);

Removing manually

Via CLI

You may want to completely clear your indices (before reindexing for example), you can use the search:clear command.

# Import all indices
php bin/console search:clear

# Choose what indices to reindex by passing the index name
php bin/console search:clear --indices=posts,comments

Programmatically

The same way you index data, you can use the remove method to delete entries from the Algolia index.

<?php
$indexManager->remove($post, $entityManager);

$indexManager->remove($posts, $entityManager);

$indexManager->remove($postsAndComments, $entityManager);

Indexing automatically via Doctrine Events

By default, the bundle listens to the following Doctrine events: postPersist, postUpdate, preRemove. Every time data are inserted, updated or deleted via Doctrine, your Algolia index will stay in sync.

You can easily modify which events the bundle subscribes to via the doctrineSubscribedEvents config key.

You can unsubscribe from all events by passing an empty array. This can become very handy if you are working with a queue (like RabbitMQ) or if you don’t want to call Algolia in your dev environment.

# Only insert new data (no update, no deletion)
algolia_search:
  doctrineSubscribedEvents: ['postPersist']

# Unsubscribe from all events
algolia_search:
  doctrineSubscribedEvents: []

Indexing conditionally

Most of the time, there are some of your items that you don’t want to index. For instance, you may want to only index a post if it’s published.

In your configuration, you can specify when a post should be indexed via the index_if key. Because we rely on the PropertyAccess component you can pass a method name, a class property name or even a nested key in an property array.

The property must evaluate to true to index the entity and false to bypass indexing. If you’re updating an entity via doctrine and this property evaluates to false, the entity will be removed.

Example with a method or a property

algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
      index_if: isPublished

In this case, isPublished could be a method or a class property.

With a method:

<?php
class Post
{
    public function isPublished()
    {
        return !is_null($this->published_at);
    }
}

With a property:

<?php
class Post
{
    public $isPublished = true;
}

Example with an array

algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
      index_if: config.indexable

In this case, the bundle will read this value.

<?php
class Post
{
    public $config = ['indexable' => false];
}