From b804f49ed15c2181d38788f6f2aa6242d448c7d3 Mon Sep 17 00:00:00 2001 From: rakibul Date: Tue, 31 Oct 2023 14:06:07 +0600 Subject: [PATCH 1/4] Add where between clause --- src/Builder.php | 21 +++++++++++++++++++++ src/Engines/MeilisearchEngine.php | 6 ++++++ 2 files changed, 27 insertions(+) diff --git a/src/Builder.php b/src/Builder.php index 78e9aa33..85f72d25 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -69,6 +69,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. * @@ -165,6 +172,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..1fdb795a 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -186,6 +186,12 @@ protected function filters(Builder $builder) : sprintf('%s="%s"', $key, $value); }); + if (property_exists($builder, 'whereBetween')) { + foreach ($builder->whereBetween as $key => $values) { + $filters->push(sprintf('%s %s TO %s', $key, $values[0], $values[1])); + } + } + $whereInOperators = [ 'whereIns' => 'IN', 'whereNotIns' => 'NOT IN', From b04ae4c633c8926391d0e570e804f62a58220a8d Mon Sep 17 00:00:00 2001 From: rakibul Date: Wed, 1 Nov 2023 20:50:11 +0600 Subject: [PATCH 2/4] Add builder property [where comparison] --- src/Builder.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Builder.php b/src/Builder.php index 85f72d25..027745fe 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. * From 753260bb02619fdf202317aac8c5763e7af5a40f Mon Sep 17 00:00:00 2001 From: rakibul Date: Wed, 1 Nov 2023 20:52:33 +0600 Subject: [PATCH 3/4] Change builder where method signature to accept operator as well --- src/Builder.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Builder.php b/src/Builder.php index 027745fe..bf4ddf69 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -141,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; } From 83a783ca730d79a28fa6793cfaa299087024e4bd Mon Sep 17 00:00:00 2001 From: rakibul Date: Wed, 1 Nov 2023 20:55:32 +0600 Subject: [PATCH 4/4] Update meilisearch filter to accept comparison operators --- src/Engines/MeilisearchEngine.php | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Engines/MeilisearchEngine.php b/src/Engines/MeilisearchEngine.php index 1fdb795a..86bf22e4 100644 --- a/src/Engines/MeilisearchEngine.php +++ b/src/Engines/MeilisearchEngine.php @@ -171,29 +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], $values[1])); + $filters->push(sprintf('%s "%s" TO "%s"', $key, $values[0], end($values))); } } $whereInOperators = [ - 'whereIns' => 'IN', + 'whereIns' => 'IN', 'whereNotIns' => 'NOT IN', ];