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

# Send data to Algolia

> Send data to Algolia with the Algolia SearchBundle.

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Index = () => <Tooltip tip="An Algolia index is a searchable dataset that consists of records and configuration settings. These settings define how the records are searched and ranked.">
    index
  </Tooltip>;

## Before you begin

After configuring what entities to index, you can send data to Algolia.

This guide uses [this Post entity](https://gist.github.com/julienbourdeau/3d17304951028cf370ed5fe95d104911) and the following configuration:

```yaml YAML icon=code theme={"system"}
algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
    - name: comments
      class: App\Entity\Comment
```

## Manual indexing

### With the CLI

Once your `indices` configuration is ready,
you can use the built-in console command to import all existing data:

```sh Command line icon=square-terminal theme={"system"}
# Import all indices
php bin/console search:import

# Choose what indices to reindex by passing the index name
php bin/console search:import --indices=posts,comments
```

Before reindexing everything, you may want to [clear the index](#manual-deletion).

#### Atomic reindexing

For a zero-downtime rebuild, pass the `--atomic` flag. The bundle creates a temporary index, copies your existing settings, synonyms, and rules to it, indexes all entities into it, and then atomically moves it over the source index:

```sh Command line icon=square-terminal theme={"system"}
php bin/console search:import --atomic --indices=posts
```

Use this flag when the application is serving live traffic and you need the existing records and settings to remain available until the new index is fully populated.

### Programmatically

To index any entities in your code,
use the [`SearchService`](/doc/framework-integration/symfony/getting-started/quick-start).
Pass it the `ObjectManager` associated with your records and the records to index.
Records can be a single entity,
an array of entities or even an array of different entities as long as they're using the same `ObjectManager`.

```php PHP icon=code theme={"system"}
$searchService->index($entityManager, $post);

$searchService->index($entityManager, $posts);

$searchService->index($entityManager, $postsAndComments);
```

You can pass a third optional argument to the `index` method,
the `$requestOptions` array or object.
With PHP client v4, HTTP-level options (headers, query parameters, body extras, timeouts) must be nested under their own key:

```php PHP icon=code theme={"system"}
$searchService->index($entityManager, $anyObject, [
    'headers' => [
        'X-Forwarded-For' => '0.0.0.0',
    ],
]);
```

The `index` method, along with all the methods used to manage your data and indices in the `SearchService`,
is *waitable*.
By chaining the function [`wait()`](/doc/libraries/sdk/methods/search/wait-for-task) to your operation,
you're saying explicitly that you want to wait for the engine to finish processing your task before moving on.

```php PHP icon=code theme={"system"}
$searchService->index($entityManager, $anyObject)->wait();
```

## Manual deletion

### With the CLI

To completely clear your indices (before reindexing for example), use the `search:clear` command.

```sh Command line icon=square-terminal theme={"system"}
# Clear all indices
php bin/console search:clear

# Choose what indices to clear by passing the index name
php bin/console search:clear --indices=posts,comments
```

### Programmatically

The same way you [index data](#manual-indexing),
you can use the `remove` method to delete entries from the Algolia <Index />.

```php PHP icon=code theme={"system"}
$searchService->remove($entityManager, $post);

$searchService->remove($entityManager, $posts);

$searchService->remove($entityManager, $postsAndComments);
```

Here as well, you can pass `$requestOptions`:

```php PHP icon=code theme={"system"}
$searchService->remove($entityManager, $anyObject, $requestOptions);
```

And wait for the engine to process your deletion completely:

```php PHP icon=code theme={"system"}
$searchService->remove($entityManager, $anyObject)->wait();
```

## Automatic indexing with Doctrine events

The bundle listens to three Doctrine events: `postPersist`, `postUpdate`, and `preRemove`.
Every time data is inserted, updated, or deleted using Doctrine, your Algolia index stays in sync.

To unsubscribe from all three events, set `doctrineSubscribedEvents` to an empty array.
This is useful when you index through a queue (such as RabbitMQ) or want to skip Algolia calls in development.

```yaml YAML icon=code theme={"system"}
# Unsubscribe from all Doctrine events
algolia_search:
  doctrineSubscribedEvents: []
```

<Note>
  The three events are subscribed together or not at all. Passing a subset (for example, `['postPersist']`) has no effect: the bundle still listens to all three.
</Note>

## Conditional indexing

Most of the time, there are some of your items that you don't want to index.
For instance, you may want to only index a post if it's published.

In your configuration, you can specify when a post should be indexed with the `index_if` key.
Because Algolia relies on the [PropertyAccess component](https://symfony.com/doc/current/components/property_access.html) you can pass a method name, a class property name, or a nested key in a property array.

The property must evaluate to true to index the entity and false to bypass indexing.
If you're updating an entity using Doctrine and this property evaluates to false, the entity will be removed.

**Example with a method or a property**

```yaml YAML icon=code theme={"system"}
algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
      index_if: isPublished
```

In this case, `isPublished` could be a method or a class property.

With a method:

```php PHP icon=code theme={"system"}
class Post
{
    public function isPublished()
    {
        return !is_null($this->published_at);
    }
}
```

With a property:

```php PHP icon=code theme={"system"}
class Post
{
    public $isPublished = true;
}
```

**Example with an array**

```yaml YAML icon=code theme={"system"}
algolia_search:
  indices:
    - name: posts
      class: App\Entity\Post
      index_if: config.indexable
```

In this case, the bundle will read this value.

```php PHP icon=code theme={"system"}
class Post
{
    public $config = ['indexable' => false];
}
```

## Connection errors

Are you getting "Impossible to connect", "Unable to connect", or "Unreachable hosts" errors?
First, make sure the issue isn't at your end:

* Ensure you're using the correct application ID and API key. Find these credentials on your [Algolia dashboard](https://dashboard.algolia.com/account/api-keys/all).
* Check for recent changes in your code.
* Check the status of your data center provider.

<Note>
  If you're using Firebase, you can only access Algolia from a paid Firebase tier.
</Note>

If you can't solve the problem yourself,
contact the [Algolia support](https://support.algolia.com/hc/en-us/requests/new) team and provide them with the following information:

* The name of your Symfony integration and its version number
* A code snippet to reproduce the issue
* Error message or stack trace (if applicable)
* The name of the Algolia index that's causing problems
* The exact [UTC time](https://wikipedia.org/wiki/Coordinated_Universal_Time) of the event
* If you can't connect to the Algolia API from your servers, send the output from the following command (run on any affected server):

  ```sh Command line icon=square-terminal theme={"system"}
  curl -sL https://algolia.com/downloads/diag.sh > ./diag.sh && sudo ./diag.sh ALGOLIA_APPLICATION_ID
  ```

  <Check>Replace `ALGOLIA_APPLICATION_ID` with *your* Algolia application ID.</Check>

## Indexing errors

Any `Record at the position XX objectID=XX is too big` errors during indexing are because you've exceeded the [size limit for records](https://support.algolia.com/hc/en-us/articles/4406981897617-Is-there-a-size-limit-for-my-index-records-).
Reduce the size of your <Records /> and try again.
