Skip to content

Commit 4a69619

Browse files
committed
index manager for rebuild mapping
1 parent 04d1a08 commit 4a69619

File tree

4 files changed

+89
-105
lines changed

4 files changed

+89
-105
lines changed

config/services/manager.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
3+
_defaults:
4+
autowire: true
5+
autoconfigure: true
6+
public: false
7+
8+
DsOpenSearchBundle\Manager\IndexManager: ~

src/Command/RebuildIndexCommand.php

Lines changed: 14 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
namespace DsOpenSearchBundle\Command;
44

5-
use DsOpenSearchBundle\Builder\ClientBuilderInterface;
6-
use DsOpenSearchBundle\Service\IndexPersistenceService;
7-
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
8-
use DynamicSearchBundle\Context\ContextDefinitionInterface;
9-
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
10-
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;
5+
use DsOpenSearchBundle\Manager\IndexManager;
116
use Symfony\Component\Console\Command\Command;
127
use Symfony\Component\Console\Helper\QuestionHelper;
138
use Symfony\Component\Console\Input\InputInterface;
@@ -22,9 +17,7 @@ class RebuildIndexCommand extends Command
2217
protected static $defaultDescription = 'Rebuild Index Mapping';
2318

2419
public function __construct(
25-
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
26-
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
27-
protected ClientBuilderInterface $clientBuilder,
20+
protected IndexManager $indexManager,
2821
protected TranslatorInterface $translator
2922
) {
3023
parent::__construct();
@@ -41,75 +34,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4134

4235
if (empty($contextName)) {
4336
$output->writeln('<error>no context definition name given</error>');
44-
return 0;
37+
return Command::FAILURE;
4538
}
4639

47-
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);
40+
/** @var QuestionHelper $helper */
41+
$helper = $this->getHelper('question');
4842

49-
if (!$contextDefinition instanceof ContextDefinitionInterface) {
50-
$output->writeln(sprintf('<error>no context definition with name "%s" found</error>', $contextName));
51-
return 0;
52-
}
53-
54-
try {
55-
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
56-
} catch (\Throwable $e) {
57-
$output->writeln(
58-
sprintf(
59-
'%s. (The current context index provider also requires pre-configured indices. Please make sure your document definition implements the "%s" interface)',
60-
$e->getMessage(), PreConfiguredIndexProviderInterface::class
61-
)
62-
);
63-
64-
return 0;
65-
}
66-
67-
if (!$indexDocument->hasIndexFields()) {
68-
$output->writeln(
69-
sprintf(
70-
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
71-
PreConfiguredIndexProviderInterface::class
72-
)
73-
);
74-
75-
return 0;
76-
}
77-
78-
$options = $contextDefinition->getIndexProviderOptions();
79-
80-
$client = $this->clientBuilder->build($options);
81-
$indexService = new IndexPersistenceService($client, $options);
82-
83-
if ($indexService->indexExists()) {
84-
85-
/** @var QuestionHelper $helper */
86-
$helper = $this->getHelper('question');
87-
88-
$text = $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message', [], 'admin');
89-
$commandText = sprintf(' <info>%s (y/n)</info> [<comment>%s</comment>]:', $text, 'no');
90-
$question = new ConfirmationQuestion($commandText, false);
91-
92-
if (!$helper->ask($input, $output, $question)) {
93-
return 0;
94-
}
43+
$text = $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message', [], 'admin');
44+
$commandText = sprintf(' <info>%s (y/n)</info> [<comment>%s</comment>]:', $text, 'no');
45+
$question = new ConfirmationQuestion($commandText, false);
9546

96-
try {
97-
$indexService->dropIndex();
98-
} catch (\Throwable $e) {
99-
$output->writeln(sprintf('Error while dropping index: %s', $e->getMessage()));
100-
return 0;
101-
}
47+
if (!$helper->ask($input, $output, $question)) {
48+
return Command::SUCCESS;
10249
}
10350

10451
try {
105-
$indexService->createIndex($indexDocument);
52+
$this->indexManager->rebuildIndexMapping($contextName);
10653
} catch (\Throwable $e) {
107-
$output->writeln(sprintf('Error while creating index: %s', $e->getMessage()));
108-
return 0;
54+
$output->writeln(sprintf('<error>Error rebuilding index mapping: %s</error>', $e->getMessage()));
55+
return Command::FAILURE;
10956
}
11057

11158
$output->writeln(sprintf('<info>%s</info>', $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.success', [], 'admin')));
11259

113-
return 0;
60+
return Command::SUCCESS;
11461
}
11562
}

src/Controller/Admin/IndexController.php

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,15 @@
22

33
namespace DsOpenSearchBundle\Controller\Admin;
44

5-
use DsOpenSearchBundle\Builder\ClientBuilderInterface;
6-
use DsOpenSearchBundle\Service\IndexPersistenceService;
7-
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
8-
use DynamicSearchBundle\Context\ContextDefinitionInterface;
9-
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
10-
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;
5+
use DsOpenSearchBundle\Manager\IndexManager;
116
use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController;
127
use Symfony\Component\HttpFoundation\Request;
138
use Symfony\Component\HttpFoundation\Response;
149

1510
class IndexController extends AdminAbstractController
1611
{
1712
public function __construct(
18-
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
19-
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
20-
protected ClientBuilderInterface $clientBuilder,
13+
protected IndexManager $indexManager
2114
)
2215
{
2316
}
@@ -31,35 +24,7 @@ public function rebuildMappingAction(Request $request): Response
3124
}
3225

3326
try {
34-
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);
35-
36-
if (!$contextDefinition instanceof ContextDefinitionInterface) {
37-
throw new \Exception(
38-
sprintf('no context definition with name "%s" found', $contextName)
39-
);
40-
}
41-
42-
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
43-
44-
if (!$indexDocument->hasIndexFields()) {
45-
throw new \Exception(
46-
sprintf(
47-
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
48-
PreConfiguredIndexProviderInterface::class
49-
)
50-
);
51-
}
52-
53-
$options = $contextDefinition->getIndexProviderOptions();
54-
55-
$client = $this->clientBuilder->build($options);
56-
$indexService = new IndexPersistenceService($client, $options);
57-
58-
if ($indexService->indexExists()) {
59-
$indexService->dropIndex();
60-
}
61-
62-
$indexService->createIndex($indexDocument);
27+
$this->indexManager->rebuildIndexMapping($contextName);
6328
} catch (\Throwable $e) {
6429
return new Response($e->getMessage(), 500);
6530
}

