Skip to content

Index Configuration

Tomas Norkūnas edited this page Mar 6, 2024 · 9 revisions

Create meilisearch.yaml

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

This is how you define what entity or document 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

meilisearch:
    url: '%env(MEILISEARCH_URL)%'          # URL of the Meilisearch server (mandatory)
    api_key: '%env(MEILISEARCH_API_KEY)%'  # API key to access the Meilisearch server (mandatory)
    indices:
        - name: posts
          class: App\Entity\Post

        - name: comments
          class: App\Entity\Comment

Then, define the MEILISEARCH_URL and MEILISEARCH_API_KEY as environment variables.

More about Meilisearch authentication and API keys.

A complete example

meilisearch:
    url: '%env(MEILISEARCH_URL)%'
    api_key: '%env(MEILISEARCH_API_KEY)%'
    nbResults: 8                  # Retrieve fewer results on search (default: 20)
    prefix: %env(SEARCH_PREFIX)%  # Use a prefix for index names based on 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 create 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.

# config/packages/meilisearch.yaml
meilisearch:
    ...
    prefix: app_prod_
    ...

when@dev:
    meilisearch:
        prefix: app_dev_

Environment Variables

In your config file, set the prefix as an environment variable.

meilisearch:
    ...
    prefix: %env(MEILISEARCH_PREFIX)%
    ...

Then, define the MEILISEARCH_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:

MEILISEARCH_PREFIX=app1_${APP_ENV}_