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