Skip to content

Commit

Permalink
Flip object ids for ID lookup
Browse files Browse the repository at this point in the history
By flipping the object ids as keys their positions become the values of the array and it becomes easier to look them up. This way we don't need to do a separate time-consuming array_search call for each item.
  • Loading branch information
driesvints committed Apr 1, 2019
1 parent 8a404bc commit bde4969
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()];
});
}

Expand Down

0 comments on commit bde4969

Please sign in to comment.