> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Index configuration

> Configure your index to optimize the search experience with Algolia SearchBundle.

## Create configuration file `algolia_search.yaml`

The configuration for Algolia lives in `config/packages/algolia_search.yaml`.
Here, you can define what to index and change other settings,
such as the index name prefix and the batch size for automatic indexing.

This guide uses the [Symfony demo](https://github.com/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.

```yaml YAML icon=code theme={"system"}
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 also turns off real-time sync and opts posts into serializer groups.

```yaml YAML icon=code theme={"system"}
algolia_search:
  # Use a prefix for index names based on environment 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:

```yaml YAML icon=code theme={"system"}
# 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:

```yaml YAML icon=code theme={"system"}
# 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.

```yaml YAML icon=code theme={"system"}
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:

```dotenv .env icon=lock-keyhole theme={"system"}
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](/doc/framework-integration/symfony/indexing/index-data-into-algolia#automatic-indexing-with-doctrine-events) to avoid indexing calls when you update your data.
* Override `search.service` with the built-in `Algolia\SearchBundle\Services\NullSearchService` in your test environment:

```yaml YAML icon=code theme={"system"}
# config/services_test.yaml
services:
  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:

```sh Command line icon=square-terminal theme={"system"}
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

By default, the bundle looks for settings files under `config/settings/algolia_search/`.

You can change the settings directory in your configuration file. The value is appended directly to `kernel.project_dir`, so it must start with a leading slash:

```yaml YAML icon=code theme={"system"}
algolia_search:
  settingsDirectory: /search-settings
```
