-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.php
More file actions
237 lines (203 loc) · 7.02 KB
/
Copy pathEngine.php
File metadata and controls
237 lines (203 loc) · 7.02 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php
namespace DirectoryTree\OpenSearchScoutDriver;
use DirectoryTree\OpenSearchAdapter\Documents\DocumentManagerInterface;
use DirectoryTree\OpenSearchAdapter\Indices\IndexBlueprint;
use DirectoryTree\OpenSearchAdapter\Indices\IndexManagerInterface;
use DirectoryTree\OpenSearchAdapter\Search\Hit;
use DirectoryTree\OpenSearchAdapter\Search\SearchResponse;
use DirectoryTree\OpenSearchScoutDriver\Factories\DocumentFactoryInterface;
use DirectoryTree\OpenSearchScoutDriver\Factories\ModelFactoryInterface;
use DirectoryTree\OpenSearchScoutDriver\Factories\SearchRequestFactoryInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\Cursor;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection as BaseCollection;
use Illuminate\Support\LazyCollection;
use InvalidArgumentException;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine as ScoutEngine;
use stdClass;
/**
* Laravel Scout engine backed by OpenSearch.
*/
class Engine extends ScoutEngine
{
/**
* Create a new OpenSearch Scout engine instance.
*/
public function __construct(
protected ModelFactoryInterface $modelFactory,
protected IndexManagerInterface $indexManager,
protected DocumentManagerInterface $documentManager,
protected DocumentFactoryInterface $documentFactory,
protected SearchRequestFactoryInterface $searchRequestFactory,
protected bool $refreshDocuments = false,
) {}
/**
* Update the given models in the index.
*
* @param Collection $models
*/
public function update($models): void
{
if ($models->isEmpty()) {
return;
}
$index = $models->first()->searchableAs();
$documents = $this->documentFactory->makeFromModels($models);
$this->documentManager->index($index, $documents->all(), $this->refreshDocuments);
}
/**
* Delete the given models from the index.
*
* @param Collection $models
*/
public function delete($models): void
{
if ($models->isEmpty()) {
return;
}
$index = $models->first()->searchableAs();
$documentIds = $models->map(fn (Model $model) => (string) $model->getScoutKey())->all();
$this->documentManager->delete($index, $documentIds, $this->refreshDocuments);
}
/**
* Perform the given search.
*/
public function search(Builder $builder): SearchResponse
{
$searchRequest = $this->searchRequestFactory->makeFromBuilder($builder);
return $this->documentManager->search($searchRequest->indexName(), $searchRequest->request());
}
/**
* Perform the given paginated search.
*
* @param int $perPage
* @param int $page
*/
public function paginate(Builder $builder, $perPage, $page): SearchResponse
{
$searchRequest = $this->searchRequestFactory->makeFromBuilder($builder, [
'perPage' => (int) $perPage,
'page' => (int) $page,
]);
return $this->documentManager->search($searchRequest->indexName(), $searchRequest->request());
}
/**
* Cursor paginate the given search using OpenSearch search_after values.
*
* @param int|null $perPage
* @param string $cursorName
* @param Cursor|null $cursor
*/
public function cursorPaginate(Builder $builder, $perPage = null, $cursorName = 'cursor', $cursor = null): CursorPaginator
{
$perPage = (int) ($perPage ?: $builder->model->getPerPage());
$cursor = CursorPaginator::resolveCursor($cursor, $cursorName);
$searchRequest = $this->searchRequestFactory->makeFromBuilder($builder, [
'perPage' => $perPage + 1,
'reversed' => $cursor?->pointsToPreviousItems() ?? false,
'searchAfter' => $cursor?->parameter(CursorPaginator::SEARCH_AFTER_PARAMETER),
]);
if (! $searchRequest->request()->hasSort()) {
throw new InvalidArgumentException('OpenSearch cursor pagination requires at least one explicit sort.');
}
$response = $builder->applyAfterRawSearchCallback(
$this->documentManager->search($searchRequest->indexName(), $searchRequest->request())
);
return new CursorPaginator(
$this->map($builder, $response, $builder->model),
$perPage,
$cursor,
[
'cursorName' => $cursorName,
'path' => Paginator::resolveCurrentPath(),
'parameters' => [CursorPaginator::SEARCH_AFTER_PARAMETER],
'searchAfter' => $this->searchAfterValuesByDocumentId($response),
],
);
}
/**
* Get the primary keys from the search results.
*/
public function mapIds($results): BaseCollection
{
return collect($results->hits())->map(fn (Hit $hit) => $hit->document()->id());
}
/**
* Map the search results to models.
*
* @param SearchResponse $results
* @param Model $model
*/
public function map(Builder $builder, $results, $model): EloquentCollection
{
return $this->modelFactory->makeFromSearchResponse($results, $builder);
}
/**
* Lazily map the search results to models.
*
* @param SearchResponse $results
* @param Model $model
*/
public function lazyMap(Builder $builder, $results, $model): LazyCollection
{
return $this->modelFactory->makeLazyFromSearchResponse($results, $builder);
}
/**
* Get the total count from the search results.
*
* @param SearchResponse $results
*/
public function getTotalCount($results): ?int
{
return $results->total();
}
/**
* Get hit sort values keyed by document ID.
*
* @return array<string, array<int, mixed>>
*/
protected function searchAfterValuesByDocumentId(SearchResponse $response): array
{
$values = [];
foreach ($response->hits() as $hit) {
$values[$hit->document()->id()] = $hit->sort();
}
return $values;
}
/**
* Remove all model records from the index.
*
* @param Model $model
*/
public function flush($model): void
{
$index = $model->searchableAs();
$query = ['match_all' => new stdClass];
$this->documentManager->deleteByQuery($index, $query, $this->refreshDocuments);
}
/**
* Create an index.
*
* @param string $name
*/
public function createIndex($name, array $options = []): void
{
if (isset($options['primaryKey'])) {
throw new InvalidArgumentException('It is not possible to change the primary key name.');
}
$this->indexManager->create(new IndexBlueprint($name));
}
/**
* Delete an index.
*
* @param string $name
*/
public function deleteIndex($name): void
{
$this->indexManager->delete($name);
}
}