Filtering Meilisearch Search Results with Laravel Scout

Dan Pastori

April 11st, 2022

In order to fine tune your Meilisearch implementation with Laravel Scout, it's a good idea to set up some filters on your index. With Eloquent queries, filtering results is easy. Out of the box, you just add a ->where('x', 'y') or other condition to return results with. You can do the same with Meilisearch, but it has to be configured first.

Since Meilisearch is not a relational database, these have to be very simple filters. However, when using Meilisearch, I've never run into a situation where this became an issue. We are usually letting the full text query do the filtering for us. I usually apply these filters in situations where we need "only results for a user" or "number is greater than x" along with a full text search. Let's get started!

Prerequisites

Like our other Meilisearch articles, I'm going to assume you have Laravel Scout installed and configured with a functioning Meilisearch instance. The documentation to get set up on both of these platforms is top notch.

Step 1: Set Up Filterable Attributes

For our sample, we will use a User model. This model has the following fields, first_name, last_name, company_id. These users are stored in Meilisearch on the users_index index.

In our large scale app, we will have a company (id = 5) that has 30k users. When we visit the company's home page, we want to search only the 30k users that belong to the company (company_id = 5).

Even though you can run a ->where() method on a searchable model (in our case User), you will get an error if you don't have it configured correctly within Meilisearch. To get configured you will have to interact with the Meilisearch instance directly. I usually do this through php artisan tinker or through a migration script when deploying to production. Wherever you place your script, the commands will be the same.

You could also do this through a cURL command if you want as well (just select cURL in the link).

The first step is to connect to the Meilisearch instance through the client:

Initialize Meilisearch Client

$client = new MeiliSearch\Client('https://url_to_meilisearch_instance:7700');

By default, Meilisearch connects on port 7700. If you are deploying to production, it's recommended to set a MASTER_KEY for secure communication. This would be the second parameter:

Initialize Meilisearch Client with Master Key

$client = new MeiliSearch\Client('https://url_to_meilisearch_instance:7700', MASTER_KEY);

Now, we need to user our $client to set the filterable attributes on the users_index. These are the fields we can include in our where clauses.

To do that, run the following command:

Update Filterable Attributes

$client->index('users_index')->updateFilterableAttributes(['company_id']);

That's it! The updateFilterableAttributes() method accepts an array as its parameter of all of the keys that can be filtered. You are now ready to filter your Meilisearch results directly from Meilisearch!

If you are running this on a large dataset, it might take a minute or so to index correctly. You can check on the status by running php artisan tinker and using your $client to run the following method:

Check Indexing Status

$client->index('users_index')->stats();

If isIndexing => false, then you are good to go!

Step 2: Applying the filter

Now all you have to do to filter by company in our example (or by whatever you are filtering by) is to create a query like this:

Filter Search Results by Company

User::search( $term )
    ->where('company_id', 5)
    ->get();

All of your results will be scoped to the company with id = 5!

Conclusion

Meilisearch is a great addition to any application. We use it a ton when building APIs. If you are interested in an API Driven Development approach with Laravel, check out our book!

If you want more information on filterable attributes, check out the Meilisearch documentation. There's a lot of little tweaks and more advanced filters you can run. We will touch on these advanced queries next.

If you have any questions, feel free to reach out on Twitter or leave a comment in our community!

Want to work together?

Professional developers choose Server Side Up to ship quality applications without surrendering control. Explore our tools and resources or work directly with us.

Join our community

We're a community of 3,000+ members help each other level up our development skills.

Platinum Sponsors

Active Discord Members

We help each other through the challenges and share our knowledge when we learn something cool.

Stars on GitHub

Our community is active and growing.

Newsletter Subscribers

We send periodic updates what we're learning and what new tools are available. No spam. No BS.

Sign up for our newsletter

Be the first to know about our latest releases and product updates.

    Privacy first. No spam. No sharing. Just updates.