Skip to content

Commit 2682355

Browse files
ComiRdfeyer
authored andcommitted
TASK: Adapted index removal for ES 2.x
1 parent 6d3cef7 commit 2682355

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

Classes/Flowpack/ElasticSearch/ContentRepositoryAdaptor/Command/NodeIndexCommandController.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,19 @@ public function buildCommand($limit = null, $update = false, $workspace = null,
206206
public function cleanupCommand()
207207
{
208208
try {
209-
$indicesToBeRemoved = $this->nodeIndexer->removeOldIndices();
210-
if (count($indicesToBeRemoved) > 0) {
211-
foreach ($indicesToBeRemoved as $indexToBeRemoved) {
212-
$this->logger->log('Removing old index ' . $indexToBeRemoved);
209+
$removedIndices = $this->nodeIndexer->removeOldIndices();
210+
if (count($removedIndices) > 0) {
211+
if (count($removedIndices) === 1) {
212+
$this->logger->log('Removed old index ' . $removedIndices[0]);
213+
} else {
214+
$this->logger->log('Removed old indices ' . implode(', ', $removedIndices));
213215
}
214216
} else {
215217
$this->logger->log('Nothing to remove.');
216218
}
217219
} catch (\Flowpack\ElasticSearch\Transfer\Exception\ApiException $exception) {
218220
$response = json_decode($exception->getResponse());
219-
$this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s"', $response->status, $response->error));
221+
$this->logger->log(sprintf('Nothing removed. ElasticSearch responded with status %s, saying "%s: %s"', $response->status, $response->error->type, $response->error->reason));
220222
}
221223
}
222224

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

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -583,27 +583,15 @@ public function removeOldIndices()
583583

584584
$currentlyLiveIndices = array_keys($this->searchClient->request('GET', '/_alias/' . $aliasName)->getTreatedContent());
585585

586-
$indexStatus = $this->searchClient->request('GET', '/_status')->getTreatedContent();
587-
$allIndices = array_keys($indexStatus['indices']);
586+
$indexMappings = $this->searchClient->request('GET', '/_all')->getTreatedContent();
587+
$allIndices = array_keys($indexMappings);
588588

589-
$indicesToBeRemoved = [];
590-
591-
foreach ($allIndices as $indexName) {
592-
if (strpos($indexName, $aliasName . '-') !== 0) {
593-
// filter out all indices not starting with the alias-name, as they are unrelated to our application
594-
continue;
595-
}
596-
597-
if (array_search($indexName, $currentlyLiveIndices) !== false) {
598-
// skip the currently live index names from deletion
599-
continue;
600-
}
601-
602-
$indicesToBeRemoved[] = $indexName;
603-
}
589+
$indicesToBeRemoved = array_filter($allIndices, function ($indexName) use ($aliasName, $currentlyLiveIndices) {
590+
return strpos($indexName, $aliasName . '-') === 0 && array_search($indexName, $currentlyLiveIndices) === false;
591+
});
604592

605593
if (count($indicesToBeRemoved) > 0) {
606-
$this->searchClient->request('DELETE', '/' . implode(',', $indicesToBeRemoved) . '/');
594+
$this->searchClient->request('DELETE', '/' . implode(',', $indicesToBeRemoved));
607595
}
608596

609597
return $indicesToBeRemoved;

0 commit comments

Comments
 (0)