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

ryangurnick's avatar

Scout Greater/Less Than

I am working on a project that is leveraging scout with meilisearch and I am slowly noticing that scout only really supports the simplest searching functionality.

My desire at this point is to be able to "filter" (add a where clause) on a meilisearch query using greater than or less than with dates.

In my searchable data I have added a date that a record was created and right now I can only provide the end user the ability to search for the exact date string. Not ideal.

At this point I am running my meilisearch query using scout and dynamically adding calls to where() when needed before calling paginate(). Such as in my example below.

$results = Sighting::search($query);
if ($condition) {
    $results->where('key', 'value');
}

What I ideally would like to be able to do (but is currently not possible)... note: the <= comparison operator.

$results = Sighting::search($query);
if ($condition) {
    $results->where('key', '<=', 'value');
}

Is there some way to paginate and easily filter/restrict the result-set using greater than or less than operators on a date?

0 likes
1 reply
LaryAI's avatar
Level 58

As of my knowledge cutoff in early 2023, Laravel Scout does not natively support complex where clauses with operators like <, <=, >, or >= out of the box, especially when using MeiliSearch as the search engine. This is because Scout is designed to provide a simple, driver-agnostic way of adding full-text search to your Eloquent models, and it abstracts away the specifics of any particular search engine.

However, you can work around this limitation by using MeiliSearch's filtering capabilities directly. MeiliSearch supports a range of filters that can be applied to search queries, including comparisons on numerical values and dates (if they are formatted in a way that MeiliSearch can interpret as a number, such as a timestamp).

Here's how you might implement a greater-than filter using MeiliSearch's raw search capabilities within a Laravel application:

use MeiliSearch\Client;

// Assuming you have set up MeiliSearch client and have the index name
$client = new Client(config('scout.meilisearch.host'), config('scout.meilisearch.key'));
$index = $client->index('sightings'); // Replace 'sightings' with your actual index name

$query = 'your search query';
$filters = 'created_at > 2023-01-01'; // Replace with your actual condition

// Perform the search with filters
$searchResults = $index->search($query, [
    'filter' => $filters
]);

// Now you can paginate the results manually or return them as needed

Please note that the created_at field must be indexed as a sortable attribute in your MeiliSearch index for this to work. You can configure sortable attributes in MeiliSearch using the dashboard or via an API call.

If you want to integrate this more closely with Scout, you could extend the Scout builder with a custom macro that allows you to add raw filters:

use Laravel\Scout\Builder;

Builder::macro('whereRaw', function ($filter) {
    $this->engine()->addFilter($this, $filter);
    return $this;
});

// Usage
$results = Sighting::search($query)
    ->whereRaw('created_at > 2023-01-01')
    ->paginate();

In this macro, addFilter would be a custom method you'd need to implement in your custom engine that extends MeiliSearch's engine, which would handle adding the raw filter to the search query.

Remember that this is a workaround and may not be as elegant as using Scout's built-in methods. It's also important to be aware that by bypassing Scout's standard methods, you may lose some of the benefits of using Scout, such as model hydration and automatic pagination. You'll need to handle these aspects manually if they are required for your application.

Please or to participate in this conversation.