Skip to content

Commit abac40b

Browse files
Merge pull request #373 from kdambekalns/bugfix/370-alias-creation
BUGFIX: Set up missing alias during node indexing
2 parents 2afe033 + 1c978a8 commit abac40b

1 file changed

Lines changed: 89 additions & 28 deletions

File tree

Classes/Command/NodeIndexCommandController.php

Lines changed: 89 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@
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;
21+
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception;
1922
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\ConfigurationException;
2023
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception\RuntimeException;
2124
use Flowpack\ElasticSearch\ContentRepositoryAdaptor\Indexer\NodeIndexer;
@@ -35,12 +38,9 @@
3538
use Neos\Flow\Configuration\ConfigurationManager;
3639
use Neos\Flow\Core\Booting\Exception\SubProcessException;
3740
use Neos\Flow\Core\Booting\Scripts;
38-
use Neos\Flow\Exception;
39-
use Neos\Utility\Exception\FilesException;
40-
use Neos\Utility\Files;
4141
use Neos\Flow\Log\Utility\LogEnvironment;
42+
use Neos\Utility\Files;
4243
use Psr\Log\LoggerInterface;
43-
use Symfony\Component\Yaml\Yaml;
4444

4545
/**
4646
* Provides CLI features for index handling
@@ -133,17 +133,30 @@ class NodeIndexCommandController extends CommandController
133133
*/
134134
protected $workspaceIndexer;
135135

136+
/**
137+
* @Flow\Inject
138+
* @var ElasticSearchClient
139+
*/
140+
protected $searchClient;
141+
142+
/**
143+
* @var IndexDriverInterface
144+
* @Flow\Inject
145+
*/
146+
protected $indexDriver;
147+
136148
/**
137149
* Index a single node by the given identifier and workspace name
138150
*
139151
* @param string $identifier
140-
* @param string $workspace
152+
* @param string|null $workspace
141153
* @param string|null $postfix
142154
* @return void
155+
* @throws ApiException
143156
* @throws ConfigurationException
144-
* @throws FilesException
145-
* @throws StopCommandException
146-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
157+
* @throws Exception
158+
* @throws RuntimeException
159+
* @throws SubProcessException
147160
* @throws \Flowpack\ElasticSearch\Exception
148161
*/
149162
public function indexNodeCommand(string $identifier, string $workspace = null, string $postfix = null): void
@@ -152,8 +165,13 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
152165
$workspace = 'live';
153166
}
154167

168+
$updateAliases = false;
155169
if ($postfix !== null) {
156170
$this->nodeIndexer->setIndexNamePostfix($postfix);
171+
} elseif ($this->aliasesExist() === false) {
172+
$postfix = (string)time();
173+
$updateAliases = true;
174+
$this->nodeIndexer->setIndexNamePostfix($postfix);
157175
}
158176

159177
$indexNode = function ($identifier, Workspace $workspace, array $dimensions) {
@@ -201,6 +219,22 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
201219
}
202220

203221
$this->nodeIndexer->flush();
222+
223+
if ($updateAliases) {
224+
$combinations = $this->contentDimensionCombinator->getAllAllowedCombinations();
225+
$combinations = $combinations === [] ? [[]] : $combinations;
226+
227+
foreach ($combinations as $combination) {
228+
$this->executeInternalCommand('aliasInternal', [
229+
'dimensionsValues' => json_encode($combination),
230+
'postfix' => $postfix,
231+
'update' => false
232+
]);
233+
}
234+
235+
$this->nodeIndexer->updateMainAlias();
236+
}
237+
204238
$this->outputErrorHandling();
205239
}
206240

@@ -209,14 +243,15 @@ public function indexNodeCommand(string $identifier, string $workspace = null, s
209243
*
210244
* This command (re-)indexes all nodes contained in the content repository and sets the schema beforehand.
211245
*
212-
* @param int $limit Amount of nodes to index at maximum
246+
* @param int|null $limit Amount of nodes to index at maximum
213247
* @param bool $update if TRUE, do not throw away the index at the start. Should *only be used for development*.
214-
* @param string $workspace name of the workspace which should be indexed
215-
* @param string $postfix Index postfix, index with the same postfix will be deleted if exist
248+
* @param string|null $workspace name of the workspace which should be indexed
249+
* @param string|null $postfix Index postfix, index with the same postfix will be deleted if exist
216250
* @return void
217251
* @throws StopCommandException
218-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
252+
* @throws Exception
219253
* @throws ConfigurationException
254+
* @throws ApiException
220255
*/
221256
public function buildCommand(int $limit = null, bool $update = false, string $workspace = null, string $postfix = null): void
222257
{
@@ -238,7 +273,7 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
238273
]);
239274
};
240275

