1
|
|
|
<?php |
2
|
|
|
namespace Triadev\Es; |
3
|
|
|
|
4
|
|
|
use Triadev\Es\Business\AbstractElasticsearch; |
5
|
|
|
use Triadev\Es\Contract\ElasticsearchIndexContract; |
6
|
|
|
use Triadev\Es\Exception\Index\IndexFoundException; |
7
|
|
|
use Triadev\Es\Exception\Index\IndexNotFoundException; |
8
|
|
|
|
9
|
|
|
class ElasticsearchIndex extends AbstractElasticsearch implements ElasticsearchIndexContract |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Create index |
13
|
|
|
* |
14
|
|
|
* @param string $index |
15
|
|
|
* @param array $params |
16
|
|
|
* @param string|null $version |
17
|
|
|
* @return array |
18
|
|
|
* @throws IndexFoundException |
19
|
|
|
*/ |
20
|
19 |
|
public function createIndex(string $index, array $params, string $version = null) : array |
21
|
|
|
{ |
22
|
19 |
|
if (!$this->existIndex([$index], $version)) { |
23
|
16 |
|
$params['index'] = $this->createIndexWithVersion($index, $version); |
24
|
|
|
|
25
|
16 |
|
return $this->getElasticsearchClient()->indices()->create($params); |
26
|
|
|
} |
27
|
|
|
|
28
|
4 |
|
throw new IndexFoundException($index, $version); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Delete index |
33
|
|
|
* |
34
|
|
|
* @param array $index |
35
|
|
|
* @param string|null $version |
36
|
|
|
* @return array |
37
|
|
|
* @throws IndexNotFoundException |
38
|
|
|
*/ |
39
|
1 |
|
public function deleteIndex(array $index, string $version = null) : array |
40
|
|
|
{ |
41
|
1 |
|
$indices = []; |
42
|
|
|
|
43
|
1 |
|
foreach ($index as $i) { |
44
|
1 |
|
if ($this->existIndex([$i], $version)) { |
45
|
1 |
|
$indices[] = $this->createIndexWithVersion($i, $version); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
if (!empty($indices)) { |
50
|
1 |
|
return $this->getElasticsearchClient()->indices()->delete([ |
51
|
1 |
|
'index' => implode(',', $indices) |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
throw new IndexNotFoundException(implode(',', $index), $version); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Delete all indexes |
60
|
|
|
* |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
14 |
|
public function deleteAllIndexes() : array |
64
|
|
|
{ |
65
|
14 |
|
return $this->getElasticsearchClient()->indices()->delete([ |
66
|
14 |
|
'index' => '_all' |
67
|
|
|
]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Exist index |
72
|
|
|
* |
73
|
|
|
* @param array $index |
74
|
|
|
* @param string|null $version |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
19 |
|
public function existIndex(array $index, ?string $version = null) : bool |
78
|
|
|
{ |
79
|
19 |
|
$indices = []; |
80
|
|
|
|
81
|
19 |
|
foreach ($index as $i) { |
82
|
19 |
|
$indices[] = $this->createIndexWithVersion($i, $version); |
83
|
|
|
} |
84
|
|
|
|
85
|
19 |
|
return $this->getElasticsearchClient()->indices()->exists([ |
86
|
19 |
|
'index' => implode(',', $indices) |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get versioned indices |
92
|
|
|
* |
93
|
|
|
* @param string $index |
94
|
|
|
* @return array |
95
|
|
|
*/ |
96
|
2 |
|
public function getVersionedIndices(string $index) : array |
97
|
|
|
{ |
98
|
2 |
|
return array_keys($this->getElasticsearchClient()->indices()->get([ |
|
|
|
|
99
|
2 |
|
'index' => sprintf("%s_*", $index) |
100
|
|
|
])); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Reindex |
105
|
|
|
* |
106
|
|
|
* @param string $index |
107
|
|
|
* @param string $from_version |
108
|
|
|
* @param string $to_version |
109
|
|
|
* @param array $params |
110
|
|
|
* @param array|null $source |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function reindex( |
114
|
|
|
string $index, |
115
|
|
|
string $from_version, |
116
|
|
|
string $to_version, |
117
|
|
|
array $params = [], |
118
|
|
|
?array $source = null |
119
|
|
|
) : array { |
120
|
|
|
$params['body'] = [ |
121
|
|
|
'source' => [ |
122
|
|
|
'index' => $this->createIndexWithVersion($index, $from_version) |
123
|
|
|
], |
124
|
|
|
'dest' => [ |
125
|
|
|
'index' => $this->createIndexWithVersion($index, $to_version) |
126
|
|
|
] |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
if (is_array($source)) { |
130
|
|
|
$params['body']['source']['_source'] = $source; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->getElasticsearchClient()->reindex($params); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|