From 899286eea5c8111a8b5d5f89b3c59ac8210c17ec Mon Sep 17 00:00:00 2001 From: Oliver Kaufmann Date: Mon, 9 May 2022 17:33:27 +0200 Subject: [PATCH] always add reference to index --- src/MeiliSearch/Index.php | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/MeiliSearch/Index.php b/src/MeiliSearch/Index.php index 1bca644..70b1600 100644 --- a/src/MeiliSearch/Index.php +++ b/src/MeiliSearch/Index.php @@ -112,11 +112,7 @@ 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() @@ -124,9 +120,12 @@ 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) @@ -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);