Skip to content

Commit

Permalink
Map inner hits into the Hit decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
babenkoivan committed Sep 27, 2022
1 parent 9aa343a commit ccc5bba
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/Decorators/Hit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastic\ScoutDriverPlus\Factories\LazyModelFactory;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\Traits\ForwardsCalls;

/**
Expand All @@ -32,6 +33,15 @@ public function model(): ?Model
);
}

public function innerHits(): BaseCollection
{
return $this->baseHit->innerHits()->map(
fn (BaseCollection $baseInnerHits) => $baseInnerHits->map(
fn (BaseHit $baseInnerHit) => new self($baseInnerHit, $this->lazyModelFactory)
)
);
}

/**
* @return mixed
*/
Expand Down
15 changes: 11 additions & 4 deletions src/Factories/LazyModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
use Elastic\Adapter\Search\Hit as BaseHit;
use Elastic\Adapter\Search\SearchResult as BaseSearchResult;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;

class LazyModelFactory
{
private array $documentIds;
private array $documentIds = [];
private ModelFactory $modelFactory;
private array $models = [];

public function __construct(BaseSearchResult $searchResult, ModelFactory $modelFactory)
{
$this->documentIds = $searchResult->hits()->mapToGroups(
static fn (BaseHit $baseHit) => [$baseHit->indexName() => $baseHit->document()->id()]
)->toArray();
$searchResult->hits()->each(function (BaseHit $baseHit) {
$this->documentIds[$baseHit->indexName()][] = $baseHit->document()->id();

$baseHit->innerHits()->each(function (BaseCollection $baseInnerHits) {
$baseInnerHits->each(function (BaseHit $baseInnerHit) {
$this->documentIds[$baseInnerHit->indexName()][] = $baseInnerHit->document()->id();
});
});
});

$this->modelFactory = $modelFactory;
}
Expand Down
14 changes: 12 additions & 2 deletions tests/Integration/Queries/RawQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,21 @@ public function test_models_can_be_found_using_raw_field_collapsing(): void

// find the cheapest books by author
$found = Book::searchQuery(['match_all' => new stdClass()])
->collapseRaw(['field' => 'author_id'])
->sort('price', 'asc')
->collapseRaw([
'field' => 'author_id',
'inner_hits' => [
'name' => 'cheapest',
'size' => 5,
'sort' => [['price' => 'asc']],
],
])
->sort('price')
->execute();

$this->assertFoundModels(collect([$firstTarget, $secondTarget]), $found);

$this->assertCount(5, $found->hits()->first()->innerHits()->get('cheapest')->map->model());
$this->assertCount(1, $found->hits()->last()->innerHits()->get('cheapest')->map->model());
}

public function test_models_can_be_found_using_field_collapsing(): void
Expand Down

0 comments on commit ccc5bba

Please sign in to comment.