Skip to content

Commit

Permalink
Support sort_by when querying
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Aug 20, 2024
1 parent 668e0e0 commit 3162202
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ Any additional settings you want to define per index can be included in the `sta
],
],
],
/*
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.sort_by', false)) {
$options['sort_by'] = $sort;
}
}

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

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

0 comments on commit 3162202

Please sign in to comment.