Skip to content

Commit

Permalink
Allow index to maintain the typesense ranking
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Nov 25, 2024
1 parent e152a2e commit 55b3df4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ Any additional settings you want to define per index can be included in the `sta
*/
'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc',
],

/*
Set this to true to maintain the sort score order that Typesense returns
*/
'maintain_rankings' => false,
],
],
```
6 changes: 4 additions & 2 deletions src/Typesense/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,14 @@ public function searchUsingApi($query, array $options = []): array

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

$total = count($searchResults['hits']);

return [
'raw' => $searchResults,
'results' => collect($searchResults['hits'] ?? [])
->map(function ($result, $i) {
->map(function ($result, $i) use ($total) {
$result['document']['reference'] = $result['document']['id'];
$result['document']['search_score'] = (int) ($result['text_match'] ?? 0);
$result['document']['search_score'] = Arr::get($this->config, 'settings.maintain_ranking', false) ? ($total - $i) : (int) ($result['text_match'] ?? 0);

return $result['document'];
}),
Expand Down

0 comments on commit 55b3df4

Please sign in to comment.