1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/elasticsearch7-adapter project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\Elasticsearch7\Migration; |
10
|
|
|
|
11
|
|
|
use Daikon\Dbal\Exception\DbalException; |
12
|
|
|
use Daikon\Dbal\Migration\Migration; |
13
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
14
|
|
|
|
15
|
|
|
abstract class Elasticsearch7Migration extends Migration |
16
|
|
|
{ |
17
|
|
|
protected function createIndex(string $index, array $settings = []): void |
18
|
|
|
{ |
19
|
|
|
$indices = $this->connector->getConnection()->indices(); |
20
|
|
|
|
21
|
|
|
if ($this->indexExists($index)) { |
22
|
|
|
throw new DbalException("Cannot create already existing index '$index'."); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$indices->create(['index' => $index, 'body' => $settings]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
protected function createAlias(string $index, string $alias): void |
29
|
|
|
{ |
30
|
|
|
$indices = $this->connector->getConnection()->indices(); |
31
|
|
|
$indices->updateAliases([ |
32
|
|
|
'body' => [ |
33
|
|
|
'actions' => [['add' => ['index' => $index, 'alias' => $alias]]] |
34
|
|
|
] |
35
|
|
|
]); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
protected function reassignAlias(string $index, string $alias): void |
39
|
|
|
{ |
40
|
|
|
$currentIndices = $this->getIndicesWithAlias($alias); |
41
|
|
|
if (count($currentIndices) !== 1) { |
42
|
|
|
throw new DbalException( |
43
|
|
|
"Cannot reassign alias '$alias' since it is not assigned to exactly one index." |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$indices = $this->connector->getConnection()->indices(); |
48
|
|
|
$indices->updateAliases([ |
49
|
|
|
'body' => [ |
50
|
|
|
'actions' => [ |
51
|
|
|
['remove' => ['index' => current($currentIndices), 'alias' => $alias]], |
52
|
|
|
['add' => ['index' => $index, 'alias' => $alias]] |
53
|
|
|
] |
54
|
|
|
] |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function deleteIndex(string $index): void |
59
|
|
|
{ |
60
|
|
|
$indices = $this->connector->getConnection()->indices(); |
61
|
|
|
|
62
|
|
|
if (!$this->indexExists($index)) { |
63
|
|
|
throw new DbalException("Cannot delete non-existing index '$index'."); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$indices->delete(['index' => $index]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function putMapping(string $index, array $mapping): void |
70
|
|
|
{ |
71
|
|
|
$indices = $this->connector->getConnection()->indices(); |
72
|
|
|
$indices->putMapping(['index' => $index, 'body' => $mapping]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function reindexWithMapping(string $source, string $dest, array $mapping): void |
76
|
|
|
{ |
77
|
|
|
$settings = $this->getIndexSettings($source); |
78
|
|
|
$mappings = ['mappings' => $mapping]; |
79
|
|
|
$this->createIndex($dest, array_merge($settings, $mappings)); |
80
|
|
|
$this->reindex($source, $dest); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function reindex(string $source, string $dest): void |
84
|
|
|
{ |
85
|
|
|
$client = $this->connector->getConnection(); |
86
|
|
|
$client->reindex([ |
87
|
|
|
'body' => [ |
88
|
|
|
'source' => ['index' => $source], |
89
|
|
|
'dest' => ['index' => $dest, 'version_type' => 'external'] |
90
|
|
|
] |
91
|
|
|
]); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
protected function getIndexSettings(string $index): array |
95
|
|
|
{ |
96
|
|
|
$indices = $this->connector->getConnection()->indices(); |
97
|
|
|
$settings = current($indices->getSettings(['index' => $index])); |
98
|
|
|
// have to remove info settings to create new index.. |
99
|
|
|
unset($settings['settings']['index']['uuid']); |
100
|
|
|
unset($settings['settings']['index']['version']); |
101
|
|
|
unset($settings['settings']['index']['creation_date']); |
102
|
|
|
unset($settings['settings']['index']['provided_name']); |
103
|
|
|
return $settings; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getIndicesWithAlias(string $alias): array |
107
|
|
|
{ |
108
|
|
|
$indices = $this->connector->getConnection()->indices(); |
109
|
|
|
|
110
|
|
|
try { |
111
|
|
|
$indexNames = array_keys($indices->getAlias(['name' => $alias])); |
112
|
|
|
} catch (Missing404Exception $error) { |
113
|
|
|
$indexNames = []; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $indexNames; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
protected function indexExists(string $index): bool |
120
|
|
|
{ |
121
|
|
|
$indices = $this->connector->getConnection()->indices(); |
122
|
|
|
return $indices->exists(['index' => $index]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function getIndexPrefix(): string |
126
|
|
|
{ |
127
|
|
|
return $this->connector->getSettings()['index_prefix']; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|