1
|
|
|
<?php |
2
|
|
|
namespace Elastica; |
3
|
|
|
|
4
|
|
|
use Elastica\Exception\ResponseException; |
5
|
|
|
use Elasticsearch\Endpoints\Indices\Alias\Get; |
6
|
|
|
use Elasticsearch\Endpoints\Indices\Stats; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Elastica general status. |
10
|
|
|
* |
11
|
|
|
* @author Nicolas Ruflin <[email protected]> |
12
|
|
|
* |
13
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html |
14
|
|
|
*/ |
15
|
|
|
class Status |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Contains all status infos. |
19
|
|
|
* |
20
|
|
|
* @var \Elastica\Response Response object |
21
|
|
|
*/ |
22
|
|
|
protected $_response; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Data. |
26
|
|
|
* |
27
|
|
|
* @var array Data |
28
|
|
|
*/ |
29
|
|
|
protected $_data; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Client object. |
33
|
|
|
* |
34
|
|
|
* @var \Elastica\Client Client object |
35
|
|
|
*/ |
36
|
|
|
protected $_client; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructs Status object. |
40
|
|
|
* |
41
|
|
|
* @param \Elastica\Client $client Client object |
42
|
|
|
*/ |
43
|
|
|
public function __construct(Client $client) |
44
|
|
|
{ |
45
|
|
|
$this->_client = $client; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns status data. |
50
|
|
|
* |
51
|
|
|
* @return array Status data |
52
|
|
|
*/ |
53
|
|
|
public function getData() |
54
|
|
|
{ |
55
|
|
|
if (is_null($this->_data)) { |
56
|
|
|
$this->refresh(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->_data; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns a list of the existing index names. |
64
|
|
|
* |
65
|
|
|
* @return array Index names list |
66
|
|
|
*/ |
67
|
|
|
public function getIndexNames() |
68
|
|
|
{ |
69
|
|
|
$data = $this->getData(); |
70
|
|
|
|
71
|
|
|
return array_keys($data['indices']); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks if the given index exists. |
76
|
|
|
* |
77
|
|
|
* @param string $name Index name to check |
78
|
|
|
* |
79
|
|
|
* @return bool True if index exists |
80
|
|
|
*/ |
81
|
|
|
public function indexExists($name) |
82
|
|
|
{ |
83
|
|
|
return in_array($name, $this->getIndexNames()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Checks if the given alias exists. |
88
|
|
|
* |
89
|
|
|
* @param string $name Alias name |
90
|
|
|
* |
91
|
|
|
* @return bool True if alias exists |
92
|
|
|
*/ |
93
|
|
|
public function aliasExists($name) |
94
|
|
|
{ |
95
|
|
|
return count($this->getIndicesWithAlias($name)) > 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Returns an array with all indices that the given alias name points to. |
100
|
|
|
* |
101
|
|
|
* @param string $alias Alias name |
102
|
|
|
* |
103
|
|
|
* @return array|\Elastica\Index[] List of Elastica\Index |
104
|
|
|
*/ |
105
|
|
|
public function getIndicesWithAlias($alias) |
106
|
|
|
{ |
107
|
|
|
$endpoint = new Get(); |
108
|
|
|
$endpoint->setName($alias); |
109
|
|
|
|
110
|
|
|
$response = null; |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$response = $this->_client->requestEndpoint($endpoint); |
114
|
|
|
} catch (ResponseException $e) { |
115
|
|
|
// 404 means the index alias doesn't exist which means no indexes have it. |
116
|
|
|
if ($e->getResponse()->getStatus() === 404) { |
117
|
|
|
return []; |
118
|
|
|
} |
119
|
|
|
// If we don't have a 404 then this is still unexpected so rethrow the exception. |
120
|
|
|
throw $e; |
121
|
|
|
} |
122
|
|
|
$indices = []; |
123
|
|
|
foreach ($response->getData() as $name => $unused) { |
124
|
|
|
$indices[] = new Index($this->_client, $name); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $indices; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns response object. |
132
|
|
|
* |
133
|
|
|
* @return \Elastica\Response Response object |
134
|
|
|
*/ |
135
|
|
|
public function getResponse() |
136
|
|
|
{ |
137
|
|
|
if (is_null($this->_response)) { |
138
|
|
|
$this->refresh(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->_response; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return shards info. |
146
|
|
|
* |
147
|
|
|
* @return array Shards info |
148
|
|
|
*/ |
149
|
|
|
public function getShards() |
150
|
|
|
{ |
151
|
|
|
$data = $this->getData(); |
152
|
|
|
|
153
|
|
|
return $data['shards']; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Refresh status object. |
158
|
|
|
*/ |
159
|
|
|
public function refresh() |
160
|
|
|
{ |
161
|
|
|
$this->_response = $this->_client->requestEndpoint(new Stats()); |
162
|
|
|
$this->_data = $this->getResponse()->getData(); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|