-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelFactory.php
More file actions
90 lines (76 loc) · 2.95 KB
/
Copy pathModelFactory.php
File metadata and controls
90 lines (76 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace DirectoryTree\OpenSearchScoutDriver\Factories;
use DirectoryTree\OpenSearchAdapter\Search\Hit;
use DirectoryTree\OpenSearchAdapter\Search\SearchResponse;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as SupportCollection;
use Illuminate\Support\LazyCollection;
use Laravel\Scout\Builder;
/**
* Creates Scout model results from OpenSearch responses.
*/
class ModelFactory implements ModelFactoryInterface
{
/**
* Create an Eloquent collection from an OpenSearch response.
*/
public function makeFromSearchResponse(SearchResponse $searchResponse, Builder $builder): EloquentCollection
{
$documentIds = $this->pluckDocumentIds($searchResponse);
if (empty($documentIds)) {
return $builder->model->newCollection();
}
/** @var EloquentCollection $models */
$models = $builder->model->getScoutModelsByIds($builder, $documentIds);
return $this->sortModels($this->filterModels($models, $documentIds), $documentIds);
}
/**
* Lazily create an Eloquent collection from an OpenSearch response.
*/
public function makeLazyFromSearchResponse(SearchResponse $searchResponse, Builder $builder): LazyCollection
{
$documentIds = $this->pluckDocumentIds($searchResponse);
if (empty($documentIds)) {
return LazyCollection::make($builder->model->newCollection());
}
/** @var LazyCollection $models */
$models = $builder->model->queryScoutModelsByIds($builder, $documentIds)->cursor();
return $this->sortModels($this->filterModels($models, $documentIds), $documentIds);
}
/**
* Get document IDs from the OpenSearch hits.
*
* @return array<int, string>
*/
protected function pluckDocumentIds(SearchResponse $searchResponse): array
{
return array_map(
fn (Hit $hit) => $hit->document()->id(),
$searchResponse->hits()
);
}
/**
* Sort models into the same order as the OpenSearch response.
*
* @param array<int, string> $documentIds
*/
protected function sortModels(EloquentCollection|LazyCollection $models, array $documentIds): SupportCollection|EloquentCollection|LazyCollection
{
$documentIdPositions = array_flip($documentIds);
return $models->sortBy(
fn (Model $model) => $documentIdPositions[(string) $model->getScoutKey()]
)->values();
}
/**
* Remove models that are no longer present in the database.
*
* @param array<int, string> $documentIds
*/
protected function filterModels(EloquentCollection|LazyCollection $models, array $documentIds): SupportCollection|EloquentCollection|LazyCollection
{
return $models->filter(
fn (Model $model) => in_array((string) $model->getScoutKey(), $documentIds, true)
)->values();
}
}