Integrations / Frameworks / Symfony / Extending
Feb. 20, 2019

Extending Engine and SettingsManager

One of the best thing about the v3 of the Algolia/SearchBundle is that you can extend it. It is open to unlimited possibilities.

There are 2 main reasons you might need to extend this package:

  • You have specific needs with Algolia
  • You want to use another search engine

To help you get started, we recommend using our skeleton project.

Create your own engine

Write new CustomEngine class

The first mandatory step to extend the bundle is to write your own Engine. It requires you to implement the EngineInterface.

If you need inspiration, the bundle ships with AlgoliaEngine and NullEngine. In the tests, you will also find an AlgoliaSyncEngine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// src/Engine/CustomEngine

namespace App\Engine;

use Algolia\SearchBundle\Engine\EngineInterface;

class CustomEngine implements EngineInterface
{

    public function add($searchableEntities)
    {
        // TODO: Implement add() method.
    }

    public function update($searchableEntities)
    {
        // TODO: Implement update() method.
    }

    public function remove($searchableEntities)
    {
        // TODO: Implement remove() method.
    }

    public function clear($indexName)
    {
        // TODO: Implement clear() method.
    }

    public function delete($indexName)
    {
        // TODO: Implement delete() method.
    }

    public function search($query, $indexName, $page = 0, $nbResults = null, array $parameters = [])
    {
        // TODO: Implement search() method.
    }

    public function searchIds($query, $indexName, $page = 0, $nbResults = null, array $parameters = [])
    {
        // TODO: Implement searchIds() method.
    }

    public function count($query, $indexName)
    {
        // TODO: Implement count() method.
    }
}

Override the service definition

The engine is injected in the IndexManager by changing the service definition of search.engine. It will use your brand new class.

1
2
3
search.engine:
    class: App\Engine\CustomEngine
    public: true

Create your own settings manager

You may need to change the way settings are handled. In this case, you can define your own search.settings_manager.

If you are using the console command, the $params argument is a list of index plus all other arguments passed to the command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// src/Engine/CustomEngine

namespace App\Engine;

use Algolia\SearchBundle\Settings\SettingsManagerInterface;

class CustomSettingsManager implements SettingsManagerInterface
{

    public function backup(array $params)
    {
        // TODO: Implement backup() method.
    }

    public function push(array $params)
    {
        // TODO: Implement push() method.
    }
}

### Override the service definition

The engine is injected in the `IndexManager` by changing the service definition
of `search.engine`. It will use your brand new class.


```yaml
search.settings_manager:
    class: App\Engine\CustomSettingsManager
    public: true

Did you find this page helpful?