Skip to content

Commit cc044a3

Browse files
Make use of Meilisearch sorting (#27)
* Make use of ranking score from Meilisearch * Return collection * Move `->map()` into Query This matches where the search score is set in the Algolia driver. --------- Co-authored-by: Robert <robert@codepeak.se>
1 parent 644e81a commit cc044a3

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/Meilisearch/Index.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace StatamicRadPack\Meilisearch\Meilisearch;
44

5+
use Illuminate\Support\Collection;
56
use Illuminate\Support\Str;
67
use Meilisearch\Client;
78
use Meilisearch\Exceptions\ApiException;
@@ -108,12 +109,12 @@ public function update()
108109
return $this;
109110
}
110111

111-
public function searchUsingApi($query, $options = ['hitsPerPage' => 1000000])
112+
public function searchUsingApi($query, array $options = ['hitsPerPage' => 1000000, 'showRankingScore' => true]): Collection
112113
{
113114
try {
114115
$searchResults = $this->getIndex()->search($query, $options);
115116
} catch (\Exception $e) {
116-
$this->handlemeilisearchException($e, 'searchUsingApi');
117+
$this->handleMeilisearchException($e, 'searchUsingApi');
117118
}
118119

119120
return collect($searchResults->getHits());
@@ -132,16 +133,18 @@ private function getDefaultFields(Searchable $entry): array
132133
];
133134
}
134135

135-
private function handlemeilisearchException($e, $method)
136+
/**
137+
* Custom error parsing for Meilisearch exceptions.
138+
*/
139+
private function handleMeilisearchException($e, $method)
136140
{
137-
// custom error parsing for meilisearch exceptions
141+
// Ignore if already created.
138142
if ($e->errorCode === 'index_already_exists' && $method === 'createIndex') {
139-
// ignore if already created
140143
return true;
141144
}
142145

146+
// Ignore if not found.
143147
if ($e->errorCode === 'index_not_found' && $method === 'deleteIndex') {
144-
// ignore if not found
145148
return true;
146149
}
147150

src/Meilisearch/Query.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ class Query extends QueryBuilder
88
{
99
public function getSearchResults($query)
1010
{
11-
return $this->index->searchUsingApi($query);
11+
$results = $this->index->searchUsingApi($query);
12+
13+
return $results->map(function ($result, $i) {
14+
$result['search_score'] = (int) ceil($result['_rankingScore'] * 1000);
15+
16+
return $result;
17+
});
1218
}
1319
}

0 commit comments

Comments
 (0)