Search
Simple Search
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 are
pulled from the database (that’s why you need to pass the Doctrine Manager).
Notice that I use $this->indexManager
here because your IndexManager must be
injected in your class. Read how to inject the IndexManager here.
1
2
3
$em = $this->getDoctrine()->getManagerForClass(Post::class);
$posts = $this->indexManager->search('query', Post::class, $em);
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.
1
$posts = $this->indexManager->rawSearch('query', Post::class);
Pagination
To get a specific page, define the page
(and nbResults
if you want).
1
2
3
4
5
$em = $this->getDoctrine()->getManagerForClass(Post::class);
$posts = $this->indexManager->search('query', Post::class, $em, 2);
// Or
$posts = $this->indexManager->search('query', Post::class, $em, 2, 100);
Count
1
$posts = $this->indexManager->count('query', Post::class);
Advanced search
Search-related methods have take a $parameters
array as the last arguments. You can pass any search parameters (in the Algolia sense).
1
2
3
4
5
$em = $this->getDoctrine()->getManagerForClass(Post::class);
$posts = $this->indexManager->search('query', Post::class, $em, 1, 10, ['filters' => 'comment_count>10']);
// Or
$posts = $this->indexManager->rawSearch('query', Post::class, 1, 10, ['filters' => 'comment_count>10']);
Note that search
will only take IDs and use doctrine to create a collection of 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.
1
2
3
4
5
6
7
8
9
10
$results = $this->indexManager->rawSearch('query', Post::class, 1, 10, [
'facets' => ['*'], // Retrieve all facets
'getRankingInfo' => true,
]);
$results = $this->indexManager->rawSearch('query', Post::class, 1, 10, [
'facets' => ['tags', 'year'],
'attributesToRetrieve' => ['title', 'author_name'],
'getRankingInfo' => true,
]);