Framework integration / Symfony / Indexing

Create algolia_search.yaml

The configuration for Algolia is in the file config/packages/algolia_search.yaml for Symfony 4 apps. Here, you can define what to index, and change other settings, such as, the number of results to retrieve with every search.

This guide uses the Symfony demo app as an example, using posts and comments.

Configuration examples

The following example lets Algolia store your posts in the index posts and your comments in the index comments. The other settings use their default values.

1
2
3
4
5
6
7
algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post

    - name: comments
      class: App\Entity\Comment

The following example lets Algolia index your posts and comments. The index names are combined from a prefix and the names posts or comments. The prefix is read from the environment variable SEARCH_PREFIX. This configuration example changes a few settings, such as reduce the number of retrieved results.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
algolia_search:
  # Retrieve fewer results (default: 20)
  nbResults: 8
  # Use a prefix for index names based on envinronment variables
  prefix: '%env(SEARCH_PREFIX)%'
  # Turn off real-time sync
  doctrineSubscribedEvents: []
  indices:
    - name: posts
      class: App\Entity\Post
      enable_serializer_groups: true

    - name: comments
      class: App\Entity\Comment

Multi-environment setup

To avoid working with production data while developing your app, use different configurations for each environment.

Prefix

To use different indices for your production and development environments, set up different index name prefixes, either in configuration files, or environment variables.

Set up different prefixes in configuration files

Create a configuration file inside the dev/ directory and override your default configuration. The configuration for your production environment uses the app_prod_ prefix:

1
2
3
# config/packages/algolia_search.yaml
algolia_search:
  prefix: app_prod_

The configuration for your development environment would store the indices with the app_dev_ prefix:

1
2
3
# config/packages/dev/algolia_search.yaml
algolia_search:
  prefix: app_dev_

Set up different prefixes in environment variables

In your configuration file, set the prefix as environment variable.

1
2
algolia_search:
  prefix: %env(SEARCH_PREFIX)%

Then, define the SEARCH_PREFIX variable in your .env file or your server configuration.

The following example uses the APP_ENV environment variable to create a search prefix:

1
SEARCH_PREFIX=app1_${APP_ENV}_

Bypass calls to Algolia

To prevent requests to Algolia while developing your app, you can use one of the following approaches:

  • Unsubscribe from Doctrine events to avoid indexing calls when you update your data.
  • Extend the SearchServiceInterface and use that new class, for example in your tests. As a starting point, you can use the NullSearchService class:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    
    namespace App\Tests\Service;
    
    use Algolia\SearchBundle\SearchService;
    
    /**
    * Class NullSearchService.
    */
    final class NullSearchService implements SearchServiceInterface
    {
      //...
    }
    

You need to override the service in your configuration:

1
2
3
# config/packages/test/algolia_search.yaml
search.service:
  class: Algolia\SearchBundle\Services\NullSearchService

Index settings

To configure index settings, you can use the JSON files in config/settings/algolia_search/{$index_name}-settings.json. The SettingsManager class backs up settings from the engine and pushes them back with the following commands:

1
2
php bin/console search:settings:backup --indices=posts,comments
php bin/console search:settings:push --indices=posts,comments

The --indices option takes a comma-separated list of index names (without prefix). Without options, all indices are processed.

Settings directory

Depending on your version of Symfony, the settings are in different locations:

  • Symfony 4: config/settings/algolia_search/
  • Symfony 3: app/Resources/SearchBundle/settings/

You can change the settings directory in your configuration file:

1
2
algolia_search:
  settingsDirectory: app/search-settings/
Did you find this page helpful?