Skip to content

Commit ba6f238

Browse files
committed
Merge pull request #24 from Flowpack/fix-alias-is-actual-index
[BUGFIX] Aliasing the index fails if name is used by actual index
2 parents 72c5dc8 + 8717115 commit ba6f238

1 file changed

Lines changed: 21 additions & 8 deletions

File tree

  • Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Indexer

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Indexer/NodeIndexer.php

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,8 @@ protected function getDefaultContextVariables() {
506506
*
507507
* @return void
508508
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
509+
* @throws \Flowpack\ElasticSearch\Transfer\Exception\ApiException
510+
* @throws \Exception
509511
*/
510512
public function updateIndexAlias() {
511513
$aliasName = $this->searchClient->getIndexName(); // The alias name is the unprefixed index name
@@ -521,18 +523,29 @@ public function updateIndexAlias() {
521523
try {
522524
$response = $this->searchClient->request('GET', '/*/_alias/' . $aliasName);
523525
if ($response->getStatusCode() !== 200) {
524-
throw new \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception('The alias "' . $aliasName . '" was not found with some unexpected error... (return code: ' . $response->getStatusCode(), 1383650137);
526+
throw new \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception('The alias "' . $aliasName . '" was not found with some unexpected error... (return code: ' . $response->getStatusCode() . ')', 1383650137);
525527
}
526528

527529
$indexNames = array_keys($response->getTreatedContent());
528530

529-
foreach ($indexNames as $indexName) {
530-
$aliasActions[] = array(
531-
'remove' => array(
532-
'index' => $indexName,
533-
'alias' => $aliasName
534-
)
535-
);
531+
if ($indexNames === array()) {
532+
// if there is an actual index with the name we want to use as alias, remove it now
533+
$response = $this->searchClient->request('HEAD', '/' . $aliasName);
534+
if ($response->getStatusCode() === 200) {
535+
$response = $this->searchClient->request('DELETE', '/' . $aliasName);
536+
if ($response->getStatusCode() !== 200) {
537+
throw new \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception('The index "' . $aliasName . '" could not be removed to be replaced by an alias. (return code: ' . $response->getStatusCode() . ')', 1395419177);
538+
}
539+
}
540+
} else {
541+
foreach ($indexNames as $indexName) {
542+
$aliasActions[] = array(
543+
'remove' => array(
544+
'index' => $indexName,
545+
'alias' => $aliasName
546+
)
547+
);
548+
}
536549
}
537550
} catch (\Flowpack\ElasticSearch\Transfer\Exception\ApiException $exception) {
538551
// in case of 404, do not throw an error...

0 commit comments

Comments
 (0)