diff --git a/src/Engines/AlgoliaEngine.php b/src/Engines/AlgoliaEngine.php index 77a4268b..5bf88ed8 100644 --- a/src/Engines/AlgoliaEngine.php +++ b/src/Engines/AlgoliaEngine.php @@ -183,13 +183,14 @@ public function map(Builder $builder, $results, $model) } $objectIds = collect($results['hits'])->pluck('objectID')->values()->all(); + $objectIdPositions = array_flip($objectIds); return $model->getScoutModelsByIds( $builder, $objectIds )->filter(function ($model) use ($objectIds) { return in_array($model->getScoutKey(), $objectIds); - })->sortBy(function($model) use ($objectIds) { - return array_search($model->getScoutKey(), $objectIds); + })->sortBy(function($model) use ($objectIdPositions) { + return $objectIdPositions[$model->getScoutKey()]; }); }