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

We need more operators in where clauses #799

Closed neokofg closed 9 months ago

neokofg commented 9 months ago

Now, there is only the = operator, why not add the rest like !=, >, >=, <, <=, TO, EXISTS, IN, NOT, AND, or OR, I wrote a custom classes for meilisearch engine and everything is works fine there , I don't understand why not add these operators? i did something like this

class Builder extends \Laravel\Scout\Builder
{
    public function where($field, $operator = '=', $value = null)
    {
        $this->wheres[$field] = [
            'operator' => $operator,
            'value' => $value
        ];

        return $this;
    }
}
class ExtendedMeiliSearchEngine extends MeilisearchEngine
{
    protected function filters(Builder $builder)
    {
        $wheres = $builder->wheres;

        $filters = null;

        foreach ($wheres as $key => $value) {
            $expression = $value['operator'] == 'TO'
                ? "{$key} {$value['value'][0]} {$value['operator']} {$value['value'][1]}"
                : "{$key}{$value['operator']}{$value['value']}";

            $filters = is_null($filters) ? $expression : "{$filters} AND {$expression}";
        }

        return $filters;
    }
}
trait ExtendedSearchable
{
    use Searchable {
        Searchable::search as parentSearch;
    }

    public static function search($query = '', $callback = null)
    {
        return app(\App\Custom\Scout\Builder::class, [
            'model' => new static,
            'query' => $query,
            'callback' => $callback,
            'softDelete' => static::usesSoftDelete() && config('scout.soft_delete', false),
        ]);
    }
}

now it works fine like this

$this->resumes->where('work_experience', 'TO', [$work_months_from,$work_months_to]); 

or like this

$this->resumes->where('occupation', '!=', $occupation);
neokofg commented 9 months ago

I think this gives a boost to search performance because, now we need to do additional eloquent queries, instead of native engine queries

driesvints commented 9 months ago

We're always open to PR's, thanks.