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