Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kerranm's avatar

Filtering results with Algolia/Laravel on the server side using the numericFilter param.

Hey ya'all, So, I've integrated the Algolia Search API into Laravel using this documentation - https://www.algolia.com/doc/laravel#install and I'm wondering if someone could tell me how I could 'filter' results using the numericFilter param, server side. Basically I want to return any result based on the search string between two timestamps. I'd love to hear how others have done it..... Thanks, KD

0 likes
6 replies
quarkmarino's avatar

Yeah, I'm wondering too, how to filter an algolia search. anyone?

quarkmarino's avatar

Ok I think I got it, I was reviewing this class

\AlgoliaSearch\Index and at the "search" method it accepts an $args parameter which defaults to null, but here is where you specify the filtering, in my case I needed a facetFilter for the status attribute of the document as published (don't ask), therefore I called this way

    News::search($request->input('query'),['facetFilters' => ['status:published']]);

The first argument it the normal query string for the search and the second are my $args.

but at least for the faceFilters I needed to configure the $algoliaSettings property of my "News" class which uses the "AlgoliaEloquentTrait" and add the "attributesForFaceting" element with the list of attributes that should be available to use for faceting as follows

class News extends Model
{
    ...
    public $algoliaSettings = [
        'attributesToIndex' => [
            'title',
            ...
        ],
        'attributesToRetrieve' => [
            'id',
            'title',
            ...
        ],
        ...
        'attributesForFaceting' => [    //here you specify which attributes could be used on the facetFilters search args
            'status'
        ]
    ];

well, thats all, it is working for me, but I guess its not much different for the numericFilter, although you may not need to alter the $algoliaSettings property.

cheers.

1 like
kerranm's avatar

That worked great quarkmarino, thank you for your time and effort on this.

lasse's avatar

I don't seem to be able to get this to work with Laravel 5.4. Would you be able to post updated example code of how you solved it @kerranm ?

lasse's avatar

For anyone else reading this: It's as easy as chaining ->where('tenant_id', $tenant->id) onto the search()method which sends a NumericFiltered request to Algolia.

Please or to participate in this conversation.