Elastica\Status::getIndicesWithAlias PHP Method

getIndicesWithAlias() public method

Returns an array with all indices that the given alias name points to.
public getIndicesWithAlias ( string $alias ) : array | Index[]
$alias string Alias name
return array | Index[] List of Elastica\Index
    public function getIndicesWithAlias($alias)
    {
        $response = null;
        try {
            $response = $this->_client->request('/_alias/' . $alias);
        } catch (ResponseException $e) {
            $transferInfo = $e->getResponse()->getTransferInfo();
            // 404 means the index alias doesn't exist which means no indexes have it.
            if ($transferInfo['http_code'] === 404) {
                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;
    }

Usage Example

Example #1
0
 /**
  * @group functional
  */
 public function testAliasExists()
 {
     $aliasName = 'elastica_test-alias';
     $index1 = $this->_createIndex();
     $indexName = $index1->getName();
     $status = new Status($index1->getClient());
     foreach ($status->getIndicesWithAlias($aliasName) as $tmpIndex) {
         $tmpIndex->removeAlias($aliasName);
     }
     $this->assertFalse($status->aliasExists($aliasName));
     $index1->addAlias($aliasName);
     $status->refresh();
     $this->assertTrue($status->aliasExists($aliasName));
     $indicesWithAlias = $status->getIndicesWithAlias($aliasName);
     $this->assertEquals(array($indexName), array_map(function ($index) {
         return $index->getName();
     }, $indicesWithAlias));
 }
All Usage Examples Of Elastica\Status::getIndicesWithAlias