Ejemplo n.º 1
0
 /**
  * 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
  *
  * @throws AliasIsIndexException
  * @throws \RuntimeException
  */
 public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
 {
     $client = $index->getClient();
     $aliasName = $indexConfig->getElasticSearchName();
     $oldIndexName = null;
     $newIndexName = $index->getName();
     try {
         $oldIndexName = $this->getAliasedIndex($client, $aliasName);
     } catch (AliasIsIndexException $e) {
         if (!$force) {
             throw $e;
         }
         $this->deleteIndex($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) {
         $this->deleteIndex($client, $oldIndexName);
     }
 }
 /**
  * Builds mappings for an entire index.
  *
  * @param IndexConfig $indexConfig
  * @return array
  */
 public function buildIndexMapping(IndexConfig $indexConfig)
 {
     $typeMappings = array();
     foreach ($indexConfig->getTypes() as $typeConfig) {
         $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
     }
     $mapping = array('mappings' => $typeMappings, 'settings' => $indexConfig->getSettings());
     return $mapping;
 }
 /**
  * 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
  * @throws AliasIsIndexException
  * @throws \RuntimeException
  */
 public function switchIndexAlias(IndexConfig $indexConfig, Index $index, $force = false)
 {
     $client = $index->getClient();
     $aliasName = $indexConfig->getElasticSearchName();
     $oldIndexName = false;
     $newIndexName = $index->getName();
     try {
         $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
     } catch (AliasIsIndexException $e) {
         if (!$force) {
             throw $e;
         }
         $this->deleteIndex($client, $aliasName);
         $aliasedIndexes = array();
     }
     if (count($aliasedIndexes) > 1) {
         throw new \RuntimeException(sprintf('Alias %s is used for multiple indexes: [%s].
                 Make sure it\'s either not used or is assigned to one index only', $aliasName, join(', ', $aliasedIndexes)));
     }
     $aliasUpdateRequest = array('actions' => array());
     if (count($aliasedIndexes) === 1) {
         // if the alias is set - add an action to remove it
         $oldIndexName = $aliasedIndexes[0];
         $aliasUpdateRequest['actions'][] = array('remove' => array('index' => $oldIndexName, 'alias' => $aliasName));
     }
     // add an action to point the alias to the new index
     $aliasUpdateRequest['actions'][] = array('add' => array('index' => $newIndexName, 'alias' => $aliasName));
     try {
         $client->request('_aliases', 'POST', $aliasUpdateRequest);
     } catch (ExceptionInterface $renameAliasException) {
         $additionalError = '';
         // if we failed to move the alias, delete the newly built index
         try {
             $index->delete();
         } catch (ExceptionInterface $deleteNewIndexException) {
             $additionalError = sprintf('Tried to delete newly built index %s, but also failed: %s', $newIndexName, $deleteNewIndexException->getMessage());
         }
         throw new \RuntimeException(sprintf('Failed to updated index alias: %s. %s', $renameAliasException->getMessage(), $additionalError ?: sprintf('Newly built index %s was deleted', $newIndexName)), 0, $renameAliasException);
     }
     // Delete the old index after the alias has been switched
     if ($oldIndexName) {
         $oldIndex = new Index($client, $oldIndexName);
         try {
             $oldIndex->delete();
         } catch (ExceptionInterface $deleteOldIndexException) {
             throw new \RuntimeException(sprintf('Failed to delete old index %s with message: %s', $oldIndexName, $deleteOldIndexException->getMessage()), 0, $deleteOldIndexException);
         }
     }
 }
 /**
  * Builds mappings for an entire index.
  *
  * @param IndexConfig $indexConfig
  * @return array
  */
 public function buildIndexMapping(IndexConfig $indexConfig)
 {
     $typeMappings = array();
     foreach ($indexConfig->getTypes() as $typeConfig) {
         $typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig);
     }
     $mapping = array();
     if ($typeMappings) {
         $mapping['mappings'] = $typeMappings;
     }
     // 'warmers' => $indexConfig->getWarmers(),
     $settings = $indexConfig->getSettings();
     if ($settings) {
         $mapping['settings'] = $settings;
     }
     return $mapping;
 }