kennedytedesco / meilisearch-search-filter
A fluent and intuitive way to construct filters for Meilisearch queries.
Installs: 3 092
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- laravel/pint: ^1.11
- pestphp/pest: ^2.16
- phpstan/extension-installer: ^1.3
- phpstan/phpstan: ^1.10.32
- phpstan/phpstan-strict-rules: ^1.5
- rector/rector: ^0.18.0
README
Meilisearch Search Filter
This zero-dependency library provides a fluent and intuitive way to construct filters for Meilisearch queries. It simplifies the process of building filters by offering a chainable API.
Installation
Minimum requirements: PHP 8.1 or higher.
To use this library in your project, you can install it using Composer:
composer require kennedytedesco/meilisearch-search-filter "^1.0"
Usage
You can learn how Meilisearch filters work by reading the official documentation.
You should also check out the Meilisearch PHP SDK.
Here are some examples that demonstrate how to build filters using this library:
use Meilisearch\Client; use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $client = new Client('http://127.0.0.1:7700', 'masterKey'); $filter = SearchFilter::new() ->where(function (SearchFilter $filter) { $filter->whereGreaterThan('rating.critics', 80) ->whereGreaterThanOrEqual('rating.users', 70); }) ->whereIn('genres', ['Horror', 'Thriller']); $index = $client->index('movies'); $results = $index->search('wonder', [ // (rating.critics > 80 AND rating.users >= 70) AND genres IN ["Horror", "Thriller"] 'filter' => $filter->build(), ]);
You can also use alias methods when constructing filters:
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $filter = SearchFilter::new() ->where(function (SearchFilter $filter) { $filter->whereGt('rating.critics', 80) ->whereGte('rating.users', 70); }) ->orWhereIn('genres', ['Horror', 'Thriller']); echo $filter->build(); // Output: (rating.critics > 80 AND rating.users >= 70) OR genres IN ["Horror", "Thriller"]
Alternatively, you can use the where(...)
method and pass the operator as the second argument:
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $filter = SearchFilter::new() ->where(function (SearchFilter $filter) { $filter->where('rating.critics', '>', 80) ->where('rating.users', '>=', 70); }) ->orWhereIn('genres', ['Horror', 'Thriller']); echo $filter->build(); // Output: (rating.critics > 80 AND rating.users >= 70) OR genres IN ["Horror", "Thriller"]
Using Between
The TO
operator is equivalent to >=
AND <=
.
For more details, see this link.
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $filter = SearchFilter::new() ->whereBetween('rating.critics', 80, 90); echo $filter->build(); // Output: rating.critics 80 TO 90
If you want your results to only include "comedy" and "horror" movies released after March 1995, it's mandatory to group
the OR
conditions:
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $filter = SearchFilter::new() ->where(function (SearchFilter $filter) { $filter->where('genres', 'horror') ->orWhere('genres', 'comedy'); }) ->where('release_date', '>', '795484800'); echo $filter->build(); // Output: (genres = "horror" OR genres = "comedy") AND release_date > 795484800
So, when you provide a closure to the where()
or orWhere()
methods, a fresh SearchFilter
instance is passed to the
closure as the first argument. This lets you craft nested filters within parentheses.
Using when()
The when()
method allows you to conditionally add filters to the query. For example:
use KennedyTedesco\Meilisearch\SearchFilter\SearchFilter; $filter = SearchFilter::new() ->when($request->filled('type'), function (SearchFilter $filter) use($request) { $filter->where('type', $request->get('type')); }) ->where('release_date', '>', '795484800');
All available filter methods
Contributing
If you'd like to contribute to this project, feel free to submit pull requests or open issues on the GitHub repository.