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!
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,
company_id. These users are stored in Meilisearch on the
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 (
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:
$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:
$client = new MeiliSearch\Client('https://url_to_meilisearch_instance:7700', MASTER_KEY);
ow, we need to user our
$client to set the filterable attributes on the
users_index. These are the fields we can include in our
To do that, run the following command:
hat’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:
sIndexing => 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:
User::search( $term ) ->where('company_id', 5) ->get();
All of your results will be scoped to the company with id = 5!
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.