Configuration
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
Indexing data
First, we need to define which entities should be indexed in Algolia.
Each entry under the indices
config key must contain at least the 2 following attributes:
name
is the canonical name of the index in Algoliaclass
is the full name of the entity to index
Example:
1
2
3
4
algolia_search:
indices:
- name: posts
class: App\Entity\Post
enable_serializer_groups
Before sending your data to Algolia, each entity will be converted to an array
using the Symfony built-in serializer. This option lets you define what
attribute you want to index using the annotation @Groups({"searchable"})
.
Read more about how entities are serialized here.
Example:
1
2
3
4
5
algolia_search:
indices:
- name: posts
class: App\Entity\Post
enable_serializer_groups: true
Check out the indexing documentation to learn how to send data to Algolia.
Batching
By default, calls to algolia to index or remove data are batched per 500 items. You can easily modify the batch size in your configuration.
1
2
algolia_search:
batchSize: 250
The import command also follows this parameter to retrieve data via Doctrine. If you are running out of
memory while importing your data, use a smaller batchSize
value.
Using JMS Serializer
The bundle also provides basic support for the JMS Serializer. Note that not all features are supported (like the @Groups annotation).
In your config, pass the name of the JMS Serializer service (jms_serializer
by default).
1
2
3
4
5
algolia_search:
serializer: jms_serializer
indices:
- name: posts
class: App\Entity\Post
Per environment setup
Usually, you need different configurations per environment, at least to avoid touching prod data while developing.
Bypass calls to Algolia
While working locally you might want to bypass all calls to Algolia and this bundle has introduced new ways to do so.
- You can unsubscribe from Doctrine events to avoid calls on data updates.
- You can use the
NullEngine
to mute all calls.
Prefix
The first thing to do is to set a prefix per environment. There are 2 ways to do that: either you create 2 config files or you rely on environment variables.
Env variables
In your config file, you set the prefix in an environment variable.
1
2
algolia_search:
prefix: %env(SEARCH_PREFIX)%
Then you define your prefix in your .env
, or your Apache/Nginx configuration.
Symfony makes it easy to concatenate environment variables in the .env
file.
Assuming APP_ENV is an environment variable:
1
SEARCH_PREFIX=app1_${APP_ENV}_
Override configuration per environment
Or you can create a config file inside the dev
directory and override the config.
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_