Skip to content

Commit 5ad0cd9

Browse files
committed
BUGFIX: Set up missing alias during nodeindex:build
The command `nodeindex:build` does not set up aliases after creating fresh indexes until it considers the indexing complete. This is good, usually, since it allows for indexing "in the background". But if no aliases exist at all, this is going to break. This change fixes that by setting up aliases after creating (fresh) indexes, if no aliases exist. Fixes #370
1 parent 1ed01ec commit 5ad0cd9

1 file changed

Lines changed: 41 additions & 0 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
*/
1515

1616
use Doctrine\Common\Collections\ArrayCollection;
17+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\IndexDriverInterface;
1718
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Driver\NodeTypeMappingBuilderInterface;
19+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ElasticSearchClient;
1820
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\ErrorHandling\ErrorHandlingService;
1921
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
2022
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
@@ -131,6 +133,18 @@ class NodeIndexCommandController extends CommandController
131133
*/
132134
protected $workspaceIndexer;
133135

136+
/**
137+
* @Flow\Inject
138+
* @var ElasticSearchClient
139+
*/
140+
protected $searchClient;
141+
142+
/**
143+
* @var IndexDriverInterface
144+
* @Flow\Inject
145+
*/
146+
protected $indexDriver;
147+
134148
/**
135149
* Index a single node by the given identifier and workspace name
136150
*
@@ -213,6 +227,7 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
213227
* @throws StopCommandException
214228
* @throws Exception
215229
* @throws ConfigurationException
230+
* @throws ApiException
216231
*/
217232
public function buildCommand(int $limit = null, bool $update = false, string $workspace = null, string $postfix = null): void
218233
{
@@ -264,6 +279,10 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
264279

265280
$runAndLog($createIndicesAndApplyMapping, 'Creating indices and apply mapping');
266281

282+
if ($this->aliasesExist() === false) {
283+
$runAndLog($updateAliases, 'Set up aliases');
284+
}
285+
267286
$runAndLog($buildIndex, 'Indexing nodes');
268287

269288
$runAndLog($refresh, 'Refresh indicies');
@@ -276,6 +295,28 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
276295
$this->outputMemoryUsage();
277296
}
278297

298+
/**
299+
* @return bool
300+
* @throws ApiException
301+
* @throws ConfigurationException
302+
* @throws Exception
303+
*/
304+
private function aliasesExist(): bool
305+
{
306+
$aliasName = $this->searchClient->getIndexName();
307+
$aliasesExist = false;
308+
try {
309+
$aliasesExist = $this->indexDriver->getIndexNamesByAlias($aliasName) !== [];
310+
} catch (ApiException $exception) {
311+
// in case of 404, do not throw an error...
312+
if ($exception->getResponse()->getStatusCode() !== 404) {
313+
throw $exception;
314+
}
315+
}
316+
317+
return $aliasesExist;
318+
}
319+
279320
/**
280321
* Build up the node index
281322
*

0 commit comments

Comments
 (0)