|
1
|
|
|
<?php |
|
2
|
|
|
namespace EsIndexSwitcher; |
|
3
|
|
|
|
|
4
|
|
|
use Elasticsearch\Client; |
|
5
|
|
|
use DateTime; |
|
6
|
|
|
|
|
7
|
|
|
class EsIndexSwitcher |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
* @var Client |
|
13
|
|
|
*/ |
|
14
|
|
|
private $client; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $alias; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $indexPrefix; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
private $indexSuffix; |
|
33
|
|
|
|
|
34
|
8 |
|
public function __construct(Client $client, string $alias, string $indexPrefix, string $indexSuffix = null) |
|
35
|
|
|
{ |
|
36
|
8 |
|
$this->client = $client; |
|
37
|
|
|
|
|
38
|
8 |
|
$this->alias = $alias; |
|
39
|
|
|
|
|
40
|
8 |
|
if ($indexSuffix === null) { |
|
41
|
7 |
|
$indexSuffix = new DateTime(); |
|
42
|
7 |
|
$indexSuffix = $indexSuffix->format('Y-m-d_H-i-s'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
8 |
|
$this->indexPrefix = $indexPrefix; |
|
46
|
8 |
|
$this->indexSuffix = $indexSuffix; |
|
47
|
8 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* |
|
51
|
|
|
* @return Client |
|
52
|
|
|
*/ |
|
53
|
8 |
|
public function getClient() |
|
54
|
|
|
{ |
|
55
|
8 |
|
return $this->client; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
3 |
|
public function getAlias() |
|
63
|
|
|
{ |
|
64
|
3 |
|
return $this->alias; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
8 |
|
public function getIndexPrefix() |
|
72
|
|
|
{ |
|
73
|
8 |
|
return $this->indexPrefix; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
7 |
|
public function getIndexSuffix() |
|
81
|
|
|
{ |
|
82
|
7 |
|
return $this->indexSuffix; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
7 |
|
public function getNewIndexName() |
|
90
|
|
|
{ |
|
91
|
7 |
|
return $this->getIndexPrefix() . '_' . $this->getIndexSuffix(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $indexBody |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function createNewIndex(array $indexBody = []) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$client = $this->getClient(); |
|
102
|
|
|
|
|
103
|
|
|
$params = [ |
|
104
|
2 |
|
'index' => $this->getNewIndexName(), |
|
105
|
2 |
|
'body' => $indexBody |
|
106
|
|
|
]; |
|
107
|
2 |
|
return $client->indices()->create($params); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function finish() |
|
115
|
|
|
{ |
|
116
|
|
|
/* |
|
117
|
|
|
* Step 2) Update now the alias |
|
118
|
|
|
*/ |
|
119
|
1 |
|
$response = $this->updateAlias(); |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/* |
|
122
|
|
|
* Step 3) list all indices with the given prefix |
|
123
|
|
|
*/ |
|
124
|
1 |
|
$indices = $this->fetchCurrentIndices(); |
|
125
|
|
|
|
|
126
|
|
|
/* |
|
127
|
|
|
* Step 3) Remove old indices |
|
128
|
|
|
*/ |
|
129
|
1 |
|
$response = $this->removeOldIndices($indices); |
|
|
|
|
|
|
130
|
1 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
2 |
|
private function updateAlias() |
|
137
|
|
|
{ |
|
138
|
2 |
|
$client = $this->getClient(); |
|
139
|
|
|
|
|
140
|
|
|
$params = [ |
|
141
|
|
|
'body' => [ |
|
142
|
|
|
'actions' => [ |
|
143
|
|
|
[ |
|
144
|
|
|
'add' => [ |
|
145
|
2 |
|
'index' => $this->getNewIndexName(), |
|
146
|
2 |
|
'alias' => $this->getAlias() |
|
147
|
|
|
] |
|
148
|
|
|
] |
|
149
|
|
|
] |
|
150
|
|
|
] |
|
151
|
|
|
]; |
|
152
|
|
|
|
|
153
|
2 |
|
return $client->indices()->updateAliases($params); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
2 |
|
private function fetchCurrentIndices() |
|
161
|
|
|
{ |
|
162
|
2 |
|
$client = $this->getClient(); |
|
163
|
|
|
|
|
164
|
2 |
|
return $client->cat()->indices([ |
|
165
|
2 |
|
'index' => $this->getIndexPrefix() . '*' |
|
166
|
|
|
]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* |
|
171
|
|
|
* @param array $indices |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
2 |
|
private function removeOldIndices(array $indices) |
|
175
|
|
|
{ |
|
176
|
2 |
|
$client = $this->getClient(); |
|
177
|
|
|
|
|
178
|
2 |
|
$response = []; |
|
179
|
2 |
|
foreach ($indices as $index) { |
|
180
|
|
|
// do not delete the just new created index |
|
181
|
2 |
|
if ($index['index'] === $this->getNewIndexName()) { |
|
182
|
|
|
continue; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// but all other |
|
186
|
2 |
|
$response[$index['index']] = $client->indices()->delete([ |
|
187
|
2 |
|
'index' => $index['index'] |
|
188
|
|
|
]); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
return $response; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.