Status.php 3.5 KB

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