public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false, $delete = true)
    {
        $client = $index->getClient();

        $aliasName = $indexConfig->getElasticSearchName();
        $oldIndexName = null;
        $newIndexName = $index->getName();

        try {
            $oldIndexName = $this->getAliasedIndex($client, $aliasName);
        } catch (AliasIsIndexException $e) {
            if (!$force) {
                throw $e;
            }

            if ($delete) {
                $this->deleteIndex($client, $aliasName);
            } else {
                $this->closeIndex($client, $aliasName);
            }
        }

        try {
            $aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName);
            $client->request('_aliases', 'POST', $aliasUpdateRequest);
        } catch (ExceptionInterface $e) {
            $this->cleanupRenameFailure($client, $newIndexName, $e);
        }

        // Delete the old index after the alias has been switched
        if (null !== $oldIndexName) {
            if ($delete) {
                $this->deleteIndex($client, $oldIndexName);
            } else {
                $this->closeIndex($client, $oldIndexName);
            }
        }
    }
public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false, $delete = true)
    {
        $client = $index->getClient();

        $aliasName = $indexConfig->getElasticSearchName();
        $oldIndexName = null;
        $newIndexName = $index->getName();

        try {
            $oldIndexName = $this->getAliasedIndex($client, $aliasName);
        } catch (AliasIsIndexException $e) {
            if (!$force) {
                throw $e;
            }

            if ($delete) {
                $this->deleteIndex($client, $aliasName);
            } else {
                $this->closeIndex($client, $aliasName);
            }
        }

        try {
            $aliasUpdateRequest = $this->buildAliasUpdateRequest($oldIndexName, $aliasName, $newIndexName);
            $client->request('_aliases', 'POST', $aliasUpdateRequest);
        } catch (ExceptionInterface $e) {
            $this->cleanupRenameFailure($client, $newIndexName, $e);
        }

        // Delete the old index after the alias has been switched
        if (null !== $oldIndexName) {
            if ($delete) {
                $this->deleteIndex($client, $oldIndexName);
            } else {
                $this->closeIndex($client, $oldIndexName);
            }
        }
    }
public function switchIndexAlias ( IndexConfig $ indexConfig , Index $ index , $ force = false , $ delete = true ) { $ client = $ index -> getClient ( ) ; $ aliasName = $ indexConfig -> getElasticSearchName ( ) ; $ oldIndexName = null ; $ newIndexName = $ index -> getName ( ) ; try { $ oldIndexName = $ this -> getAliasedIndex ( $ client , $ aliasName ) ; } catch ( AliasIsIndexException $ e ) { if ( ! $ force ) { throw $ e ; } if ( $ delete ) { $ this -> deleteIndex ( $ client , $ aliasName ) ; } else { $ this -> closeIndex ( $ client , $ aliasName ) ; } } try { $ aliasUpdateRequest = $ this -> buildAliasUpdateRequest ( $ oldIndexName , $ aliasName , $ newIndexName ) ; $ client -> request ( '_aliases' , 'POST' , $ aliasUpdateRequest ) ; } catch ( ExceptionInterface $ e ) { $ this -> cleanupRenameFailure ( $ client , $ newIndexName , $ e ) ; } // Delete the old index after the alias has been switched if ( null !== $ oldIndexName ) { if ( $ delete ) { $ this -> deleteIndex ( $ client , $ oldIndexName ) ; } else { $ this -> closeIndex ( $ client , $ oldIndexName ) ; } } }
Switches an index to become the new target for an alias. Only applies for
indexes that are set to use aliases.

$force will delete an index encountered where an alias is expected.

@param IndexConfig $indexConfig
@param Index       $index
@param bool        $force
@param bool        $delete

@throws AliasIsIndexException
Switches an index to become the new target for an alias . Only applies for indexes that are set to use aliases .