|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matchish\ScoutElasticSearch\Jobs\Stages; |
|
4
|
|
|
|
|
5
|
|
|
use Laravel\Scout\Searchable; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @internal |
|
12
|
|
|
*/ |
|
13
|
|
|
final class PullFromSource |
|
14
|
|
|
{ |
|
15
|
|
|
const DEFAULT_CHUNK_SIZE = 500; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Builder |
|
19
|
|
|
*/ |
|
20
|
|
|
private $query; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param Builder $query |
|
24
|
|
|
*/ |
|
25
|
15 |
|
public function __construct(Builder $query) |
|
26
|
|
|
{ |
|
27
|
15 |
|
$this->query = $query; |
|
28
|
15 |
|
} |
|
29
|
|
|
|
|
30
|
14 |
|
public function handle(): void |
|
31
|
|
|
{ |
|
32
|
14 |
|
$results = $this->query->get(); |
|
33
|
14 |
|
$results->filter->shouldBeSearchable()->searchable(); |
|
34
|
14 |
|
} |
|
35
|
|
|
|
|
36
|
8 |
|
public function estimate(): int |
|
37
|
|
|
{ |
|
38
|
8 |
|
return 1; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
8 |
|
public function title(): string |
|
42
|
|
|
{ |
|
43
|
8 |
|
return 'Indexing...'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Model $searchable |
|
48
|
|
|
* @return Collection |
|
49
|
|
|
*/ |
|
50
|
15 |
|
public static function chunked(Model $searchable): Collection |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var Searchable $searchable */ |
|
53
|
15 |
|
$softDelete = config('scout.soft_delete', false); |
|
54
|
15 |
|
$query = $searchable->newQuery() |
|
|
|
|
|
|
55
|
|
|
->when($softDelete, function ($query) { |
|
56
|
1 |
|
return $query->withTrashed(); |
|
57
|
15 |
|
}) |
|
58
|
15 |
|
->orderBy($searchable->getKeyName()); |
|
|
|
|
|
|
59
|
15 |
|
$totalSearchables = $query->count(); |
|
60
|
15 |
|
if ($totalSearchables) { |
|
61
|
11 |
|
$chunkSize = (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE); |
|
62
|
11 |
|
$totalChunks = (int) ceil($totalSearchables / $chunkSize); |
|
63
|
|
|
|
|
64
|
|
|
return collect(range(1, $totalChunks))->map(function ($page) use ($query, $chunkSize) { |
|
65
|
11 |
|
$clone = (clone $query)->forPage($page, $chunkSize); |
|
66
|
|
|
|
|
67
|
11 |
|
return new static($clone); |
|
68
|
11 |
|
}); |
|
69
|
|
|
} else { |
|
70
|
10 |
|
return collect(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|