241-
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $update, $postfix) {
276+
$buildIndex = function (array $dimensionsValues) use ($workspace, $limit, $postfix) {
242277
$this->build($dimensionsValues, $workspace, $postfix, $limit);
243278
};
244279

@@ -268,10 +303,9 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
268303

269304
$runAndLog($createIndicesAndApplyMapping, 'Creating indices and apply mapping');
270305

271-
// $timeStart = microtime(true);
272-
// $this->output(str_pad('Indexing nodes ... ', 20));
273-
// $buildIndex([]);
274-
// $this->outputLine('<success>Done</success> (took %s seconds)', [number_format(microtime(true) - $timeStart, 2)]);
306+
if ($this->aliasesExist() === false) {
307+
$runAndLog($updateAliases, 'Set up aliases');
308+
}
275309

276310
$runAndLog($buildIndex, 'Indexing nodes');
277311

@@ -285,14 +319,39 @@ public function buildCommand(int $limit = null, bool $update = false, string $wo
285319
$this->outputMemoryUsage();
286320
}
287321

322+
/**
323+
* @return bool
324+
* @throws ApiException
325+
* @throws ConfigurationException
326+
* @throws Exception
327+
*/
328+
private function aliasesExist(): bool
329+
{
330+
$aliasName = $this->searchClient->getIndexName();
331+
$aliasesExist = false;
332+
try {
333+
$aliasesExist = $this->indexDriver->getIndexNamesByAlias($aliasName) !== [];
334+
} catch (ApiException $exception) {
335+
// in case of 404, do not throw an error...
336+
if ($exception->getResponse()->getStatusCode() !== 404) {
337+
throw $exception;
338+
}
339+
}
340+
341+
return $aliasesExist;
342+
}
343+
288344
/**
289345
* Build up the node index
290346
*
291347
* @param array $dimensionsValues
292-
* @param string $postfix
293-
* @param string $workspace
294-
* @param int $limit
348+
* @param string|null $workspace
349+
* @param string|null $postfix
350+
* @param int|null $limit
351+
* @throws ConfigurationException
295352
* @throws Exception
353+
* @throws RuntimeException
354+
* @throws SubProcessException
296355
*/
297356
private function build(array $dimensionsValues, ?string $workspace = null, ?string $postfix = null, ?int $limit = null): void
298357
{
@@ -342,13 +401,13 @@ private function build(array $dimensionsValues, ?string $workspace = null, ?stri
342401
* @param string $dimensionsValues
343402
* @param bool $update
344403
* @param string|null $postfix
345-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
404+
* @throws Exception
346405
* @throws \Flowpack\ElasticSearch\Exception
347406
* @throws \Neos\Flow\Http\Exception
348407
* @throws \Exception
349408
* @Flow\Internal
350409
*/
351-
public function createInternalCommand(string $dimensionsValues, bool $update = false, ?string $postfix = null): void
410+
public function createInternalCommand(string $dimensionsValues, bool $update = false, string $postfix = null): void
352411
{
353412
if ($update === true) {
354413
$this->logger->warning('!!! Update Mode (Development) active!', LogEnvironment::fromMethodName(__METHOD__));
@@ -370,7 +429,7 @@ public function createInternalCommand(string $dimensionsValues, bool $update = f
370429
* @param string $workspace
371430
* @param string $dimensionsValues
372431
* @param string $postfix
373-
* @param int $limit
432+
* @param int|null $limit
374433
* @return void
375434
* @Flow\Internal
376435
*/
@@ -397,7 +456,7 @@ public function buildWorkspaceInternalCommand(string $workspace, string $dimensi
397456
*
398457
* @param string $dimensionsValues
399458
* @param string $postfix
400-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
459+
* @throws Exception
401460
* @throws \Flowpack\ElasticSearch\Exception
402461
* @throws \Neos\Flow\Http\Exception
403462
* @throws ConfigurationException
@@ -417,7 +476,7 @@ public function refreshInternalCommand(string $dimensionsValues, string $postfix
417476
* @param string $dimensionsValues
418477
* @param string $postfix
419478
* @param bool $update
420-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
479+
* @throws Exception
421480
* @throws \Flowpack\ElasticSearch\Exception
422481
* @throws ApiException
423482
* @throws ConfigurationException
@@ -451,7 +510,8 @@ private function configureNodeIndexer(array $dimensionsValues, string $postfix):
451510
* Clean up old indexes (i.e. all but the current one)
452511
*
453512
* @return void
454-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
513+
* @throws ConfigurationException
514+
* @throws Exception
455515
*/
456516
public function cleanupCommand(): void
457517
{
@@ -536,7 +596,7 @@ private function createContentContext(string $workspaceName, array $dimensions =
536596

537597
if ($dimensions !== []) {
538598
$contextProperties['dimensions'] = $dimensions;
539-
$contextProperties['targetDimensions'] = array_map(function ($dimensionValues) {
599+
$contextProperties['targetDimensions'] = array_map(static function ($dimensionValues) {
540600
return array_shift($dimensionValues);
541601
}, $dimensions);
542602
}
@@ -548,9 +608,10 @@ private function createContentContext(string $workspaceName, array $dimensions =
548608
* Apply the mapping to the current index.
549609
*
550610
* @return void
551-
* @throws \Flowpack\ElasticSearch\ContentRepositoryAdaptor\Exception
611+
* @throws Exception
552612
* @throws \Flowpack\ElasticSearch\Exception
553613
* @throws ConfigurationException
614+
* @throws \Neos\Flow\Http\Exception
554615
*/
555616
private function applyMapping(): void
556617
{

0 commit comments

Comments
 (0)