|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matchish\ScoutElasticSearch\Searchable; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Matchish\ScoutElasticSearch\Database\Scopes\PageScope; |
|
9
|
|
|
use Matchish\ScoutElasticSearch\ElasticSearch\Index; |
|
10
|
|
|
|
|
11
|
|
|
class DefaultImportSource implements ImportSource |
|
12
|
|
|
{ |
|
13
|
|
|
const DEFAULT_CHUNK_SIZE = 500; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $className; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $scopes; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* DefaultImportSource constructor. |
|
26
|
|
|
* @param string $className |
|
27
|
|
|
* @param array $scopes |
|
28
|
|
|
*/ |
|
29
|
28 |
|
public function __construct(string $className, array $scopes = []) |
|
30
|
|
|
{ |
|
31
|
28 |
|
$this->className = $className; |
|
32
|
28 |
|
$this->scopes = $scopes; |
|
33
|
28 |
|
} |
|
34
|
|
|
|
|
35
|
12 |
|
public function syncWithSearchUsingQueue(): ?string |
|
36
|
|
|
{ |
|
37
|
12 |
|
return $this->model()->syncWithSearchUsingQueue(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
12 |
|
public function syncWithSearchUsing(): ?string |
|
41
|
|
|
{ |
|
42
|
12 |
|
return $this->model()->syncWithSearchUsing(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
20 |
|
public function searchableAs(): string |
|
46
|
|
|
{ |
|
47
|
20 |
|
return $this->model()->searchableAs(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
18 |
|
public function chunked(): Collection |
|
51
|
|
|
{ |
|
52
|
18 |
|
$query = $this->newQuery(); |
|
53
|
18 |
|
$totalSearchables = $query->count(); |
|
54
|
18 |
|
if ($totalSearchables) { |
|
55
|
13 |
|
$chunkSize = $this->getChunkSize(); |
|
56
|
13 |
|
$totalChunks = (int) ceil($totalSearchables / $chunkSize); |
|
57
|
|
|
|
|
58
|
13 |
|
return collect(range(1, $totalChunks))->map(function ($page) use ($chunkSize) { |
|
59
|
13 |
|
$chunkScope = new PageScope($page, $chunkSize); |
|
60
|
|
|
|
|
61
|
13 |
|
return new static($this->className, array_merge($this->scopes, [$chunkScope])); |
|
62
|
13 |
|
}); |
|
63
|
|
|
} else { |
|
64
|
13 |
|
return collect(); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
13 |
|
public function getChunkSize(): int |
|
69
|
|
|
{ |
|
70
|
13 |
|
return (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
18 |
|
public function defineIndex(): Index |
|
74
|
|
|
{ |
|
75
|
18 |
|
$name = $this->searchableAs().'_'.time(); |
|
76
|
18 |
|
$settingsConfigKey = "elasticsearch.indices.settings.{$this->searchableAs()}"; |
|
77
|
18 |
|
$mappingsConfigKey = "elasticsearch.indices.mappings.{$this->searchableAs()}"; |
|
78
|
|
|
$defaultSettings = [ |
|
79
|
18 |
|
'number_of_shards' => 1, |
|
80
|
|
|
'number_of_replicas' => 0, |
|
81
|
|
|
]; |
|
82
|
18 |
|
$settings = config($settingsConfigKey, config('elasticsearch.indices.settings.default', $defaultSettings)); |
|
83
|
18 |
|
$mappings = config($mappingsConfigKey, config('elasticsearch.indices.mappings.default')); |
|
84
|
|
|
|
|
85
|
18 |
|
return new Index($name, $settings, $mappings); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
28 |
|
protected function model() |
|
92
|
|
|
{ |
|
93
|
28 |
|
return new $this->className; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
23 |
|
protected function newQuery(): Builder |
|
97
|
|
|
{ |
|
98
|
23 |
|
$query = $this->model()->newQuery(); |
|
99
|
23 |
|
$softDelete = $this->className::usesSoftDelete() && config('scout.soft_delete', false); |
|
100
|
|
|
$query |
|
101
|
23 |
|
->when($softDelete, function ($query) { |
|
102
|
2 |
|
return $query->withTrashed(); |
|
103
|
23 |
|
}) |
|
104
|
23 |
|
->orderBy($this->model()->getKeyName()); |
|
105
|
23 |
|
$scopes = $this->scopes; |
|
106
|
|
|
|
|
107
|
23 |
|
return collect($scopes)->reduce(function ($instance, $scope) { |
|
108
|
13 |
|
$instance->withGlobalScope(get_class($scope), $scope); |
|
109
|
|
|
|
|
110
|
13 |
|
return $instance; |
|
111
|
23 |
|
}, $query); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
17 |
|
public function get(): EloquentCollection |
|
115
|
|
|
{ |
|
116
|
|
|
/** @var EloquentCollection $models */ |
|
117
|
17 |
|
$models = $this->newQuery()->get(); |
|
118
|
|
|
|
|
119
|
17 |
|
return $models; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|