Skip to content

Commit bde4969

Browse files
committed
Flip object ids for ID lookup
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.
1 parent 8a404bc commit bde4969

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/Engines/AlgoliaEngine.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,14 @@ public function map(Builder $builder, $results, $model)
183183
}
184184

185185
$objectIds = collect($results['hits'])->pluck('objectID')->values()->all();
186+
$objectIdPositions = array_flip($objectIds);
186187

187188
return $model->getScoutModelsByIds(
188189
$builder, $objectIds
189190
)->filter(function ($model) use ($objectIds) {
190191
return in_array($model->getScoutKey(), $objectIds);
191-
})->sortBy(function($model) use ($objectIds) {
192-
return array_search($model->getScoutKey(), $objectIds);
192+
})->sortBy(function($model) use ($objectIdPositions) {
193+
return $objectIdPositions[$model->getScoutKey()];
193194
});
194195
}
195196

0 commit comments

Comments
 (0)