diff --git a/README.md b/README.md index 3cd83a2..894eeac 100644 --- a/README.md +++ b/README.md @@ -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, ], ], ``` diff --git a/src/Typesense/Index.php b/src/Typesense/Index.php index 9748e85..733f225 100644 --- a/src/Typesense/Index.php +++ b/src/Typesense/Index.php @@ -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']; }),