Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Scout Search with FacetFilters?

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?

like image 488
bl4cksta Avatar asked Sep 18 '17 17:09

bl4cksta


2 Answers

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));
});
like image 107
bl4cksta Avatar answered Oct 30 '22 04:10

bl4cksta


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/

like image 34
forsvunnet Avatar answered Oct 30 '22 02:10

forsvunnet