Skip to content

Commit

Permalink
index manager for rebuild mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
benwalch committed Dec 12, 2024
1 parent 04d1a08 commit 146e031
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 105 deletions.
8 changes: 8 additions & 0 deletions config/services/manager.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:

_defaults:
autowire: true
autoconfigure: true
public: false

DsOpenSearchBundle\Manager\IndexManager: ~
81 changes: 14 additions & 67 deletions src/Command/RebuildIndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace DsOpenSearchBundle\Command;

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

public function __construct(
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
protected ClientBuilderInterface $clientBuilder,
protected IndexManager $indexManager,
protected TranslatorInterface $translator
) {
parent::__construct();
Expand All @@ -41,75 +34,29 @@ protected function execute(InputInterface $input, OutputInterface $output): int

if (empty($contextName)) {
$output->writeln('<error>no context definition name given</error>');
return 0;
return Command::FAILURE;
}

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

if (!$contextDefinition instanceof ContextDefinitionInterface) {
$output->writeln(sprintf('<error>no context definition with name "%s" found</error>', $contextName));
return 0;
}

try {
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
} catch (\Throwable $e) {
$output->writeln(
sprintf(
'%s. (The current context index provider also requires pre-configured indices. Please make sure your document definition implements the "%s" interface)',
$e->getMessage(), PreConfiguredIndexProviderInterface::class
)
);

return 0;
}

if (!$indexDocument->hasIndexFields()) {
$output->writeln(
sprintf(
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
PreConfiguredIndexProviderInterface::class
)
);

return 0;
}

$options = $contextDefinition->getIndexProviderOptions();

$client = $this->clientBuilder->build($options);
$indexService = new IndexPersistenceService($client, $options);

if ($indexService->indexExists()) {

/** @var QuestionHelper $helper */
$helper = $this->getHelper('question');

$text = $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message', [], 'admin');
$commandText = sprintf(' <info>%s (y/n)</info> [<comment>%s</comment>]:', $text, 'no');
$question = new ConfirmationQuestion($commandText, false);

if (!$helper->ask($input, $output, $question)) {
return 0;
}
$text = $this->translator->trans('ds_index_provider_opensearch.actions.index.rebuild_mapping.confirmation.message', [], 'admin');
$commandText = sprintf(' <info>%s (y/n)</info> [<comment>%s</comment>]:', $text, 'no');
$question = new ConfirmationQuestion($commandText, false);

try {
$indexService->dropIndex();
} catch (\Throwable $e) {
$output->writeln(sprintf('Error while dropping index: %s', $e->getMessage()));
return 0;
}
if (!$helper->ask($input, $output, $question)) {
return Command::SUCCESS;
}

try {
$indexService->createIndex($indexDocument);
$this->indexManager->rebuildIndex($contextName);
} catch (\Throwable $e) {
$output->writeln(sprintf('Error while creating index: %s', $e->getMessage()));
return 0;
$output->writeln(sprintf('<error>Error rebuilding index mapping: %s</error>', $e->getMessage()));
return Command::FAILURE;
}

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

return 0;
return Command::SUCCESS;
}
}
41 changes: 3 additions & 38 deletions src/Controller/Admin/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@

namespace DsOpenSearchBundle\Controller\Admin;

use DsOpenSearchBundle\Builder\ClientBuilderInterface;
use DsOpenSearchBundle\Service\IndexPersistenceService;
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
use DynamicSearchBundle\Context\ContextDefinitionInterface;
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;
use DsOpenSearchBundle\Manager\IndexManager;
use Pimcore\Bundle\AdminBundle\Controller\AdminAbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class IndexController extends AdminAbstractController
{
public function __construct(
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
protected ClientBuilderInterface $clientBuilder,
protected IndexManager $indexManager
)
{
}
Expand All @@ -31,35 +24,7 @@ public function rebuildMappingAction(Request $request): Response
}

try {
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);

if (!$contextDefinition instanceof ContextDefinitionInterface) {
throw new \Exception(
sprintf('no context definition with name "%s" found', $contextName)
);
}

$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);

if (!$indexDocument->hasIndexFields()) {
throw new \Exception(
sprintf(
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
PreConfiguredIndexProviderInterface::class
)
);
}

$options = $contextDefinition->getIndexProviderOptions();

$client = $this->clientBuilder->build($options);
$indexService = new IndexPersistenceService($client, $options);

if ($indexService->indexExists()) {
$indexService->dropIndex();
}

$indexService->createIndex($indexDocument);
$this->indexManager->rebuildIndex($contextName);
} catch (\Throwable $e) {
return new Response($e->getMessage(), 500);
}
Expand Down
64 changes: 64 additions & 0 deletions src/Manager/IndexManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace DsOpenSearchBundle\Manager;

use DsOpenSearchBundle\Builder\ClientBuilderInterface;
use DsOpenSearchBundle\Service\IndexPersistenceService;
use DynamicSearchBundle\Builder\ContextDefinitionBuilderInterface;
use DynamicSearchBundle\Context\ContextDefinitionInterface;
use DynamicSearchBundle\Generator\IndexDocumentGeneratorInterface;
use DynamicSearchBundle\Provider\PreConfiguredIndexProviderInterface;

class IndexManager
{

public function __construct(
protected ContextDefinitionBuilderInterface $contextDefinitionBuilder,
protected IndexDocumentGeneratorInterface $indexDocumentGenerator,
protected ClientBuilderInterface $clientBuilder,
)
{
}

public function rebuildIndex(string $contextName): void
{
$contextDefinition = $this->contextDefinitionBuilder->buildContextDefinition($contextName, ContextDefinitionInterface::CONTEXT_DISPATCH_TYPE_INDEX);

if (!$contextDefinition instanceof ContextDefinitionInterface) {
throw new \Exception(sprintf('no context definition with name "%s" found', $contextName));
}

try {
$indexDocument = $this->indexDocumentGenerator->generateWithoutData($contextDefinition, ['preConfiguredIndexProvider' => true]);
} catch (\Throwable $e) {
throw new \Exception(
sprintf(
'%s. (The current context index provider also requires pre-configured indices. Please make sure your document definition implements the "%s" interface)',
$e->getMessage(), PreConfiguredIndexProviderInterface::class
)
);
}

if (!$indexDocument->hasIndexFields()) {
throw new \Exception(
sprintf(
'No Index Document found. The current context index provider requires pre-configured indices. Please make sure your document definition implements the "%s" interface',
PreConfiguredIndexProviderInterface::class
)
);
}

$options = $contextDefinition->getIndexProviderOptions();

$client = $this->clientBuilder->build($options);
$indexService = new IndexPersistenceService($client, $options);

if ($indexService->indexExists()) {
$indexService->dropIndex();
}

$indexService->createIndex($indexDocument);
}


}

0 comments on commit 146e031

Please sign in to comment.