-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
89 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |