Version 4 of the Algolia SearchBundle for Symfony is based on version 2 of the PHP API client.
Version 4 includes these architectural changes:
- Remove search engine-agnostic feature
- Merge
AlgoliaEngine
and IndexManager
classes into SearchService
class
- Updated signatures for the
SearchClient
class to reflect changes between versions 1 and 2 of the Algolia PHP API client.
For more information, see the PHP client upgrade guide.
To upgrade the Algolia SearchBundle in your app,
upgrade the algolia/search-bundle
dependency from ^3.4
to 4.0.0
in your composer.json
file and run:
1
| composer update algolia/search-bundle algolia/algoliasearch-client-php
|
With the version upgrade, you can benefit from these new capabilities of the PHP API client:
- Copy a whole index
- Copy all rules from an index
- Replace all data in an index
- Generate secured API keys
For example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ArticleController extends AbstractController
{
public function refillAction(SearchClient $client, array $objects)
{
// Copy an index
$client->copyIndex('SRC_INDEX_NAME', 'DEST_INDEX_NAME');
// Copy all the rules from an index
$client->copyRules('SRC_INDEX_NAME', 'DEST_INDEX_NAME');
// Replace all objects
$index = $client->initIndex('DEST_INDEX_NAME');
$index->replaceAllObjects($objects);
}
}
|
IndexManager
has been renamed to SearchService
. This is the only service you will need to interact with Algolia.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Algolia\SearchBundle\IndexManagerInterface;
+ use Algolia\SearchBundle\SearchService;
class ExampleController extends Controller
{
- protected $indexManager;
+ protected $searchService;
- public function __construct(IndexManagerInterface $indexingManager)
+ public function __construct(SearchServiceInterface $searchService)
{
- $this->indexManager = $indexingManager;
+ $this->searchService = $searchService;
}
}
|
Renamed methods#
The following methods are renamed in version 4:
getSearchableEntities()
#
1
2
3
4
5
| // Version 3:
- getSearchableEntities(): array<int, string>
// Version 4
+ getSearchables(): array<int, string>
|
getFullIndexName()
#
1
2
3
4
5
| // Version 3:
- getFullIndexName($className): string
// Version 4
+ searchableAs($className): string
|
Method signature changes#
The following methods have their signatures changed in version 4:
index()
#
1
2
| - index($entities, ObjectManager $objectManager): array<string, int>
+ index(ObjectManager $objectManager, $searchables, $requestOptions = []): array<int, array<string, \Algolia\AlgoliaSearch\Response\AbstractResponse>>
|
remove()
#
1
2
| - remove($entities, ObjectManager $objectManager): array<string, int>
+ remove(ObjectManager $objectManager, $searchables, $requestOptions = []): array<int, array<string, \Algolia\AlgoliaSearch\Response\AbstractResponse>>`
|
clear()
#
1
2
| - clear($className): boolean
+ clear($className): \Algolia\AlgoliaSearch\Response\AbstractResponse
|
delete()
#
1
2
| - delete($className): boolean
+ delete($className): \Algolia\AlgoliaSearch\Response\AbstractResponse
|
search()
#
1
2
| - search($query, $className, ObjectManager $objectManager, $page = 1, $nbResults = null, array $parameters = []): array<int, object>
+ search(ObjectManager $objectManager, $className, $query = '', $requestOptions = []): array<int, object>
|
rawSearch()
#
1
2
| - rawSearch($query, $className, $page = 1, $nbResults = null, array $parameters = []): array<string, int | string | array>
+ rawSearch($className, $query = '', $requestOptions = []): array<string, int | string | array>
|
count()
#
1
2
| - count($query, $className, array $parameters = []): int
+ count($className, $query = '', $requestOptions = []): int
|
Methods from the SearchService
are now waitable.
You can use the wait()
method to wait for your task to be completely handled by the engine before moving on, instead of handling this logic yourself.
Examples:
1
2
3
4
5
| // index objects and wait for the task to finish
$response = $this->searchService->index($entityManager, $objects)->wait();
// clear an index
$response = $this->searchService->clear($className)->wait();
|
This also means that the return type of waitable methods has changed. They now all return an AbstractResponse
. Please update your code accordingly.
The methods follow these rules:
- Required parameters have their own argument
- Optional arguments are passed in an array or as
RequestOptions
object as the last argument
- The client never sets any default values
You can pass any optional arguments and search parameters to the engine in the $requestOptions
parameter, available in all SearchService
methods. For example, you can add filters, choose which facets to retrieve, change the number of hits per page, or pass new headers.
For example:
1
2
3
4
5
6
7
8
9
10
11
| - $result = $this->indexManager->remove(
+ $result = $this->searchService->remove(
+ $this->get('doctrine')->getManager(),
$searchablePosts,
- $this->get('doctrine')->getManager()
- // you could not pass requestOptions
+ // here you can pass any requestOptions or headers, for example 'X-Forwarded-For', 'X-Algolia-UserToken'...
+ [
+ 'X-Forwarded-For' => '0.0.0.0',
+ ]
+ );
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| - $result = $this->indexManager->search(
+ $result = $this->searchService->search(
+ $this->get('doctrine')->getManager(),
+ Post::class,
'foo',
- Post::class,
- $this->get('doctrine')->getManager(),
- // the optional page and hitsPerPage parameters were passed separately
- // page argument was starting from 1
- 1,
- 20,
- 'attributesToRetrieve' => [
- 'title',
- ]
+ // all the optional parameters are now sent as once in the $requestOptions
+ // be careful as page argument now starts from 0
+ [
+ 'page' => 0,
+ 'hitsPerPage' => 20,
+ 'attributesToRetrieve' => [
+ 'title',
+ ],
+ ]
+ );
|
Algolia\SearchBundle\Engine\AlgoliaEngine
#
Removed in version 4.
Algolia\SearchBundle\IndexManager
#
1
2
| - Algolia\SearchBundle\IndexManager
+ Algolia\SearchBundle\Services\AlgoliaSearchService
|
Algolia\SearchBundle\IndexManagerInterface
#
1
2
| - Algolia\SearchBundle\IndexManagerInterface
+ Algolia\SearchBundle\SearchService
|
Algolia\SearchBundle\Engine\NullEngine
#
1
2
| - Algolia\SearchBundle\Engine\NullEngine
+ Algolia\SearchBundle\Services\NullSearchService
|
For testing, use Algolia\SearchBundle\SearchService
by mocking or extending it,
and override the search.service
in your test configuration.
For more information, see:
These classes are internal in version 4:
Algolia\SearchBundle\Command\SearchClearCommand
Algolia\SearchBundle\Command\SearchImportCommand
Algolia\SearchBundle\Command\SearchSettingsBackupCommand
Algolia\SearchBundle\Command\SearchSettingsCommand
Algolia\SearchBundle\Command\SearchSettingsPushCommand
Algolia\SearchBundle\DependencyInjection\AlgoliaSearchExtension
Algolia\SearchBundle\DependencyInjection\Configuration
Algolia\SearchBundle\EventListener\SearchIndexerSubscriber
Algolia\SearchBundle\SearchableEntity
Algolia\SearchBundle\SettingsManager
Internal classes may change between minor versions.
Algolia\SearchBundle\Settings\SettingsManagerInterface
#
1
2
| - Algolia\SearchBundle\Settings\SettingsManagerInterface
+ Algolia\SearchBundle\Settings\SettingsManager
|
Algolia\SearchBundle\Engine\EngineInterface
#
Deleted without replacement.
search.index_manager
#
1
2
| - search.index_manager
+ search.service
|
algolia_client
#
1
2
| - algolia_client
+ search.client // or `SearchClient` class autowired
|