1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Matchish\ScoutElasticSearch\ElasticSearch; |
4
|
|
|
|
5
|
|
|
use Matchish\ScoutElasticSearch\Searchable\ImportSource; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @internal |
9
|
|
|
*/ |
10
|
|
|
final class Index |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $aliases = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $name; |
21
|
|
|
/** |
22
|
|
|
* @var array|null |
23
|
|
|
*/ |
24
|
|
|
private $settings; |
25
|
|
|
/** |
26
|
|
|
* @var array|null |
27
|
|
|
*/ |
28
|
|
|
private $mappings; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Index constructor. |
32
|
|
|
* @param string $name |
33
|
|
|
* @param array $settings |
34
|
|
|
* @param array $mappings |
35
|
|
|
*/ |
36
|
20 |
|
public function __construct(string $name, array $settings = null, array $mappings = null) |
37
|
|
|
{ |
38
|
20 |
|
$this->name = $name; |
39
|
20 |
|
$this->settings = $settings; |
40
|
20 |
|
$this->mappings = $mappings; |
41
|
20 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
15 |
|
public function aliases(): array |
47
|
|
|
{ |
48
|
15 |
|
return $this->aliases; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
17 |
|
public function name(): string |
55
|
|
|
{ |
56
|
17 |
|
return $this->name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Alias $alias |
61
|
|
|
*/ |
62
|
14 |
|
public function addAlias(Alias $alias): void |
63
|
|
|
{ |
64
|
14 |
|
$this->aliases[$alias->name()] = $alias->config() ?: new \stdClass(); |
65
|
14 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
15 |
|
public function config(): array |
71
|
|
|
{ |
72
|
15 |
|
$config = []; |
73
|
15 |
|
if (! empty($this->settings)) { |
74
|
15 |
|
$config['settings'] = $this->settings; |
75
|
|
|
} |
76
|
15 |
|
if (! empty($this->mappings)) { |
77
|
15 |
|
$config['mappings'] = $this->mappings; |
78
|
|
|
} |
79
|
15 |
|
if (! empty($this->aliases())) { |
80
|
14 |
|
$config['aliases'] = $this->aliases(); |
81
|
|
|
} |
82
|
|
|
|
83
|
15 |
|
return $config; |
84
|
|
|
} |
85
|
|
|
|
86
|
18 |
|
public static function fromSource(ImportSource $source): Index |
87
|
|
|
{ |
88
|
18 |
|
$name = $source->searchableAs().'_'.time(); |
89
|
18 |
|
$settingsConfigKey = "elasticsearch.indices.settings.{$source->searchableAs()}"; |
90
|
18 |
|
$mappingsConfigKey = "elasticsearch.indices.mappings.{$source->searchableAs()}"; |
91
|
|
|
$defaultSettings = [ |
92
|
18 |
|
'number_of_shards' => 1, |
93
|
|
|
'number_of_replicas' => 0, |
94
|
|
|
|
95
|
|
|
]; |
96
|
18 |
|
$settings = config($settingsConfigKey, config('elasticsearch.indices.settings.default', $defaultSettings)); |
97
|
18 |
|
$mappings = config($mappingsConfigKey, config('elasticsearch.indices.mappings.default')); |
98
|
|
|
|
99
|
18 |
|
return new static($name, $settings, $mappings); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|