| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace Elastica;
- use Elastica\Exception\ResponseException;
- use Elasticsearch\Endpoints\Indices\Alias\Get;
- use Elasticsearch\Endpoints\Indices\Stats;
- /**
- * Elastica general status.
- *
- * @author Nicolas Ruflin <spam@ruflin.com>
- *
- * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-status.html
- */
- class Status
- {
- /**
- * Contains all status infos.
- *
- * @var \Elastica\Response Response object
- */
- protected $_response;
- /**
- * Data.
- *
- * @var array Data
- */
- protected $_data;
- /**
- * Client object.
- *
- * @var \Elastica\Client Client object
- */
- protected $_client;
- /**
- * Constructs Status object.
- *
- * @param \Elastica\Client $client Client object
- */
- public function __construct(Client $client)
- {
- $this->_client = $client;
- }
- /**
- * Returns status data.
- *
- * @return array Status data
- */
- public function getData()
- {
- if (is_null($this->_data)) {
- $this->refresh();
- }
- return $this->_data;
- }
- /**
- * Returns a list of the existing index names.
- *
- * @return array Index names list
- */
- public function getIndexNames()
- {
- $data = $this->getData();
- return array_keys($data['indices']);
- }
- /**
- * Checks if the given index exists.
- *
- * @param string $name Index name to check
- *
- * @return bool True if index exists
- */
- public function indexExists($name)
- {
- return in_array($name, $this->getIndexNames());
- }
- /**
- * Checks if the given alias exists.
- *
- * @param string $name Alias name
- *
- * @return bool True if alias exists
- */
- public function aliasExists($name)
- {
- return count($this->getIndicesWithAlias($name)) > 0;
- }
- /**
- * Returns an array with all indices that the given alias name points to.
- *
- * @param string $alias Alias name
- *
- * @return array|\Elastica\Index[] List of Elastica\Index
- */
- public function getIndicesWithAlias($alias)
- {
- $endpoint = new Get();
- $endpoint->setName($alias);
- $response = null;
- try {
- $response = $this->_client->requestEndpoint($endpoint);
- } catch (ResponseException $e) {
- // 404 means the index alias doesn't exist which means no indexes have it.
- if (404 === $e->getResponse()->getStatus()) {
- return [];
- }
- // If we don't have a 404 then this is still unexpected so rethrow the exception.
- throw $e;
- }
- $indices = [];
- foreach ($response->getData() as $name => $unused) {
- $indices[] = new Index($this->_client, $name);
- }
- return $indices;
- }
- /**
- * Returns response object.
- *
- * @return \Elastica\Response Response object
- */
- public function getResponse()
- {
- if (is_null($this->_response)) {
- $this->refresh();
- }
- return $this->_response;
- }
- /**
- * Return shards info.
- *
- * @return array Shards info
- */
- public function getShards()
- {
- $data = $this->getData();
- return $data['shards'];
- }
- /**
- * Refresh status object.
- */
- public function refresh()
- {
- $this->_response = $this->_client->requestEndpoint(new Stats());
- $this->_data = $this->getResponse()->getData();
- }
- }
|