From 0f6e6ff0414bde03651c3472db9088c12b257dc0 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Mon, 25 Apr 2022 10:38:15 +0200 Subject: [PATCH 1/8] Merge where close array --- src/Engines/TypesenseEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 58924a6..4921fba 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -240,7 +240,7 @@ protected function performSearch(Builder $builder, array $options = []): mixed */ protected function filters(Builder $builder): string { - return collect($builder->wheres) + return collect(array_merge($builder->wheres, $builder->whereIns)) ->map([ $this, 'parseFilters', From 89fe39fead201047f17ee8e23f270aff91cbb924 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Mon, 25 Apr 2022 12:25:50 +0200 Subject: [PATCH 2/8] Update whereIn query string --- src/Engines/TypesenseEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 4921fba..9566470 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -260,7 +260,7 @@ protected function filters(Builder $builder): string public function parseFilters(array|string $value, string $key): string { if (is_array($value)) { - return sprintf('%s:%s', $key, implode('', $value)); + return sprintf('%s:=%s', $key, '['. implode(', ', $value).']'); } return sprintf('%s:=%s', $key, $value); From e126cc5405bbac0a43acdcede44910472ac1a8cf Mon Sep 17 00:00:00 2001 From: Joffrey Date: Mon, 25 Apr 2022 12:26:13 +0200 Subject: [PATCH 3/8] Add exhaustive search option --- src/Engines/TypesenseEngine.php | 20 ++++++++++++++++++++ src/Mixin/BuilderMixin.php | 15 +++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 9566470..7d8a181 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -57,6 +57,11 @@ class TypesenseEngine extends Engine */ private array $locationOrderBy = []; + /** + * @var bool + */ + private bool $exhaustiveSearch = false; + /** * TypesenseEngine constructor. * @@ -148,6 +153,7 @@ private function buildSearchParams(Builder $builder, int $page, int | null $perP 'page' => $page, 'highlight_start_tag' => $this->startTag, 'highlight_end_tag' => $this->endTag, + 'exhaustive_search' => $this->exhaustiveSearch, ]; if ($this->limitHits > 0) { @@ -477,6 +483,20 @@ public function orderByLocation(string $column, float $lat, float $lng, string $ return $this; } + /** + * Setting this to true will make Typesense consider all variations of prefixes and typo corrections of the words in the query exhaustively. + * + * @param bool $exhaustiveSearch + * + * @return $this + */ + public function exhaustiveSearch(bool $exhaustiveSearch): static + { + $this->exhaustiveSearch = $exhaustiveSearch; + + return $this; + } + /** * @param string $name * diff --git a/src/Mixin/BuilderMixin.php b/src/Mixin/BuilderMixin.php index 7f9b096..6f5f35e 100644 --- a/src/Mixin/BuilderMixin.php +++ b/src/Mixin/BuilderMixin.php @@ -120,4 +120,19 @@ public function limitHits(): Closure return $this; }; } + + /** + * @param bool $exhaustiveSearch + * + * @return Closure + */ + public function exhaustiveSearch(): Closure + { + return function (bool $exhaustiveSearch) { + $this->engine() + ->exhaustiveSearch($exhaustiveSearch); + + return $this; + }; + } } From e3832f7d8a4cb7c3b965421fce735f134a069004 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Mon, 25 Apr 2022 12:29:52 +0200 Subject: [PATCH 4/8] Fix indentation --- src/Engines/TypesenseEngine.php | 38 ++++++++++++++++----------------- src/Mixin/BuilderMixin.php | 28 ++++++++++++------------ 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 7d8a181..f8e42d7 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -57,10 +57,10 @@ class TypesenseEngine extends Engine */ private array $locationOrderBy = []; - /** - * @var bool - */ - private bool $exhaustiveSearch = false; + /** + * @var bool + */ + private bool $exhaustiveSearch = false; /** * TypesenseEngine constructor. @@ -153,7 +153,7 @@ private function buildSearchParams(Builder $builder, int $page, int | null $perP 'page' => $page, 'highlight_start_tag' => $this->startTag, 'highlight_end_tag' => $this->endTag, - 'exhaustive_search' => $this->exhaustiveSearch, + 'exhaustive_search' => $this->exhaustiveSearch, ]; if ($this->limitHits > 0) { @@ -266,7 +266,7 @@ protected function filters(Builder $builder): string public function parseFilters(array|string $value, string $key): string { if (is_array($value)) { - return sprintf('%s:=%s', $key, '['. implode(', ', $value).']'); + return sprintf('%s:=%s', $key, '['. implode(', ', $value).']'); } return sprintf('%s:=%s', $key, $value); @@ -483,19 +483,19 @@ public function orderByLocation(string $column, float $lat, float $lng, string $ return $this; } - /** - * Setting this to true will make Typesense consider all variations of prefixes and typo corrections of the words in the query exhaustively. - * - * @param bool $exhaustiveSearch - * - * @return $this - */ - public function exhaustiveSearch(bool $exhaustiveSearch): static - { - $this->exhaustiveSearch = $exhaustiveSearch; - - return $this; - } + /** + * Setting this to true will make Typesense consider all variations of prefixes and typo corrections of the words in the query exhaustively. + * + * @param bool $exhaustiveSearch + * + * @return $this + */ + public function exhaustiveSearch(bool $exhaustiveSearch): static + { + $this->exhaustiveSearch = $exhaustiveSearch; + + return $this; + } /** * @param string $name diff --git a/src/Mixin/BuilderMixin.php b/src/Mixin/BuilderMixin.php index 6f5f35e..7d5f458 100644 --- a/src/Mixin/BuilderMixin.php +++ b/src/Mixin/BuilderMixin.php @@ -121,18 +121,18 @@ public function limitHits(): Closure }; } - /** - * @param bool $exhaustiveSearch - * - * @return Closure - */ - public function exhaustiveSearch(): Closure - { - return function (bool $exhaustiveSearch) { - $this->engine() - ->exhaustiveSearch($exhaustiveSearch); - - return $this; - }; - } + /** + * @param bool $exhaustiveSearch + * + * @return Closure + */ + public function exhaustiveSearch(): Closure + { + return function (bool $exhaustiveSearch) { + $this->engine() + ->exhaustiveSearch($exhaustiveSearch); + + return $this; + }; + } } From 31adc89065cf83d5ad9f2a9321f78a6399d51af3 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Wed, 27 Apr 2022 10:11:13 +0200 Subject: [PATCH 5/8] Split parse filter methods To allow possibility to continue using complex where clauses --- src/Engines/TypesenseEngine.php | 42 +++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index f8e42d7..d7e2d3a 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -246,32 +246,54 @@ protected function performSearch(Builder $builder, array $options = []): mixed */ protected function filters(Builder $builder): string { - return collect(array_merge($builder->wheres, $builder->whereIns)) - ->map([ - $this, - 'parseFilters', - ]) - ->values() - ->implode(' && '); + $whereFilter = collect($builder->wheres) + ->map([ + $this, + 'parseWhereFilter', + ]) + ->values() + ->implode(' && '); + + $whereInFilter = collect($builder->whereIns) + ->map([ + $this, + 'parseWhereInFilter', + ]) + ->values() + ->implode(' && '); + + return $whereFilter . $whereInFilter; } /** - * Parse typesense filters. + * Parse typesense where filter. * * @param array|string $value * @param string $key * * @return string */ - public function parseFilters(array|string $value, string $key): string + public function parseWhereFilter(array|string $value, string $key): string { if (is_array($value)) { - return sprintf('%s:=%s', $key, '['. implode(', ', $value).']'); + return sprintf('%s=%s', $key, implode(', ', $value)); } return sprintf('%s:=%s', $key, $value); } + /** + * Parse typesense whereIn filter. + * + * @param array $value + * @param string $key + * + * @return string + */ + public function parseWhereInFilter(array $value, string $key): string + { + return sprintf('%s:=%s', $key, '['. implode(', ', $value).']'); + } /** * @param mixed $results From 85ef9393ea634c505251523d05e01099b667ebea Mon Sep 17 00:00:00 2001 From: Joffrey Date: Wed, 27 Apr 2022 10:13:23 +0200 Subject: [PATCH 6/8] Use previous code for where filter --- src/Engines/TypesenseEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index d7e2d3a..52e6d1f 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -276,7 +276,7 @@ protected function filters(Builder $builder): string public function parseWhereFilter(array|string $value, string $key): string { if (is_array($value)) { - return sprintf('%s=%s', $key, implode(', ', $value)); + return sprintf('%s:%s', $key, implode('', $value)); } return sprintf('%s:=%s', $key, $value); From b41db867cbe5cc87bc75bd997a147369e125d509 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Wed, 27 Apr 2022 10:15:08 +0200 Subject: [PATCH 7/8] Update indentation --- src/Engines/TypesenseEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index 52e6d1f..e7cd28b 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -276,7 +276,7 @@ protected function filters(Builder $builder): string public function parseWhereFilter(array|string $value, string $key): string { if (is_array($value)) { - return sprintf('%s:%s', $key, implode('', $value)); + return sprintf('%s:%s', $key, implode('', $value)); } return sprintf('%s:=%s', $key, $value); From a4f3e5fff7a445855269b6944a0d1841e49d2c67 Mon Sep 17 00:00:00 2001 From: Joffrey Date: Fri, 20 May 2022 15:21:58 +0200 Subject: [PATCH 8/8] Allow possibility to use both conditions together --- src/Engines/TypesenseEngine.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Engines/TypesenseEngine.php b/src/Engines/TypesenseEngine.php index e7cd28b..e4aaa7f 100644 --- a/src/Engines/TypesenseEngine.php +++ b/src/Engines/TypesenseEngine.php @@ -262,7 +262,7 @@ protected function filters(Builder $builder): string ->values() ->implode(' && '); - return $whereFilter . $whereInFilter; + return $whereFilter . ' && ' . $whereInFilter; } /**