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

Commit

Permalink
- Fix building location order.
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdullahFaqeir committed Oct 2, 2021
1 parent 7fdd1a0 commit 9182859
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 23 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 18 additions & 16 deletions src/Engines/TypesenseSearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions src/TypesenseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down

0 comments on commit 9182859

Please sign in to comment.