| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- <?php
- namespace Elastica;
- use Elastica\Cluster\Health;
- use Elastica\Cluster\Settings;
- use Elastica\Exception\NotImplementedException;
- use Elasticsearch\Endpoints\Cluster\State;
- /**
- * Cluster information for elasticsearch.
- *
- * @author Nicolas Ruflin <spam@ruflin.com>
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster.html
- */
- class Cluster
- {
- /**
- * Client.
- *
- * @var \Elastica\Client Client object
- */
- protected $_client;
- /**
- * Cluster state response.
- *
- * @var \Elastica\Response
- */
- protected $_response;
- /**
- * Cluster state data.
- *
- * @var array
- */
- protected $_data;
- /**
- * Creates a cluster object.
- *
- * @param \Elastica\Client $client Connection client object
- */
- public function __construct(Client $client)
- {
- $this->_client = $client;
- $this->refresh();
- }
- /**
- * Refreshes all cluster information (state).
- */
- public function refresh()
- {
- $this->_response = $this->_client->requestEndpoint(new State());
- $this->_data = $this->getResponse()->getData();
- }
- /**
- * Returns the response object.
- *
- * @return \Elastica\Response Response object
- */
- public function getResponse()
- {
- return $this->_response;
- }
- /**
- * Return list of index names.
- *
- * @return array List of index names
- */
- public function getIndexNames()
- {
- return array_keys($this->_data['metadata']['indices']);
- }
- /**
- * Returns the full state of the cluster.
- *
- * @return array State array
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-state.html
- */
- public function getState()
- {
- return $this->_data;
- }
- /**
- * Returns a list of existing node names.
- *
- * @return array List of node names
- */
- public function getNodeNames()
- {
- $data = $this->getState();
- $nodeNames = [];
- foreach ($data['nodes'] as $node) {
- $nodeNames[] = $node['name'];
- }
- return $nodeNames;
- }
- /**
- * Returns all nodes of the cluster.
- *
- * @return \Elastica\Node[]
- */
- public function getNodes()
- {
- $nodes = [];
- $data = $this->getState();
- foreach ($data['nodes'] as $id => $name) {
- $nodes[] = new Node($id, $this->getClient());
- }
- return $nodes;
- }
- /**
- * Returns the client object.
- *
- * @return \Elastica\Client Client object
- */
- public function getClient()
- {
- return $this->_client;
- }
- /**
- * Returns the cluster information (not implemented yet).
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-nodes-info.html
- *
- * @param array $args Additional arguments
- *
- * @throws \Elastica\Exception\NotImplementedException
- */
- public function getInfo(array $args)
- {
- throw new NotImplementedException('not implemented yet');
- }
- /**
- * Return Cluster health.
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/cluster-health.html
- *
- * @return \Elastica\Cluster\Health
- */
- public function getHealth()
- {
- return new Health($this->getClient());
- }
- /**
- * Return Cluster settings.
- *
- * @return \Elastica\Cluster\Settings
- */
- public function getSettings()
- {
- return new Settings($this->getClient());
- }
- }
|