|
1 | 1 | <?php
|
2 | 2 |
|
3 | 3 | namespace Elastica;
|
| 4 | +use Elastica\Exception\ResponseException; |
4 | 5 | use Elastica\Index\Status as IndexStatus;
|
5 | 6 |
|
6 | 7 | /**
|
@@ -105,30 +106,35 @@ public function indexExists($name)
|
105 | 106 | */
|
106 | 107 | public function aliasExists($name)
|
107 | 108 | {
|
108 |
| - foreach ($this->getIndexStatuses() as $status) { |
109 |
| - if ($status->hasAlias($name)) { |
110 |
| - return true; |
111 |
| - } |
112 |
| - } |
| 109 | + return count($this->getIndicesWithAlias($name)) > 0; |
113 | 110 |
|
114 | 111 | return false;
|
115 | 112 | }
|
116 | 113 |
|
117 | 114 | /**
|
118 | 115 | * Returns an array with all indices that the given alias name points to
|
119 | 116 | *
|
120 |
| - * @param string $name Alias name |
| 117 | + * @param string $alias Alias name |
121 | 118 | * @return array|\Elastica\Index[] List of Elastica\Index
|
122 | 119 | */
|
123 |
| - public function getIndicesWithAlias($name) |
| 120 | + public function getIndicesWithAlias($alias) |
124 | 121 | {
|
125 |
| - $indices = array(); |
126 |
| - foreach ($this->getIndexStatuses() as $status) { |
127 |
| - if ($status->hasAlias($name)) { |
128 |
| - $indices[] = $status->getIndex(); |
| 122 | + $response = null; |
| 123 | + try { |
| 124 | + $response = $this->_client->request("/_alias/$alias"); |
| 125 | + } catch (ResponseException $e) { |
| 126 | + $transferInfo = $e->getResponse()->getTransferInfo(); |
| 127 | + // 404 means the index alias doesn't exist which means no indexes have it. |
| 128 | + if ($transferInfo['http_code'] === 404) { |
| 129 | + return array(); |
129 | 130 | }
|
| 131 | + // If we don't have a 404 then this is still unexpected so rethrow the exception. |
| 132 | + throw $e; |
| 133 | + } |
| 134 | + $indices = array(); |
| 135 | + foreach ($response->getData() as $name => $unused) { |
| 136 | + $indices[] = new Index($this->_client, $name); |
130 | 137 | }
|
131 |
| - |
132 | 138 | return $indices;
|
133 | 139 | }
|
134 | 140 |
|
|
0 commit comments