Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 9182859

Browse files
- Fix building location order.
1 parent 7fdd1a0 commit 9182859

File tree

3 files changed

+22
-23
lines changed

3 files changed

+22
-23
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,9 @@ $search->orderBy('name','desc')
207207
- Location Order
208208

209209
```php
210-
$search->orderByLocation('location',48.853, 2.344, '1km')
210+
$search->orderByLocation('location',48.853, 2.344, 'desc')
211211
//or
212-
$search->orderByLocation('location',48.853, 2.344, '1mi')
213-
214-
//to allow ordering with other columns
215-
$search->orderByLocation('location',48.853, 2.344, '1mi','asc',true)
212+
$search->orderByLocation('location',48.853, 2.344, 'asc')
216213
```
217214

218215
- Group by limit

src/Engines/TypesenseSearchEngine.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -178,23 +178,16 @@ private function buildSearchParams(Builder $builder, int $page, int $perPage): a
178178
* @param string $column
179179
* @param float $lat
180180
* @param float $lng
181-
* @param string $radius
182181
* @param string $direction
183-
* @param bool $exclude_radius
184182
*
185183
* @return string
186184
* @noinspection PhpPureAttributeCanBeAddedInspection
187185
*/
188-
private function parseOrderByLocation(string $column, float $lat, float $lng, string $radius, string $direction = 'asc', bool $exclude_radius = false): string
186+
private function parseOrderByLocation(string $column, float $lat, float $lng, string $direction = 'asc'): string
189187
{
190188
$direction = Str::lower($direction) === 'asc' ? 'asc' : 'desc';
191-
$str = $column.'('.$lat.', '.$lng.', ';
192-
if ($exclude_radius) {
193-
$str .= 'exclude_radius: '.$radius;
194-
} else {
195-
$str .= $radius;
196-
}
197-
return $str.'):'.$direction;
189+
$str = $column.'('.$lat.', '.$lng.')';
190+
return $str.':'.$direction;
198191
}
199192

200193
/**
@@ -446,14 +439,23 @@ public function limitHits(int $limitHits): static
446439
return $this;
447440
}
448441

449-
public function orderByLocation(string $column, float $lat, float $lng, string $radius, bool $excludeRadius): static
442+
/**
443+
* Add location to order by clause
444+
*
445+
* @param string $column
446+
* @param float $lat
447+
* @param float $lng
448+
* @param string $direction
449+
*
450+
* @return $this
451+
*/
452+
public function orderByLocation(string $column, float $lat, float $lng, string $direction): static
450453
{
451454
$this->locationOrderBy = [
452-
'column' => $column,
453-
'lat' => $lat,
454-
'lng' => $lng,
455-
'radius' => $radius,
456-
'exclude_radius' => $excludeRadius,
455+
'column' => $column,
456+
'lat' => $lat,
457+
'lng' => $lng,
458+
'direction' => $direction,
457459
];
458460
return $this;
459461
}

src/TypesenseServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ private function registerMacros(): void
4747
->search($this));
4848
});
4949

50-
Builder::macro('orderByLocation', function (string $column, float $lat, float $lng, string $radius, bool $excludeRadius) {
50+
Builder::macro('orderByLocation', function (string $column, float $lat, float $lng, string $direction = 'asc') {
5151
$this->engine()
52-
->orderByLocation($column, $lat, $lng, $radius, $excludeRadius);
52+
->orderByLocation($column, $lat, $lng, $direction);
5353
return $this;
5454
});
5555

0 commit comments

Comments
 (0)