From 9182859930691ff570dd7adbb0dd9e570fbb48b4 Mon Sep 17 00:00:00 2001 From: Abdullah Al-Faqeir Date: Sat, 2 Oct 2021 23:50:28 +0300 Subject: [PATCH] - Fix building location order. --- README.md | 7 ++---- src/Engines/TypesenseSearchEngine.php | 34 ++++++++++++++------------- src/TypesenseServiceProvider.php | 4 ++-- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 8feb415..7b8bfe8 100644 --- a/README.md +++ b/README.md @@ -207,12 +207,9 @@ $search->orderBy('name','desc') - Location Order ```php -$search->orderByLocation('location',48.853, 2.344, '1km') +$search->orderByLocation('location',48.853, 2.344, 'desc') //or -$search->orderByLocation('location',48.853, 2.344, '1mi') - -//to allow ordering with other columns -$search->orderByLocation('location',48.853, 2.344, '1mi','asc',true) +$search->orderByLocation('location',48.853, 2.344, 'asc') ``` - Group by limit diff --git a/src/Engines/TypesenseSearchEngine.php b/src/Engines/TypesenseSearchEngine.php index 6f8a31e..91eedb0 100644 --- a/src/Engines/TypesenseSearchEngine.php +++ b/src/Engines/TypesenseSearchEngine.php @@ -178,23 +178,16 @@ private function buildSearchParams(Builder $builder, int $page, int $perPage): a * @param string $column * @param float $lat * @param float $lng - * @param string $radius * @param string $direction - * @param bool $exclude_radius * * @return string * @noinspection PhpPureAttributeCanBeAddedInspection */ - private function parseOrderByLocation(string $column, float $lat, float $lng, string $radius, string $direction = 'asc', bool $exclude_radius = false): string + private function parseOrderByLocation(string $column, float $lat, float $lng, string $direction = 'asc'): string { $direction = Str::lower($direction) === 'asc' ? 'asc' : 'desc'; - $str = $column.'('.$lat.', '.$lng.', '; - if ($exclude_radius) { - $str .= 'exclude_radius: '.$radius; - } else { - $str .= $radius; - } - return $str.'):'.$direction; + $str = $column.'('.$lat.', '.$lng.')'; + return $str.':'.$direction; } /** @@ -446,14 +439,23 @@ public function limitHits(int $limitHits): static return $this; } - public function orderByLocation(string $column, float $lat, float $lng, string $radius, bool $excludeRadius): static + /** + * Add location to order by clause + * + * @param string $column + * @param float $lat + * @param float $lng + * @param string $direction + * + * @return $this + */ + public function orderByLocation(string $column, float $lat, float $lng, string $direction): static { $this->locationOrderBy = [ - 'column' => $column, - 'lat' => $lat, - 'lng' => $lng, - 'radius' => $radius, - 'exclude_radius' => $excludeRadius, + 'column' => $column, + 'lat' => $lat, + 'lng' => $lng, + 'direction' => $direction, ]; return $this; } diff --git a/src/TypesenseServiceProvider.php b/src/TypesenseServiceProvider.php index 76079a1..fbc5cd2 100644 --- a/src/TypesenseServiceProvider.php +++ b/src/TypesenseServiceProvider.php @@ -47,9 +47,9 @@ private function registerMacros(): void ->search($this)); }); - Builder::macro('orderByLocation', function (string $column, float $lat, float $lng, string $radius, bool $excludeRadius) { + Builder::macro('orderByLocation', function (string $column, float $lat, float $lng, string $direction = 'asc') { $this->engine() - ->orderByLocation($column, $lat, $lng, $radius, $excludeRadius); + ->orderByLocation($column, $lat, $lng, $direction); return $this; });