diff --git a/src/Builder.php b/src/Builder.php index 78e9aa33..bf4ddf69 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -55,6 +55,13 @@ class Builder */ public $wheres = []; + /** + * The "where comparison" constraints added to the query. + * + * @var array + */ + public $whereComparisons = []; + /** * The "where in" constraints added to the query. * @@ -69,6 +76,13 @@ class Builder */ public $whereNotIns = []; + /** + * The "between" constraints added to the query. + * + * @var array + */ + public $whereBetween = []; + /** * The "limit" that should be applied to the search. * @@ -127,12 +141,19 @@ public function within($index) * Add a constraint to the search query. * * @param string $field + * @param string $operator * @param mixed $value * @return $this */ - public function where($field, $value) + public function where($field, $operator = null, $value = null) { - $this->wheres[$field] = $value; + if (func_num_args() === 2) { + $this->wheres[$field] = $operator; + } elseif (trim($operator) === '=') { + $this->wheres[$field] = $value; + } else { + $this->whereComparisons[$field] = ['operator' => $operator, 'value' => $value]; + } return $this; } @@ -165,6 +186,20 @@ public function whereNotIn($field, array $values) return $this; } + /** + * Add a "between" constraint to the search query. + * + * @param string $field + * @param array $values + * @return $this + */ + public function whereBetween($field, array $values) + { + $this->whereBetween[$field] = $values; + + return $this; + } + /** * Include soft deleted records in the results. * diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index eb8f4d28..86bf22e4 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -171,23 +171,32 @@ protected function performSearch(Builder $builder, array $searchParams = []) /** * Get the filter array for the query. * - * @param \Laravel\Scout\Builder $builder * @return string */ protected function filters(Builder $builder) { - $filters = collect($builder->wheres)->map(function ($value, $key) { - if (is_bool($value)) { - return sprintf('%s=%s', $key, $value ? 'true' : 'false'); + $wheres = array_map(function ($value) { + return ['operator' => '=', 'value' => $value]; + }, $builder->wheres); + + $filters = collect(array_merge($wheres, $builder->whereComparisons))->map(function ($value, $key) { + if (is_bool($value['value'])) { + return sprintf('%s'.$value['operator'].'%s', $key, $value['value'] ? 'true' : 'false'); } - return is_numeric($value) - ? sprintf('%s=%s', $key, $value) - : sprintf('%s="%s"', $key, $value); + return is_numeric($value['value']) + ? sprintf('%s'.$value['operator'].'%s', $key, $value['value']) + : sprintf('%s'.$value['operator'].'"%s"', $key, $value['value']); }); + if (property_exists($builder, 'whereBetween')) { + foreach ($builder->whereBetween as $key => $values) { + $filters->push(sprintf('%s "%s" TO "%s"', $key, $values[0], end($values))); + } + } + $whereInOperators = [ - 'whereIns' => 'IN', + 'whereIns' => 'IN', 'whereNotIns' => 'NOT IN', ];