1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es; |
3
|
|
|
|
4
|
|
|
use Triadev\Es\Business\AbstractElasticsearch; |
5
|
|
|
use Triadev\Es\Contract\ElasticsearchAliasContract; |
6
|
|
|
use Triadev\Es\Exception\Alias\AliasFoundException; |
7
|
|
|
use Triadev\Es\Exception\Alias\AliasNotFoundException; |
8
|
|
|
|
9
|
|
|
class ElasticsearchAlias extends AbstractElasticsearch implements ElasticsearchAliasContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Add an alias |
13
|
|
|
* |
14
|
|
|
* @param string $index |
15
|
|
|
* @param string $alias |
16
|
|
|
* @param string|null $version |
17
|
|
|
* @return array |
18
|
|
|
* @throws AliasFoundException |
19
|
|
|
*/ |
20
|
3 |
|
public function addAlias(string $index, string $alias, string $version = null) : array |
21
|
|
|
{ |
22
|
3 |
|
if (!$this->existAlias([$index], [$alias], $version)) { |
23
|
3 |
|
return $this->getElasticsearchClient()->indices()->putAlias([ |
24
|
3 |
|
'index' => $this->createIndexWithVersion($index, $version), |
25
|
3 |
|
'name' => $alias |
26
|
|
|
]); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
throw new AliasFoundException($index, $alias, $version); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Delete an alias |
34
|
|
|
* |
35
|
|
|
* @param string $index |
36
|
|
|
* @param string $alias |
37
|
|
|
* @param string|null $version |
38
|
|
|
* @return array |
39
|
|
|
* @throws AliasNotFoundException |
40
|
|
|
*/ |
41
|
2 |
|
public function deleteAlias(string $index, string $alias, string $version = null) : array |
42
|
|
|
{ |
43
|
2 |
|
if ($this->existAlias([$index], [$alias], $version)) { |
44
|
2 |
|
return $this->getElasticsearchClient()->indices()->deleteAlias([ |
45
|
2 |
|
'index' => $this->createIndexWithVersion($index, $version), |
46
|
2 |
|
'name' => $alias |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new AliasNotFoundException($index, $alias, $version); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Exist alias |
55
|
|
|
* |
56
|
|
|
* @param array $index |
57
|
|
|
* @param array $alias |
58
|
|
|
* @param string|null $version |
59
|
|
|
* @param array $params |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
4 |
|
public function existAlias(array $index, array $alias, ?string $version = null, array $params = []) : bool |
63
|
|
|
{ |
64
|
4 |
|
$params['name'] = implode(',', $alias); |
65
|
|
|
|
66
|
4 |
|
$indices = []; |
67
|
4 |
|
foreach ($index as $i) { |
68
|
4 |
|
$indices[] = $this->createIndexWithVersion($i, $version); |
69
|
|
|
} |
70
|
4 |
|
$params['index'] = implode(',', $indices); |
71
|
|
|
|
72
|
4 |
|
return $this->getElasticsearchClient()->indices()->existsAlias($params); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|