-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathArrayEngine.php
301 lines (264 loc) · 8.47 KB
/
ArrayEngine.php
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
namespace Sti3bas\ScoutArray\Engines;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Illuminate\Support\LazyCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
use Sti3bas\ScoutArray\ArrayStore;
class ArrayEngine extends Engine
{
/**
* @var ArrayStore
*/
public $store;
/**
* Determines if soft deletes for Scout are enabled or not.
*
* @var bool
*/
protected $softDelete;
public function __construct($store, $softDelete = false)
{
$this->store = $store;
$this->softDelete = $softDelete;
}
/**
* Update the given model in the index.
*
* @param Collection $models
* @return void
*/
public function update($models)
{
if ($this->usesSoftDelete($models->first()) && $this->softDelete) {
$models->each->pushSoftDeleteMetadata();
}
$models->each(function ($model) {
if (empty($searchableData = $model->toSearchableArray())) {
return;
}
$this->store->set($model->searchableAs(), $model->getScoutKey(), array_merge(
$searchableData,
$model->scoutMetadata()
));
});
}
/**
* Remove the given model from the index.
*
* @param Collection $models
* @return void
*/
public function delete($models)
{
$models->each(function ($model) {
$this->store->forget($model->searchableAs(), $model->getScoutKey());
});
}
/**
* Perform the given search on the engine.
*
*
* @return mixed
*/
public function search(Builder $builder)
{
return $this->performSearch($builder, [
'perPage' => $builder->limit,
]);
}
/**
* Perform the given search on the engine.
*
* @param int $perPage
* @param int $page
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
{
return $this->performSearch($builder, [
'perPage' => $perPage,
'page' => $page,
]);
}
/**
* Perform the given search on the engine.
*
* @return array
*/
protected function performSearch(Builder $builder, array $options = [])
{
$index = $builder->index ?: $builder->model->searchableAs();
$matches = $this->store->find($index, function ($record) use ($builder) {
$values = new RecursiveIteratorIterator(new RecursiveArrayIterator($record));
return $this->matchesFilters($record, $builder->wheres) &&
$this->matchesFilters($record, $builder->whereIns) &&
$this->matchesFilters($record, data_get($builder, 'whereNotIns', []), true) &&
!empty(array_filter(iterator_to_array($values, false), function ($value) use ($builder) {
return !$builder->query || stripos((string) $value, $builder->query) !== false;
}));
}, true);
$matches = Collection::make($matches);
return [
'hits' => (isset($options['perPage']) ? $matches->slice((($options['page'] ?? 1) - 1) * $options['perPage'], $options['perPage']) : $matches)->values()->all(),
'total' => $matches->count(),
];
}
/**
* Determine if the given record matches given filters.
*
* @param array $record
* @param array $filters
* @param bool $not
* @return bool
*/
private function matchesFilters($record, $filters, $not = false)
{
if (empty($filters)) {
return true;
}
$match = function ($record, $key, $value) {
$recordValue = data_get($record, $key);
if (is_array($value)) {
if (is_array($recordValue)) {
return count(array_intersect($recordValue, $value)) > 0;
}
return in_array($recordValue, $value, true);
}
return $recordValue === $value;
};
$match = Collection::make($filters)->every(function ($value, $key) use ($match, $record) {
$keyExploded = explode('.', $key);
if (count($keyExploded) > 1) {
if (data_get($record, $keyExploded[0]) instanceof Collection) {
return data_get($record, $keyExploded[0])->contains(function ($subRecord) use ($match, $keyExploded, $value) {
return $match($subRecord, $keyExploded[1], $value);
});
}
return $match($record, $keyExploded, $value);
}
return $match($record, $key, $value);
});
return $not ? !$match : $match;
}
/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
{
return Collection::make($results['hits'])->pluck('objectID')->values();
}
/**
* Map the given results to instances of the given model.
*
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return Collection
*/
public function map(Builder $builder, $results, $model)
{
if (count($results['hits']) === 0) {
return $model->newCollection();
}
$objectIds = Collection::make($results['hits'])->pluck('objectID')->values()->all();
$objectIdPositions = array_flip($objectIds);
return $model->getScoutModelsByIds($builder, $objectIds)
->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
}
/**
* Map the given results to instances of the given model via a lazy collection.
*
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Support\LazyCollection
*/
public function lazyMap(Builder $builder, $results, $model)
{
if (count($results['hits']) === 0) {
return LazyCollection::make($model->newCollection());
}
$objectIds = Collection::make($results['hits'])->pluck('objectID')->values()->all();
$objectIdPositions = array_flip($objectIds);
return $model->queryScoutModelsByIds(
$builder,
$objectIds
)->cursor()->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
})->sortBy(function ($model) use ($objectIdPositions) {
return $objectIdPositions[$model->getScoutKey()];
})->values();
}
/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @return int
*/
public function getTotalCount($results)
{
return $results['total'];
}
/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function flush($model)
{
$this->store->flush($model->searchableAs());
}
/**
* Create a search index.
*
* @param string $name
* @return mixed
*/
public function createIndex($name, array $options = [])
{
$this->store->createIndex($name);
}
/**
* Delete a search index.
*
* @param string $name
* @return mixed
*/
public function deleteIndex($name)
{
$this->store->deleteIndex($name);
}
/**
* Determine if the given model uses soft deletes.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
protected function usesSoftDelete($model)
{
return in_array(SoftDeletes::class, class_uses_recursive($model));
}
protected function buildSearchQuery(Builder $builder)
{
$query = $this->initializeSearchQuery(
$builder,
array_keys($builder->model->toSearchableArray()),
$this->getPrefixColumns($builder),
$this->getFullTextColumns($builder)
);
return $this->constrainForSoftDeletes(
$builder,
$this->addAdditionalConstraints($builder, $query->take($builder->limit))
);
}
}