Skip to content

Commit 55b3df4

Browse files
committed
Allow index to maintain the typesense ranking
1 parent e152a2e commit 55b3df4

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ Any additional settings you want to define per index can be included in the `sta
102102
*/
103103
'sort_by' => '_text_match(buckets: 10):desc,weighted_score:desc',
104104
],
105+
106+
/*
107+
Set this to true to maintain the sort score order that Typesense returns
108+
*/
109+
'maintain_rankings' => false,
105110
],
106111
],
107112
```

src/Typesense/Index.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ public function searchUsingApi($query, array $options = []): array
114114

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

117+
$total = count($searchResults['hits']);
118+
117119
return [
118120
'raw' => $searchResults,
119121
'results' => collect($searchResults['hits'] ?? [])
120-
->map(function ($result, $i) {
122+
->map(function ($result, $i) use ($total) {
121123
$result['document']['reference'] = $result['document']['id'];
122-
$result['document']['search_score'] = (int) ($result['text_match'] ?? 0);
124+
$result['document']['search_score'] = Arr::get($this->config, 'settings.maintain_ranking', false) ? ($total - $i) : (int) ($result['text_match'] ?? 0);
123125

124126
return $result['document'];
125127
}),

0 commit comments

Comments
 (0)