-
Notifications
You must be signed in to change notification settings - Fork 105
Search
The search method returns a SearchResult instance.
public function search($query, array $searchParams = [], array $options = [])$query: the query words.
$searchParams: the search parameters handled by MeiliSearch.
$options: options you can pass during the search. There are described in the options section.
💡 If you don't want the search method to return a class instance, use the rawSearch() method or set raw to true in the $options parameter. No instance class will be created during the process which might improve the performances in some situations.
All the supported parameters are described in the search parameters section of the documentation.
You will be able to filter, use facets, restrict the fields to retrieve...
With filters, both single and double quotes are supported.
// Enclosing with double quotes
$index->search('prince', $searchParams = ['filters' => "title = 'Le Petit Prince' OR author = 'J. R. R. Tolkien'"]);
// Enclosing with single quotes
$index->search('hobbit', $searchParams = ['filters' => 'title = "The Hitchhiker\'s Guide to the Galaxy" OR author = "J. R. R. Tolkien"']);Here are the different options you can pass in the $options parameter.
-
removeZeroFacets(default:false): to get the raw results returned by MeiliSearch. Thesearch()will return an array instead of a class instance.
$index->search('prince', $searchParams = [], $options = [removeZeroFacets => true]);
// With removeZeroFacets = false
// "facet": {
// "genre": {
// "horror: 443,
// "thriller": 200,
// "comedy": 0
// }
// }
// With removeZeroFacets = true
// "facet": {
// "genre": {
// "horror: 443,
// "thriller": 200,
// }
// }-
transformHits: the callback that will be applied to thehitsarray returned by MeiliSearch.
// Before PHP 7.4
function titlesToUpperCase(array $hits) {
return array_map($hits, function (array $hit) {
$hit['title'] = strtoupper($hit['title']);
return $hit;
});
}
// PHP 7.4 and later
function titlesToUpperCase(array $hits) {
return array_map($hits, function (array $hit) {
return [
...$hit,
title: strtoupper($hit['title']
]
});
}
$index->search('prince', $searchParams = [], $options = ['transformHits' => 'titlesToUpperCase'])-
transformFacetsDistribution(WIP): the callback that will be applied to thefacetsDistributionarray returned by MeiliSearch. -
raw(default:false): to get the raw results returned by MeiliSearch. Thesearch()will return the raw information returned by MeiliSearch server in anArray. No class instance will be created.
$index->search('prince', $searchParams = [], $options = [raw => true]);
If you only want to get an Array with your transformed hits and facetsDistribution, please use toArray instead:
$index->search('prince', $searchParams = [], $options = [removeZeroFacets => true])->toArray();- To remove some specifics hits in the results:
$removeLePetitPrince = function (array $hits) {
return array_filter(
$hits,
function (array $hit) { 'Le Petit Prince' !== $hit['title']; }
);
};
$response = $index->search('prince', $searchParams = [], $options = ['transformHits' => $removeLePetitPrince]);