Server-Side Search
Basic Search
You can search for documents in your Algolia index with a single line of code. In this example we’ll search for posts. The search
method will query Algolia to get matching results and then will create a doctrine collection. The data is pulled from the database, which is why you need to pass the Doctrine Manager.
Notice the use of $this->searchService
here. The SearchService
must be injected into your class.
1
2
3
$em = $this->getDoctrine()->getManagerForClass(Post::class);
$posts = $this->searchService->search($em, Post::class, 'query');
Note that search
will only retrieve a list of IDs from the engine and use them to create a collection of Doctrine entities so you can only pass parameters to modify what to search, not to modify the type of response.
If you want to modify the attributes to retrieve or retrieve data like facets
, facets_stats
, _rankingInfo
you will need to use the rawSearch()
method.
Request options
All methods in the SearchService
take a $requestOptions
array as the last argument. This lets you pass any search parameters and optional arguments to Algolia.
With this parameters you can influence any of your operations. For example, you can change the number of results to retrieve, fetch a precise page, filter the results, choose what attributes to retrieve, and so on. You can find all possibilities in our list of search API parameters
1
2
3
4
5
6
7
$em = $this->getDoctrine()->getManagerForClass(Post::class);
$posts = $this->searchService->search($em, Post::class, 'query', [
'page' => 0,
'hitsPerPage' => 10,
'filters' => 'comment_count>10'
]);
Raw search
If you want to get the raw results from Algolia, use the rawSearch
method. This is the method you’ll need to use if you want to retrieve the highlighted snippets or ranking information for instance. It takes optionally a $requestOptions
array as the last parameter.
1
2
3
4
5
6
7
8
$posts = $this->searchService->rawSearch(Post::class, 'query');
$posts = $this->searchService->rawSearch(Post::class, 'query', [
'page' => 0,
'hitsPerPage' => 10,
'filters' => 'comment_count>10'
// ...
]);
The difference with the search()
method is that you won’t retrieve Doctrine Entities, but a JSON object.
Count
Use this method if you need to know quickly how many results are returned by your query. Be careful as it still triggers a search in your engine.
1
$posts = $this->searchService->count(Post::class, 'query');
Clear
The clear()
method is used to clear the records of an index without affecting its settings. It takes an index name or the class name related to an index, and optionally $requestOptions
as the last parameter. This method is waitable.
1
2
3
4
5
6
7
$posts = $this->searchService->clear(Post::class);
$posts = $this->searchService->clear(Post::class, [
// ..any request options
]);
$posts = $this->searchService->clear(Post::class)->wait();
Delete
Delete an index and all its settings, including links to its replicas. It takes an index name or the class name related to an index, and optionally $requestOptions
as the last parameter. This method is waitable as well.
1
2
3
4
5
6
7
$posts = $this->searchService->delete(Post::class);
$posts = $this->searchService->delete(Post::class, [
// ..any request options
]);
$posts = $this->searchService->delete(Post::class)->wait();