forked from ruflin/Elastica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCluster.php
147 lines (129 loc) · 2.94 KB
/
Cluster.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Elastica;
use Elastica\Cluster\Health;
use Elastica\Cluster\Settings;
use Elastica\Exception\ClientException;
use Elastica\Exception\ConnectionException;
use Elastica\Exception\ResponseException;
use Elasticsearch\Endpoints\Cluster\State;
/**
* Cluster information for elasticsearch.
*
* @author Nicolas Ruflin <[email protected]>
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html
*/
class Cluster
{
/**
* Client.
*
* @var Client Client object
*/
protected $_client;
/**
* Cluster state response.
*
* @var Response
*/
protected $_response;
/**
* Cluster state data.
*
* @var array
*/
protected $_data;
/**
* Creates a cluster object.
*/
public function __construct(Client $client)
{
$this->_client = $client;
$this->refresh();
}
/**
* Refreshes all cluster information (state).
*
* @throws ClientException
* @throws ConnectionException
* @throws ResponseException
*/
public function refresh(): void
{
$this->_response = $this->_client->requestEndpoint(new State());
$this->_data = $this->getResponse()->getData();
}
/**
* Returns the response object.
*/
public function getResponse(): Response
{
return $this->_response;
}
/**
* Return list of index names.
*
* @return string[]
*/
public function getIndexNames(): array
{
return \array_keys($this->_data['metadata']['indices']);
}
/**
* Returns the full state of the cluster.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
*/
public function getState(): array
{
return $this->_data;
}
/**
* Returns a list of existing node names.
*
* @return string[]
*/
public function getNodeNames(): array
{
$data = $this->getState();
$nodeNames = [];
foreach ($data['nodes'] as $node) {
$nodeNames[] = $node['name'];
}
return $nodeNames;
}
/**
* Returns all nodes of the cluster.
*
* @return Node[]
*/
public function getNodes(): array
{
$nodes = [];
$data = $this->getState();
foreach ($data['nodes'] as $id => $name) {
$nodes[] = new Node($id, $this->getClient());
}
return $nodes;
}
public function getClient(): Client
{
return $this->_client;
}
/**
* Return Cluster health.
*
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
*/
public function getHealth(): Health
{
return new Health($this->getClient());
}
/**
* Return Cluster settings.
*/
public function getSettings(): Settings
{
return new Settings($this->getClient());
}
}