kennedytedesco/meilisearch-search-filter

A fluent and intuitive way to construct filters for Meilisearch queries.

v1.0 2023-08-30 13:31 UTC

This package is auto-updated.

Last update: 2024-10-30 02:10:59 UTC


README

Meilisearch Search Filter

Build Status PHPStan License

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.