Skip to content

Commit

Permalink
Support sort_by when querying (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell authored Aug 20, 2024
2 parents 668e0e0 + 8c95468 commit d81300f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ Any additional settings you want to define per index can be included in the `sta
],
],
],
'search_options' => [
/*
Specify a custom sort by order, see the Typesense documentation for more info:
https://typesense.org/docs/guide/ranking-and-relevance.html#ranking-based-on-relevance-and-popularity
*/
'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc',
],
],
],
```
6 changes: 6 additions & 0 deletions src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public function searchUsingApi($query, array $options = []): Collection
->join(',') ?: '*';
}

if (! isset($options['sort_by'])) {
if ($sort = Arr::get($this->config, 'settings.search_options.sort_by', false)) {
$options['sort_by'] = $sort;
}
}

$searchResults = $this->getOrCreateIndex()->documents->search($options);

return collect($searchResults['hits'] ?? [])
Expand Down

0 comments on commit d81300f

Please sign in to comment.