src/Manager/IndexManager.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace DsOpenSearchBundle\Manager;
4+
5+
use DsOpenSearchBundle\Builder\ClientBuilderInterface;
6+
use DsOpenSearchBundle\Service\IndexPersistenceService;
7+
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
8+
use DynamicSearchBundle\Context\ContextDefinitionInterface;
9+
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
10+
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;
11+
12+
class IndexManager
13+
{
14+
15+
public function __construct(
16+
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
17+
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
18+
protected ClientBuilderInterface $clientBuilder,
19+
)
20+
{
21+
}
22+
23+
public function rebuildIndexMapping(string $contextName): void
24+
{
25+
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);
26+
27+
if (!$contextDefinition instanceof ContextDefinitionInterface) {
28+
throw new \Exception(sprintf('no context definition with name "%s" found', $contextName));
29+
}
30+
31+
try {
32+
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
33+
} catch (\Throwable $e) {
34+
throw new \Exception(
35+
sprintf(
36+
'%s. (The current context index provider also requires pre-configured indices. Please make sure your document definition implements the "%s" interface)',
37+
$e->getMessage(), PreConfiguredIndexProviderInterface::class
38+
)
39+
);
40+
}
41+
42+
if (!$indexDocument->hasIndexFields()) {
43+
throw new \Exception(
44+
sprintf(
45+
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
46+
PreConfiguredIndexProviderInterface::class
47+
)
48+
);
49+
}
50+
51+
$options = $contextDefinition->getIndexProviderOptions();
52+
53+
$client = $this->clientBuilder->build($options);
54+
$indexService = new IndexPersistenceService($client, $options);
55+
56+
if ($indexService->indexExists()) {
57+
$indexService->dropIndex();
58+
}
59+
60+
$indexService->createIndex($indexDocument);
61+
}
62+
63+
64+
}

0 commit comments

Comments
 (0)