As you know, using where()
statement after the search method uses numericFilter
. I am trying to filter my search results with string filtering.
How can I use Facet Filter on search statement?
I am glad to answer my own problem. First of all, it's really hard to find any doc about the Laravel Scout already. If you want to filter your search results with string parameters. Even it's really hard to find an answer too. I checked the whole library line by line.Finally, the result made me proud.
If you want to filter your result with string parameters you need to design callback function to your search()
method and inject your FacetFilters on there.Simply, Let's assume you have Posts
model that you are using Algolia and you have enum typed category
column on it. You can filter your blog post search results with the code below.
$post = Post::search($query, function ($algolia, $query, $options) use ($category){
$new_options = [];
if (!is_null($type)) {
$new_options = ["facetFilters"=>"category_name:".$category];
}
return $algolia->search($query, array_merge($options,$new_options));
});
According to the algolia documentation you can modify the search parameters like this:
$results = Product::search($q)->with(
[
'facets' => ['*'],
'facetFilters' => [$facetFilters],
]
)->paginate(24);
https://www.algolia.com/doc/framework-integration/laravel/searching/server-side-search/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With