diff --git a/Command/ResetCommand.php b/Command/ResetCommand.php
index 06cfe48..ce05e96 100755
--- a/Command/ResetCommand.php
+++ b/Command/ResetCommand.php
@@ -33,6 +33,7 @@ class ResetCommand extends ContainerAwareCommand
->setName('fos:elastica:reset')
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to reset')
->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to reset')
+ ->addOption('force', null, InputOption::VALUE_NONE, 'Force index deletion if same name as alias')
->setDescription('Reset search indexes')
;
}
@@ -51,8 +52,9 @@ class ResetCommand extends ContainerAwareCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
- $index = $input->getOption('index');
- $type = $input->getOption('type');
+ $index = $input->getOption('index');
+ $type = $input->getOption('type');
+ $force = (bool) $input->getOption('force');
if (null === $index && null !== $type) {
throw new \InvalidArgumentException('Cannot specify type option without an index.');
@@ -69,7 +71,7 @@ class ResetCommand extends ContainerAwareCommand
foreach ($indexes as $index) {
$output->writeln(sprintf('Resetting %s', $index));
- $this->resetter->resetIndex($index);
+ $this->resetter->resetIndex($index, false, $force);
}
}
}
diff --git a/Exception/AliasIsIndexException.php b/Exception/AliasIsIndexException.php
new file mode 100644
index 0000000..87f546b
--- /dev/null
+++ b/Exception/AliasIsIndexException.php
@@ -0,0 +1,12 @@
+getClient();
@@ -45,7 +51,17 @@ class AliasProcessor
$oldIndexName = false;
$newIndexName = $index->getName();
- $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
+ try {
+ $aliasedIndexes = $this->getAliasedIndexes($client, $aliasName);
+ } catch(AliasIsIndexException $e) {
+ if ($force) {
+ $this->deleteIndex($client, $aliasName);
+
+ return;
+ }
+
+ throw $e;
+ }
if (count($aliasedIndexes) > 1) {
throw new \RuntimeException(
@@ -116,8 +132,10 @@ class AliasProcessor
/**
* Returns array of indexes which are mapped to given alias
*
+ * @param Client $client
* @param string $aliasName Alias name
* @return array
+ * @throws \FOS\ElasticaBundle\Exception\AliasIsIndexException
*/
private function getAliasedIndexes(Client $client, $aliasName)
{
@@ -125,6 +143,9 @@ class AliasProcessor
$aliasedIndexes = array();
foreach ($aliasesInfo as $indexName => $indexInfo) {
+ if ($indexName === $aliasName) {
+ throw new AliasIsIndexException($indexName);
+ }
if (isset($indexInfo['aliases'])) {
$aliases = array_keys($indexInfo['aliases']);
if (in_array($aliasName, $aliases)) {
@@ -135,4 +156,16 @@ class AliasProcessor
return $aliasedIndexes;
}
+
+ /**
+ * Delete an index
+ *
+ * @param Client $client
+ * @param string $indexName Index name to delete
+ */
+ private function deleteIndex(Client $client, $indexName)
+ {
+ $path = sprintf("%s", $indexName);
+ $client->request($path, Request::DELETE);
+ }
}
diff --git a/Index/Resetter.php b/Index/Resetter.php
index 3f07fa1..c93ae2d 100644
--- a/Index/Resetter.php
+++ b/Index/Resetter.php
@@ -45,10 +45,10 @@ class Resetter
/**
* Deletes and recreates all indexes
*/
- public function resetAllIndexes($populating = false)
+ public function resetAllIndexes($populating = false, $force = false)
{
foreach ($this->configManager->getIndexNames() as $name) {
- $this->resetIndex($name, $populating);
+ $this->resetIndex($name, $populating, $force);
}
}
@@ -58,9 +58,10 @@ class Resetter
*
* @param string $indexName
* @param bool $populating
+ * @param bool $force If index exists with same name as alias, remove it
* @throws \InvalidArgumentException if no index exists for the given name
*/
- public function resetIndex($indexName, $populating = false)
+ public function resetIndex($indexName, $populating = false, $force = false)
{
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
$index = $this->indexManager->getIndex($indexName);
@@ -73,7 +74,7 @@ class Resetter
$index->create($mapping, true);
if (!$populating and $indexConfig->isUseAlias()) {
- $this->aliasProcessor->switchIndexAlias($indexConfig, $index);
+ $this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
}
}