1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Matchish\ScoutElasticSearch\Console\Commands; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class DefaultImportSource implements ImportSource |
8
|
|
|
{ |
9
|
|
|
const DEFAULT_CHUNK_SIZE = 500; |
10
|
|
|
|
11
|
|
|
private $className; |
12
|
|
|
private $scopes; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* DefaultImportSource constructor. |
16
|
|
|
* @param string $className |
17
|
|
|
* @param array $scopes |
18
|
|
|
*/ |
19
|
25 |
|
public function __construct(string $className, array $scopes = []) |
20
|
|
|
{ |
21
|
25 |
|
$this->className = $className; |
22
|
25 |
|
$this->scopes = $scopes; |
23
|
25 |
|
} |
24
|
|
|
|
25
|
11 |
|
public function syncWithSearchUsingQueue() |
26
|
|
|
{ |
27
|
11 |
|
return $this->model()->syncWithSearchUsingQueue(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
11 |
|
public function syncWithSearchUsing() |
32
|
|
|
{ |
33
|
11 |
|
return $this->model()->syncWithSearchUsing(); |
34
|
|
|
} |
35
|
|
|
|
36
|
18 |
|
public function searchableAs(): string |
37
|
|
|
{ |
38
|
18 |
|
return $this->model()->searchableAs(); |
39
|
|
|
} |
40
|
|
|
|
41
|
17 |
|
public function chunked() |
42
|
|
|
{ |
43
|
17 |
|
$query = $this->newQuery(); |
44
|
17 |
|
$searchable = $this->model(); |
45
|
17 |
|
$totalSearchables = $query->count(); |
46
|
17 |
|
if ($totalSearchables) { |
47
|
12 |
|
$chunkSize = (int)config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE); |
48
|
12 |
|
$cloneQuery = clone $query; |
49
|
12 |
|
$cloneQuery->joinSub('SELECT @row :=0, 1 as temp', 'r', 'r.temp', 'r.temp') |
50
|
12 |
|
->selectRaw("@row := @row +1 AS rownum, {$searchable->getKeyName()}"); |
51
|
12 |
|
$ids = \DB::query()->fromSub($cloneQuery, 'ranked')->whereRaw("rownum %{$chunkSize} =0")->pluck('id'); |
52
|
12 |
|
$pairs = []; |
53
|
12 |
|
$lastId = null; |
54
|
12 |
|
foreach ($ids as $id) { |
55
|
11 |
|
if ($lastId) { |
56
|
6 |
|
$pairs[] = [$lastId, $id]; |
57
|
|
|
} else { |
58
|
11 |
|
$pairs[] = [null, $id]; |
59
|
|
|
} |
60
|
11 |
|
$lastId = $id; |
61
|
|
|
} |
62
|
12 |
|
$pairs[] = [$lastId, null]; |
63
|
|
|
return collect($pairs)->map(function ($pair) { |
64
|
12 |
|
list($start, $end) = $pair; |
65
|
12 |
|
$chunkScope = new ChunkScope($start, $end); |
66
|
12 |
|
return new static($this->className, [$chunkScope]); |
67
|
12 |
|
}); |
68
|
|
|
} else { |
69
|
12 |
|
return collect(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
25 |
|
private function model() |
78
|
|
|
{ |
79
|
25 |
|
return new $this->className; |
80
|
|
|
} |
81
|
|
|
|
82
|
21 |
|
private function newQuery() |
83
|
|
|
{ |
84
|
21 |
|
$query = $this->model()->newQuery(); |
85
|
21 |
|
$softDelete = config('scout.soft_delete', false); |
86
|
|
|
$query |
87
|
|
|
->when($softDelete, function ($query) { |
88
|
2 |
|
return $query->withTrashed(); |
89
|
21 |
|
}) |
90
|
21 |
|
->orderBy($this->model()->getKeyName()); |
91
|
21 |
|
$scopes = $this->scopes; |
92
|
|
|
return collect($scopes)->reduce(function ($instance, $scope) { |
93
|
11 |
|
$instance->withGlobalScope($scope->key(), $scope); |
94
|
11 |
|
return $instance; |
95
|
21 |
|
}, $query); |
96
|
|
|
} |
97
|
|
|
|
98
|
15 |
|
public function get() |
99
|
|
|
{ |
100
|
15 |
|
return $this->newQuery()->get(); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|