Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We need more operators in where clauses #799

Closed
neokofg opened this issue Jan 29, 2024 · 2 comments
Closed

We need more operators in where clauses #799

neokofg opened this issue Jan 29, 2024 · 2 comments

Comments

@neokofg
Copy link

neokofg commented Jan 29, 2024

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
Copy link
Author

neokofg commented Jan 29, 2024

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

@driesvints
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants