Skip to content

Commit

Permalink
implement abstract method lazyMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeroen-G committed Jun 10, 2021
1 parent dd4257b commit 10f88bf
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Infrastructure/Scout/ElasticEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use JeroenG\Explorer\Application\IndexAdapterInterface;
use JeroenG\Explorer\Application\Operations\Bulk\BulkUpdateOperation;
use JeroenG\Explorer\Application\Results;
Expand Down Expand Up @@ -132,6 +133,27 @@ public function map(Builder $builder, $results, $model): Collection
})->values();
}

public function lazyMap(Builder $builder, $results, $model): LazyCollection
{
Assert::isInstanceOf($results, Results::class);

if ($results->count() === 0) {
return LazyCollection::make($model->newCollection());
}

$objectIds = $this->mapIds($results)->all();
$objectIdPositions = array_flip($objectIds);

return $model->getScoutModelsByIds(
$builder,
$objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds, false);
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
}

/**
* Get the total count from a raw result returned by the engine.
*
Expand Down

0 comments on commit 10f88bf

Please sign in to comment.