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.