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