Cluster.php 3.5 KB

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