Skip to content

Commit

Permalink
always add reference to index
Browse files Browse the repository at this point in the history
  • Loading branch information
okaufmann committed May 9, 2022
1 parent c692bff commit 899286e
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/MeiliSearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,20 @@ public function searchUsingApi($query, $filters = [], $options = [])
$this->handleMeiliSearchException($e, 'searchUsingApi');
}

return collect($searchResults->getHits())->map(function ($hit) {
$hit['reference'] = $hit['reference'] ?? $hit['id'];

return $hit;
});
return collect($searchResults->getHits());
}

private function getIndex()
{
return $this->client->index($this->name);
}

private function getDefaultFields($entry)
private function getDefaultFields(Entry|LocalizedTerm|Asset|User $entry)
{
return ['id' => $this->getSafeDocmentID($entry)];
return [
'id' => $this->getSafeDocmentID($entry->reference()),
'reference' =>$entry->reference(),
];
}

private function handleMeiliSearchException($e, $method)
Expand All @@ -150,12 +149,12 @@ private function handleMeiliSearchException($e, $method)
* As a document id is only allowed to be an integer or string composed only of alphanumeric characters (a-z A-Z 0-9), hyphens (-), and underscores (_) we need to make sure that the ID is safe to use.
* More under https://docs.meilisearch.com/reference/api/error_codes.html#invalid-document-id
*
* @param Entry|LocalizedTerm|Asset|User $entry
* @param string $entryReference
* @return string
*/
private function getSafeDocmentID($entry)
private function getSafeDocmentID(string $entryReference)
{
return Str::of($entry->reference())
return Str::of($entryReference)
->explode('::')
->map(function ($part) {
return Str::slug($part);
Expand Down

0 comments on commit 899286e

Please sign in to comment.