Skip to content

Commit

Permalink
Return models as an Eloquent collection
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Jan 22, 2022
1 parent 2fefee8 commit f37a3a4
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Decorators/SearchResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
use ElasticAdapter\Search\Hit as BaseHit;
use ElasticAdapter\Search\SearchResponse;
use ElasticScoutDriverPlus\Factories\LazyModelFactory;
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Traits\ForwardsCalls;
use IteratorAggregate;

/**
* @mixin SearchResponse
* @mixin Collection
* @mixin BaseCollection
*
* @implements IteratorAggregate<int, Hit>
*/
Expand All @@ -35,28 +36,30 @@ public function __construct(SearchResponse $searchResponse, LazyModelFactory $la
$this->lazyModelFactory = $lazyModelFactory;
}

public function hits(): Collection
public function hits(): BaseCollection
{
return $this->searchResponse->hits()->map(function (BaseHit $hit) {
return new Hit($hit, $this->lazyModelFactory);
});
}

public function models(): Collection
public function models(): EloquentCollection
{
return $this->hits()->map(static function (Hit $hit) {
$models = $this->hits()->map(static function (Hit $hit) {
return $hit->model();
})->filter()->values();

return new EloquentCollection($models);
}

public function documents(): Collection
public function documents(): BaseCollection
{
return $this->hits()->map(static function (Hit $hit) {
return $hit->document();
})->filter()->values();
}

public function highlights(): Collection
public function highlights(): BaseCollection
{
return $this->hits()->map(static function (Hit $hit) {
return $hit->highlight();
Expand Down

0 comments on commit f37a3a4

Please sign in to comment.