🎉 Try the public beta of the new docs site at algolia.com/doc-beta! 🎉
Framework integration / Symfony / Indexing

Create algolia_search.yaml

Configuration typically lives in the config/packages/algolia_search.yaml file for a Symfony 4 application.

This is how you define what entity you want to index and some other technical details like a prefix or the number of results.

The documentation uses the Symfony/demo app as an example; we are working with posts and comments.

The simplest version

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

    - name: comments
      class: App\Entity\Comment

A more complete example

1
2
3
4
5
6
7
8
9
10
11
algolia_search:
  nbResults: 8                  # Retrieve less results on search (default: 20)
  prefix: %env(SEARCH_PREFIX)%  # Use a prefix for index names based en env var
  doctrineSubscribedEvents: []  # disable doctrine events (turn off realtime sync)
  indices:
    - name: posts
      class: App\Entity\Post
      enable_serializer_groups: true

    - name: comments
      class: App\Entity\Comment

Multi-environment setup

Usually, you need different configurations per environment, so that you don’t work with production data while developing.

Prefix

The first thing to do is to set a prefix per environment. You can do this by either creating an extra configuration file for your development environment, or rely on environment variables.

Configuration file

Create a config file inside the dev/ directory and override your default configuration.

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

Environment variables

In your config 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 Apache/Nginx configuration. Symfony makes it easy to concatenate environment variables in the .env file.

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

1
SEARCH_PREFIX=app1_${APP_ENV}_

Bypass calls to Algolia

While developing your application, you might want to prevent calls to Algolia being made. You can do this in the following ways:

  1. You can unsubscribe from Doctrine events to avoid indexing calls when you update your data.
  2. You can extend the SearchServiceInterface and use that new class, in your tests for example. You have the NullSearchService class at your disposal if you want a starting point. You will have to override the service in your configuration.
1
2
3
4
5
6
7
8
9
10
11
12
namespace App\Tests\Service;

use Algolia\SearchBundle\SearchService;

/**
 * Class NullSearchService.
 */
final class NullSearchService implements SearchServiceInterface
{
  //...
}

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

Index settings

You can use JSON files, located at config/settings/algolia_search/{$index_name}-settings.json, to handle index settings. The provided 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, as defined in configuration). If no options are passed all indices will be processed.

Settings directory

Depending on your version of Symfony, the settings will be saved in different locations:

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

The settings directory can also be set in the configuration if you have a non-standard setup or if you wish to save them elsewhere. The project directory will automatically be prepended.

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