laravel / scout

Laravel Scout provides a driver based solution to searching your Eloquent models.
https://laravel.com/docs/scout
MIT License
1.55k stars 331 forks source link

Advanced query syntax #160

Closed tillkruss closed 6 years ago

tillkruss commented 7 years ago

Right now Scout only supports simple chained where clauses. I regularly need to perform queries like != and the simplest solution for that seems to be this:

Product::search($request->q, function ($algolia, $query, $options) {
    return $algolia->search($query, $options + ['filters' => sprintf(
        'state = %s AND type != %s',
        Product::STATE_ACTIVE,
        Product::TYPE_VIDEO
    )]);
})->paginate(20);

Which doesn't read very nicely. Could we add advanced queries to Scout?

Product::search('foobar')
    ->where('state', 'one')
    ->whereNot('type', 'foo')
    ->where('rating', '>', 5)

I'm happy to submit a PR.

hubertnnn commented 6 years ago

I also think that a few more complex query options should be added. Perfectly those should match the eloquent query builder methods.

As an extra reason I can add, that different providers are adding similar filters: yab/laravel-scout-mysql-driver is using ->where('rating >', 5) damiantw/laravel-scout-mysql-driver is using ->where('rating >', 5) babenkoivan/scout-elasticsearch-driver is using ->where('rating', '>', 5)

This inconsistency makes it hard to switch between drivers, when using any form of range filtering of search results, and those are very common, eg. in online shops where users can pick price range.

driesvints commented 6 years ago

This was declined in https://github.com/laravel/scout/pull/141/files so going to close this. You can always try http://github.com/laravel/ideas to see if there's more people who want this :)