Repository navigation
Expand file tree
/
Copy pathSearchable.php
More file actions
313 lines (281 loc) · 7.85 KB
/
Copy pathSearchable.php
File metadata and controls
313 lines (281 loc) · 7.85 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
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
302
303
304
305
306
307
308
309
310
311
312
313
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Scout;
use Closure;
use Hyperf\Context\ApplicationContext;
use Hyperf\Coroutine\Concurrent;
use Hyperf\Coroutine\Coroutine;
use Hyperf\Database\Model\Collection;
use Hyperf\Database\Model\Collection as BaseCollection;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\ModelListener\Collector\ListenerCollector;
use Hyperf\Scout\Engine\Engine;
use function Hyperf\Config\config;
use function Hyperf\Support\class_uses_recursive;
use function Hyperf\Support\make;
trait Searchable
{
/**
* Additional metadata attributes managed by Scout.
*/
protected array $scoutMetadata = [];
protected static ?Concurrent $scoutRunner = null;
/**
* Boot the trait.
*/
public static function bootSearchable(): void
{
static::addGlobalScope(make(SearchableScope::class));
ListenerCollector::register(static::class, ModelObserver::class);
(new static())->registerSearchableMacros();
}
/**
* Register the searchable macros.
*/
public function registerSearchableMacros(): void
{
$self = $this;
BaseCollection::macro('searchable', function () use ($self) {
$self->queueMakeSearchable($this);
});
BaseCollection::macro('unsearchable', function () use ($self) {
$self->queueRemoveFromSearch($this);
});
}
/**
* Dispatch the coroutine to make the given models searchable.
*/
public function queueMakeSearchable(Collection $models): void
{
if ($models->isEmpty()) {
return;
}
$job = function () use ($models) {
$models->first()->searchableUsing()->update($models);
};
self::dispatchSearchableJob($job);
}
/**
* Dispatch the coroutine to make the given models unsearchable.
* @param mixed $models
*/
public function queueRemoveFromSearch($models): void
{
if ($models->isEmpty()) {
return;
}
$job = function () use ($models) {
$models->first()->searchableUsing()->delete($models);
};
self::dispatchSearchableJob($job);
}
/**
* Determine if the model should be searchable.
*/
public function shouldBeSearchable(): bool
{
return true;
}
/**
* Perform a search against the model's indexed data.
*/
public static function search(?string $query = '', ?Closure $callback = null)
{
return make(Builder::class, [
'model' => new static(),
'query' => $query,
'callback' => $callback,
'softDelete' => static::usesSoftDelete() && config('scout.soft_delete', false),
]);
}
/**
* Make all instances of the model searchable.
*/
public static function makeAllSearchable(?int $chunk = null, ?string $column = null): void
{
$self = new static();
$softDelete = static::usesSoftDelete() && config('scout.soft_delete', false);
$self->newQuery()
->when($softDelete, function ($query) {
$query->withTrashed();
})
->orderBy($column ?: $self->getKeyName())
->searchable($chunk, $column);
}
/**
* Make the given model instance searchable.
*/
public function searchable(): void
{
$this->newCollection([$this])->searchable();
}
/**
* Remove all instances of the model from the search index.
*/
public static function removeAllFromSearch(): void
{
$self = new static();
$self->searchableUsing()->flush($self);
}
/**
* Remove the given model instance from the search index.
*/
public function unsearchable(): void
{
$this->newCollection([$this])->unsearchable();
}
/**
* Get the requested models from an array of object IDs.
*/
public function getScoutModelsByIds(Builder $builder, array $ids)
{
$query = static::usesSoftDelete()
? $this->withTrashed() : $this->newQuery();
if ($builder->queryCallback) {
call_user_func($builder->queryCallback, $query);
}
$modelIdPositions = array_flip($ids);
return $query->whereIn(
$this->getScoutKeyName(),
$ids
)->get()->sortBy(static function ($model) use ($modelIdPositions) {
return $modelIdPositions[$model->getScoutKey()];
})->values();
}
/**
* Enable search syncing for this model.
*/
public static function enableSearchSyncing(): void
{
ModelObserver::enableSyncingFor(static::class);
}
/**
* Disable search syncing for this model.
*/
public static function disableSearchSyncing(): void
{
ModelObserver::disableSyncingFor(static::class);
}
/**
* Temporarily disable search syncing for the given callback.
*
* @return mixed
*/
public static function withoutSyncingToSearch(callable $callback)
{
static::disableSearchSyncing();
try {
return $callback();
} finally {
static::enableSearchSyncing();
}
}
/**
* Get the index name for the model.
*/
public function searchableAs(): string
{
return config('scout.prefix') . $this->getTable();
}
/**
* Get the indexable data array for the model.
*/
public function toSearchableArray(): array
{
return $this->toArray();
}
/**
* Get the Scout engine for the model.
*
* @return mixed
*/
public function searchableUsing()
{
return ApplicationContext::getContainer()->get(Engine::class);
}
/**
* Get the concurrency that should be used when syncing.
*/
public function syncWithSearchUsingConcurency(): int
{
return (int) config('scout.concurrency', 100);
}
/**
* Sync the soft deleted status for this model into the metadata.
*
* @return $this
*/
public function pushSoftDeleteMetadata()
{
return $this->withScoutMetadata('__soft_deleted', $this->trashed() ? 1 : 0);
}
/**
* Get all Scout related metadata.
*/
public function scoutMetadata(): array
{
return $this->scoutMetadata;
}
/**
* Set a Scout related metadata.
*
* @param string $key
* @param mixed $value
*/
public function withScoutMetadata($key, $value): static
{
$this->scoutMetadata[$key] = $value;
return $this;
}
/**
* Get the value used to index the model.
*
* @return mixed
*/
public function getScoutKey()
{
return $this->getKey();
}
/**
* Get the key name used to index the model.
*
* @return mixed
*/
public function getScoutKeyName()
{
return $this->getQualifiedKeyName();
}
/**
* Dispatch the coroutine to scout the given models.
*/
protected static function dispatchSearchableJob(callable $job): void
{
if (! Coroutine::inCoroutine()) {
$job();
return;
}
if (defined('SCOUT_COMMAND')) {
if (! static::$scoutRunner instanceof Concurrent) {
static::$scoutRunner = new Concurrent((new static())->syncWithSearchUsingConcurency());
}
self::$scoutRunner->create($job);
} else {
Coroutine::defer($job);
}
}
/**
* Determine if the current class should use soft deletes with searching.
*/
protected static function usesSoftDelete(): bool
{
return in_array(SoftDeletes::class, class_uses_recursive(static::class));
}
}