Getting Started

Introduction

This bundle provides an easy way to integrate Algolia Search into your Symfony application (using Doctrine). It allows you to index your data, keep it in sync, and search through it.

Compatibility

This documentation refers to the Algolia/SearchBundle 3.0 and later. It’s compatible with Symfony 3.4 LTS and Symfony 4.0 (and later).

If your app is running Symfony prior to Symfony 3.4, please use v2. You can find the documentation in the README. Version 2.x is not actively maintained but will receive updates if necessary.

Upgrade

To upgrade your project to the newest version of the bundle, please refer to the Upgrade Guide.

What’s new

v3 has introduced a number of great new features, like the use of Symfony Serializer; but the main reason behind this new version was to improve the developer experience.

  • Simple: You can get started with only 5 lines of YAML
  • Extensible: It lets you easily replace services by implementing Interfaces
  • Standard: It leverages Normalizers to convert entities for indexing
  • Dev-friendly: It lets you disable HTTP calls easily (while running tests, for example)
  • Future-ready: It lets you unsubscribe from doctrine events easily to use a messaging/queue system.

This bundle is Search Engine-agnostic. It means that you can use it with any other engine, not just Algolia.

Install

Require the dependency (with Composer)

composer require algolia/search-bundle

Register the bundle

The bundle should be registered automatically, otherwise follow this step.

With Symfony 4.x

Add Algolia to config/bundles.php:

<?php
return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    // ... Other bundles ...
    Algolia\SearchBundle\AlgoliaSearchBundle::class => ['all' => true],
];

With Symfony 3.4

Add Algolia to your app/Kernel.php:

<?php
$bundles = array(
    new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
    // ... Other bundles ...
    new Algolia\SearchBundle\AlgoliaSearchBundle(),
);

Algolia credentials

You will also need to provide the Algolia App ID and Admin API key. By default, they are loaded from environment variables ALGOLIA_APP_ID and ALGOLIA_API_KEY.

If you use .env config file, you can set them there.

ALGOLIA_APP_ID=XXXXXXXXXX
ALGOLIA_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

If you don’t use environment variables, you can set them in your parameters.yml.

parameters:
    env(ALGOLIA_APP_ID): XXXXXXXXXX
    env(ALGOLIA_API_KEY): xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Injecting services

Most of the time, you will be using the IndexManager object to either:

  • Check configuration
  • Index data
  • Search

Symfony 4

Symfony 4 ships with a lighter container where only some much-needed core services are registered. If your controller will be responsible for some search-related task, you need to inject it via the contructor. Good news, by simply type-hinting the variable, Symfony will handle everything for you, thanks to auto-wiring.

<?php
namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Algolia\SearchBundle\IndexManagerInterface;

class ExampleController extends Controller
{

    protected $indexManager;

    public function __construct(IndexManagerInterface $indexingManager)
    {
        $this->indexManager = $indexingManager;
    }
}

Notice that you type-hint an interface, not an actual implementation. This will be very handy if you ever need to implement your own IndexManager.

Symfony 3

Symfony 3 still uses a container holding all public services, and services are public by default. This way, you can easily get the search.index_manager from the container.

Although, it’s not considered a best practice by Symfony anymore, and in order to get ready for Symfony 4, I’d recommend using the previously shown method.

<?php
// In a Container-Aware class
$indexManager = $this->getContainer()->get('search.index_manager');

// In a Controller class
$indexManager = $this->get('search.index_manager');