From 6cfa999411612ecc46013f464dc551a524d5fe8e Mon Sep 17 00:00:00 2001 From: VBoss Date: Tue, 8 Oct 2019 14:04:49 +0200 Subject: [PATCH] Data to Elastic --- composer.json | 5 +- src/Commands/InitializeSettings.php | 54 ++++++ src/Commands/Migrate.php | 38 ++++ src/Config/Elastic.neon | 27 ++- ....php => SpameriElasticSearchExtension.php} | 4 +- src/Diagnostics/PanelLogger.php | 2 + src/Entity/AbstractImport.php | 59 +++++++ src/Entity/Import/ArrayValue.php | 40 +++++ src/Entity/Import/BoolValue.php | 41 +++++ src/Entity/Import/DateValue.php | 48 ++++++ src/Entity/Import/EmptyValue.php | 34 ++++ src/Entity/Import/FloatValue.php | 41 +++++ src/Entity/Import/IntegerValue.php | 41 +++++ src/Entity/Import/NoValue.php | 19 ++ src/Entity/Import/StringValue.php | 41 +++++ .../Import/ValidationObjectInterface.php | 12 ++ .../Import/ValidationPropertyInterface.php | 12 ++ src/Exception/SettingsNotLocated.php | 16 ++ src/Import/AfterImport/NullAfterImport.php | 16 ++ src/Import/AfterImportInterface.php | 13 ++ src/Import/DataImportInterface.php | 12 ++ src/Import/DataProviderInterface.php | 17 ++ src/Import/Exception/AlreadyLocked.php | 8 + src/Import/Exception/Error.php | 11 ++ src/Import/Exception/Fatal.php | 11 ++ src/Import/Exception/ImportException.php | 9 + src/Import/Exception/Omit.php | 11 ++ src/Import/Lock/FileLock.php | 66 +++++++ src/Import/Lock/NullLock.php | 31 ++++ src/Import/LockInterface.php | 25 +++ src/Import/LoggerHandlerInterface.php | 40 +++++ src/Import/PrepareImportDataInterface.php | 12 ++ src/Import/Response/SimpleResponse.php | 46 +++++ src/Import/ResponseInterface.php | 19 ++ src/Import/Run.php | 163 ++++++++++++++++++ src/Import/Run/LoggerHandler.php | 63 +++++++ src/Import/Run/NullLoggerHandler.php | 49 ++++++ src/Import/Run/Options.php | 27 +++ src/Import/RunHandler/ConsoleHandler.php | 30 ++++ src/Import/RunHandler/NullHandler.php | 27 +++ src/Import/RunHandlerInterface.php | 21 +++ src/Import/SimpleRun.php | 23 +++ src/Migration/AbstractMigration.php | 42 +++++ src/Migration/MigrationFileInterface.php | 15 ++ src/Migration/MigrationRunner.php | 26 +++ src/Model/BaseService.php | 19 +- src/Model/EntitySettingsLocator.php | 35 ++++ src/Model/Indices/Delete.php | 44 +++++ src/Model/Insert/PrepareEntityArray.php | 4 + src/Settings/IndexConfigInterface.php | 10 ++ tests/SpameriTests/Data/Config/Video.neon | 3 +- .../Data/Entity/Video/Identification.php | 2 + .../2019/09/24/BasicData/Initialize.php | 28 +++ .../2019/09/24/Structure/Initialize.php | 65 +++++++ 54 files changed, 1557 insertions(+), 20 deletions(-) create mode 100644 src/Commands/InitializeSettings.php create mode 100644 src/Commands/Migrate.php rename src/DI/{ElasticSearchExtension.php => SpameriElasticSearchExtension.php} (96%) create mode 100644 src/Entity/AbstractImport.php create mode 100644 src/Entity/Import/ArrayValue.php create mode 100644 src/Entity/Import/BoolValue.php create mode 100644 src/Entity/Import/DateValue.php create mode 100644 src/Entity/Import/EmptyValue.php create mode 100644 src/Entity/Import/FloatValue.php create mode 100644 src/Entity/Import/IntegerValue.php create mode 100644 src/Entity/Import/NoValue.php create mode 100644 src/Entity/Import/StringValue.php create mode 100644 src/Entity/Import/ValidationObjectInterface.php create mode 100644 src/Entity/Import/ValidationPropertyInterface.php create mode 100644 src/Exception/SettingsNotLocated.php create mode 100644 src/Import/AfterImport/NullAfterImport.php create mode 100644 src/Import/AfterImportInterface.php create mode 100644 src/Import/DataImportInterface.php create mode 100644 src/Import/DataProviderInterface.php create mode 100644 src/Import/Exception/AlreadyLocked.php create mode 100644 src/Import/Exception/Error.php create mode 100644 src/Import/Exception/Fatal.php create mode 100644 src/Import/Exception/ImportException.php create mode 100644 src/Import/Exception/Omit.php create mode 100644 src/Import/Lock/FileLock.php create mode 100644 src/Import/Lock/NullLock.php create mode 100644 src/Import/LockInterface.php create mode 100644 src/Import/LoggerHandlerInterface.php create mode 100644 src/Import/PrepareImportDataInterface.php create mode 100644 src/Import/Response/SimpleResponse.php create mode 100644 src/Import/ResponseInterface.php create mode 100644 src/Import/Run.php create mode 100644 src/Import/Run/LoggerHandler.php create mode 100644 src/Import/Run/NullLoggerHandler.php create mode 100644 src/Import/Run/Options.php create mode 100644 src/Import/RunHandler/ConsoleHandler.php create mode 100644 src/Import/RunHandler/NullHandler.php create mode 100644 src/Import/RunHandlerInterface.php create mode 100644 src/Import/SimpleRun.php create mode 100644 src/Migration/AbstractMigration.php create mode 100644 src/Migration/MigrationFileInterface.php create mode 100644 src/Migration/MigrationRunner.php create mode 100644 src/Model/EntitySettingsLocator.php create mode 100644 src/Model/Indices/Delete.php create mode 100644 src/Settings/IndexConfigInterface.php create mode 100644 tests/SpameriTests/Elastic/Migration/migrations/2019/09/24/BasicData/Initialize.php create mode 100644 tests/SpameriTests/Elastic/Migration/migrations/2019/09/24/Structure/Initialize.php diff --git a/composer.json b/composer.json index 2957ffe..48698b6 100644 --- a/composer.json +++ b/composer.json @@ -54,6 +54,9 @@ "autoload-dev": { "psr-4": { "SpameriTests\\": "tests/SpameriTests" - } + }, + "classmap": [ + "tests/SpameriTests/Elastic/Migration/migrations" + ] } } diff --git a/src/Commands/InitializeSettings.php b/src/Commands/InitializeSettings.php new file mode 100644 index 0000000..0984a2f --- /dev/null +++ b/src/Commands/InitializeSettings.php @@ -0,0 +1,54 @@ +indexConfig = $indexConfig; + } + + public function configure(): void + { + $this + ->setName('spameri:elastic:initialize-settings') + ->setDescription('Creates index and puts mapping and settings for entity/ies.') + ; + } + + + public function execute( + \Symfony\Component\Console\Input\InputInterface $input, + \Symfony\Component\Console\Output\OutputInterface $output + ) + { + $output->writeln(); + foreach ($this->indexConfig as $indexConfig) { + $output->writeln(); + $this->settinsCreator->create($indexConfig->provide()); + $output->writeln(); + } + + + $output->writeln(); + // foreach neon configs + $output->writeln(); + + + $output->writeln(); + // foreach annotation configs + $output->writeln(); + } + +} diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php new file mode 100644 index 0000000..de166f5 --- /dev/null +++ b/src/Commands/Migrate.php @@ -0,0 +1,38 @@ +setName('spameri:elastic:migrate') + ->setDescription('Runs migrations from files.') + ->addArgument('filename', \Symfony\Component\Console\Input\InputArgument::OPTIONAL) + ; + } + + + protected function execute( + \Symfony\Component\Console\Input\InputInterface $input + , \Symfony\Component\Console\Output\OutputInterface $output + ) + { + $output->writeln('Starting'); + + // 1. Get folder + // 2. Iterate folder + // 3. Run each file in folder + // 3a. Check if file was executed - skip + // 3b. Check if file was changed - skip and report + // 4. Save executed files to ES + // 5. Done + + } +} diff --git a/src/Config/Elastic.neon b/src/Config/Elastic.neon index c6edb01..01bfb5f 100644 --- a/src/Config/Elastic.neon +++ b/src/Config/Elastic.neon @@ -59,6 +59,9 @@ services: indicesCreate: class: Spameri\Elastic\Model\Indices\Create + indicesDelete: + class: Spameri\Elastic\Model\Indices\Delete + serviceLocator: class: Spameri\Elastic\Model\ServiceLocator @@ -75,7 +78,7 @@ services: class: Spameri\Elastic\ClientProvider elasticPanelLogger: - class: Spameri\Elastic\Diagnostics\PanelLogger(@elasticSearch.nullLogger) + class: Spameri\Elastic\Diagnostics\PanelLogger(@spameriElasticSearch.nullLogger) nullLogger: class: Psr\Log\NullLogger @@ -173,10 +176,28 @@ services: clientBuilder: class: Elasticsearch\ClientBuilder setup: - - setLogger(@elasticSearch.elasticPanelLogger) + - setLogger(@spameriElasticSearch.elasticPanelLogger) dateTimeProvider: - class: Spameri\Elastic\Provider\DateTimeProvider(@elasticSearch.dateTime) + class: Spameri\Elastic\Provider\DateTimeProvider(@spameriElasticSearch.dateTime) dateTime: class: \DateTimeImmutable + + nullOutput: + class: Symfony\Component\Console\Output\NullOutput + + consoleOutput: + class: Symfony\Component\Console\Output\ConsoleOutput + + nullLoggerHandler: + class: Spameri\Elastic\Import\Run\NullLoggerHandler + + nullLock: + class: Spameri\Elastic\Import\Lock\NullLock + + nullHandler: + class: Spameri\Elastic\Import\RunHandler\NullHandler + + nullAfterImport: + class: Spameri\Elastic\Import\AfterImport\NullAfterImport diff --git a/src/DI/ElasticSearchExtension.php b/src/DI/SpameriElasticSearchExtension.php similarity index 96% rename from src/DI/ElasticSearchExtension.php rename to src/DI/SpameriElasticSearchExtension.php index d330271..423caba 100644 --- a/src/DI/ElasticSearchExtension.php +++ b/src/DI/SpameriElasticSearchExtension.php @@ -3,7 +3,7 @@ namespace Spameri\Elastic\DI; -class ElasticSearchExtension extends \Nette\DI\CompilerExtension +class SpameriElasticSearchExtension extends \Nette\DI\CompilerExtension { public $defaults = [ @@ -100,7 +100,7 @@ public function loadConfiguration() : void $config = \Nette\DI\Config\Helpers::merge($this->getConfig(), $this->defaults); $config = $this->toggleSynonymAnalyzer($config); - $this->compiler->getContainerBuilder()->parameters['elasticSearch'] = $config; + $this->compiler->getContainerBuilder()->parameters['spameriElasticSearch'] = $config; $services = $this->loadFromFile(__DIR__ . '/../Config/Elastic.neon'); diff --git a/src/Diagnostics/PanelLogger.php b/src/Diagnostics/PanelLogger.php index 11b17c9..f96c625 100644 --- a/src/Diagnostics/PanelLogger.php +++ b/src/Diagnostics/PanelLogger.php @@ -148,6 +148,8 @@ private function logQuery( ) : void { if (isset($context['method'], $context['uri'])) { + $path = \explode('9200', $context['uri']); + $context['uri'] = $path[1] ?? $context['uri']; $this->queries[] = $context; } } diff --git a/src/Entity/AbstractImport.php b/src/Entity/AbstractImport.php new file mode 100644 index 0000000..8c703c2 --- /dev/null +++ b/src/Entity/AbstractImport.php @@ -0,0 +1,59 @@ +key = $key; + } + + + public function key() + { + return $this->key; + } + + + public function entityVariables(): array + { + $vars = \get_object_vars($this); + unset($vars['key']); + return $vars; + } + + + public function toArray(): array + { + $array = []; + + foreach ($this->entityVariables() as $key => $variable) { + if ($variable instanceof \Spameri\Elastic\Entity\Import\NoValue) { + continue; + } + + if ($variable instanceof \Spameri\Elastic\Entity\Import\ValidationPropertyInterface) { + $array[$variable->key()] = $variable->getValue(); + + } elseif ($variable instanceof \Spameri\Elastic\Entity\Import\ValidationObjectInterface) { + $array[$variable->key()] = $variable->toArray(); + + } else { + $array[$key] = $variable; + } + } + + return $array; + } + +} diff --git a/src/Entity/Import/ArrayValue.php b/src/Entity/Import/ArrayValue.php new file mode 100644 index 0000000..c02de34 --- /dev/null +++ b/src/Entity/Import/ArrayValue.php @@ -0,0 +1,40 @@ +array = $array; + $this->key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): array + { + return $this->array; + } + +} diff --git a/src/Entity/Import/BoolValue.php b/src/Entity/Import/BoolValue.php new file mode 100644 index 0000000..2d0b7f8 --- /dev/null +++ b/src/Entity/Import/BoolValue.php @@ -0,0 +1,41 @@ +value = $value; + $this->key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): bool + { + return $this->value; + } + +} diff --git a/src/Entity/Import/DateValue.php b/src/Entity/Import/DateValue.php new file mode 100644 index 0000000..151fa1f --- /dev/null +++ b/src/Entity/Import/DateValue.php @@ -0,0 +1,48 @@ +value = $value; + $this->key = $key; + $this->format = $format; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): string + { + return $this->value->format($this->format); + } + +} diff --git a/src/Entity/Import/EmptyValue.php b/src/Entity/Import/EmptyValue.php new file mode 100644 index 0000000..5edfca2 --- /dev/null +++ b/src/Entity/Import/EmptyValue.php @@ -0,0 +1,34 @@ +key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue() + { + return NULL; + } + +} diff --git a/src/Entity/Import/FloatValue.php b/src/Entity/Import/FloatValue.php new file mode 100644 index 0000000..79059ff --- /dev/null +++ b/src/Entity/Import/FloatValue.php @@ -0,0 +1,41 @@ +value = $value; + $this->key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): float + { + return $this->value; + } + +} diff --git a/src/Entity/Import/IntegerValue.php b/src/Entity/Import/IntegerValue.php new file mode 100644 index 0000000..fdad169 --- /dev/null +++ b/src/Entity/Import/IntegerValue.php @@ -0,0 +1,41 @@ +value = $value; + $this->key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): int + { + return $this->value; + } + +} diff --git a/src/Entity/Import/NoValue.php b/src/Entity/Import/NoValue.php new file mode 100644 index 0000000..1d52adf --- /dev/null +++ b/src/Entity/Import/NoValue.php @@ -0,0 +1,19 @@ +value = $value; + $this->key = $key; + } + + + public function key(): string + { + return $this->key; + } + + + public function getValue(): string + { + return $this->value; + } + +} diff --git a/src/Entity/Import/ValidationObjectInterface.php b/src/Entity/Import/ValidationObjectInterface.php new file mode 100644 index 0000000..0a0ea2d --- /dev/null +++ b/src/Entity/Import/ValidationObjectInterface.php @@ -0,0 +1,12 @@ +lockDir = $lockDir; + } + + + public function setRunName(string $runName): void + { + $this->runName = $runName; + } + + + public function acquire(int $time): \Spameri\Elastic\Import\LockInterface + { + $lockReleaseTime = \file_get_contents($this->getFileName()); + if ( + $lockReleaseTime + && $lockReleaseTime > \time() + ) { + throw new \Spameri\Elastic\Import\Exception\Fatal('Already locked.'); + } + + \file_put_contents($this->getFileName(), \time() + $time); + + return $this; + } + + + public function release(): void + { + \unlink($this->getFileName()); + } + + + public function extend(int $time): void + { + \file_put_contents($this->getFileName(), \time() + $time); + } + + + public function getFileName(): string + { + return $this->lockDir . \DIRECTORY_SEPARATOR . $this->runName; + } + +} diff --git a/src/Import/Lock/NullLock.php b/src/Import/Lock/NullLock.php new file mode 100644 index 0000000..d623c57 --- /dev/null +++ b/src/Import/Lock/NullLock.php @@ -0,0 +1,31 @@ +response = $response; + $this->entity = $entity; + } + + + public function isSuccessful(): bool + { + return $this->response ? TRUE : FALSE; + } + + + public function getResponse() + { + return $this->response; + } + + + public function getEntity($entity) + { + return $this->entity; + } + +} diff --git a/src/Import/ResponseInterface.php b/src/Import/ResponseInterface.php new file mode 100644 index 0000000..02c5c5a --- /dev/null +++ b/src/Import/ResponseInterface.php @@ -0,0 +1,19 @@ +output = $output; + $this->lock = $lock; + $this->loggerHandler = $loggerHandler; + $this->runHandler = $runHandler; + $this->dataProvider = $dataProvider; + $this->prepareImportData = $prepareImportData; + $this->dataImport = $dataImport; + $this->afterImport = $afterImport; + + $this->runName = (new \ReflectionClass($this))->getShortName(); + $this->lock->setRunName($this->runName); + $this->setUpLogger($logDir); + } + + + protected function setUpLogger($logDir): void + { + $directory = $logDir; + \Nette\Utils\FileSystem::createDir($directory); + + $directory .= \DIRECTORY_SEPARATOR; + $directory .= $this->runName; + \Nette\Utils\FileSystem::createDir($directory); + + $directory .= \DIRECTORY_SEPARATOR; + $directory .= (new \DateTime())->format('Y-m-d'); + \Nette\Utils\FileSystem::createDir($directory); + + $this->fileName = (new \DateTime())->format('H-i-s'); + } + + + public function execute( + \Spameri\Elastic\Import\Run\Options $options + ): void + { + $this->lock->acquire($options->lockDuration()); + + $this->initializeProgressBar($this->dataProvider->count($options)); + + $data = $this->dataProvider->provide($options); + + foreach ($data as $item) { + try { + $this->loggerHandler->logItemStart($item); + + $prepared = $this->prepareImportData->prepare($item); + $this->loggerHandler->logPrepared($prepared); + + $response = $this->dataImport->import($prepared); + $this->loggerHandler->logResponse($response); + + $this->afterImport->process($item, $response); + + $this->runHandler->advance($this->runName, $this->progressBar, $prepared); + + $this->lock->extend($options->lockDuration()); + + } catch (\Spameri\Elastic\Import\Exception\Omit $exception) { + $this->loggerHandler->logOmitException($exception); + + } catch (\Spameri\Elastic\Import\Exception\Error $exception) { + $this->loggerHandler->logErrorException($exception); + + } catch (\Spameri\Elastic\Import\Exception\Fatal $exception) { + $this->loggerHandler->logFatalException($exception); + } + } + + $this->runHandler->finish($this->runName, $this->progressBar, $prepared ?? NULL); + + $this->lock->release(); + + $this->loggerHandler->logFinish(); + } + + + public function initializeProgressBar(int $maxCount): void + { + $this->progressBar = new \Symfony\Component\Console\Helper\ProgressBar($this->output, $maxCount); + $this->progressBar->setFormat('debug'); + $this->progressBar->start($maxCount); + } + +} diff --git a/src/Import/Run/LoggerHandler.php b/src/Import/Run/LoggerHandler.php new file mode 100644 index 0000000..64b605d --- /dev/null +++ b/src/Import/Run/LoggerHandler.php @@ -0,0 +1,63 @@ +logger = $logger; + } + + + public function logItemStart($item): void + { + $this->logger->debug('Processing item ' . \Tracy\Dumper::toText($item)); + } + + + public function logPrepared(\Spameri\Elastic\Entity\AbstractImport $import): void + { + $this->logger->debug('Prepared data ' . \Tracy\Dumper::toText($import->toArray())); + } + + + public function logResponse(\Spameri\Elastic\Import\ResponseInterface $result): void + { + $this->logger->debug('Response ' . \Tracy\Dumper::toText($result)); + } + + + public function logOmitException(\Spameri\Elastic\Import\Exception\ImportException $exception): void + { + $this->logger->notice($exception->getMessage()); + } + + + public function logErrorException(\Spameri\Elastic\Import\Exception\ImportException $exception): void + { + $this->logger->error($exception->getMessage()); + } + + + public function logFatalException(\Spameri\Elastic\Import\Exception\ImportException $exception): void + { + $this->logger->critical($exception->getMessage()); + } + + + public function logFinish(): void + { + $this->logger->info('Finished'); + } + +} diff --git a/src/Import/Run/NullLoggerHandler.php b/src/Import/Run/NullLoggerHandler.php new file mode 100644 index 0000000..092dc44 --- /dev/null +++ b/src/Import/Run/NullLoggerHandler.php @@ -0,0 +1,49 @@ +lockDuration = $lockDuration; + } + + + public function lockDuration(): int + { + return $this->lockDuration; + } + +} diff --git a/src/Import/RunHandler/ConsoleHandler.php b/src/Import/RunHandler/ConsoleHandler.php new file mode 100644 index 0000000..bd71ef8 --- /dev/null +++ b/src/Import/RunHandler/ConsoleHandler.php @@ -0,0 +1,30 @@ +advance(); + if ($progressBar->getProgress() % 100) { + $progressBar->display(); + } + } + + + public function finish( + string $runName, + \Symfony\Component\Console\Helper\ProgressBar $progressBar, + ?\Spameri\Elastic\Entity\AbstractImport $lastProcessed + ): void + { + $progressBar->finish(); + } + +} diff --git a/src/Import/RunHandler/NullHandler.php b/src/Import/RunHandler/NullHandler.php new file mode 100644 index 0000000..991b8e2 --- /dev/null +++ b/src/Import/RunHandler/NullHandler.php @@ -0,0 +1,27 @@ +clientProvider = $clientProvider; + $this->output = $output; + } + + + public function before(): void + { + $this->output->writeln('Starting migration ' . self::class); + $this->output->writeln('Running before scripts'); + } + + + public function after(): void + { + $this->output->writeln('Running after scripts'); + $this->output->writeln('Finished migration ' . self::class); + } + +} diff --git a/src/Migration/MigrationFileInterface.php b/src/Migration/MigrationFileInterface.php new file mode 100644 index 0000000..32710df --- /dev/null +++ b/src/Migration/MigrationFileInterface.php @@ -0,0 +1,15 @@ +run($file); + } + } + + + public function run( + MigrationFileInterface $file + ): void + { + $file->before(); + $file->migrate(); + $file->after(); + } + +} diff --git a/src/Model/BaseService.php b/src/Model/BaseService.php index c34ab3f..0cfa14f 100644 --- a/src/Model/BaseService.php +++ b/src/Model/BaseService.php @@ -41,11 +41,6 @@ abstract class BaseService implements IService */ protected $getAllBy; - /** - * @var array - */ - protected $entityProperties; - /** * @var \Spameri\Elastic\Factory\IEntityFactory */ @@ -63,16 +58,15 @@ abstract class BaseService implements IService public function __construct( string $index - , array $entityProperties , \Spameri\Elastic\Factory\IEntityFactory $entityFactory , \Spameri\Elastic\Factory\ICollectionFactory $collectionFactory , \Spameri\Elastic\ClientProvider $client - , Insert $insert - , Get $get - , GetBy $getBy - , GetAllBy $getAllBy - , Delete $delete - , Aggregate $aggregate + , \Spameri\Elastic\Model\Insert $insert + , \Spameri\Elastic\Model\Get $get + , \Spameri\Elastic\Model\GetBy $getBy + , \Spameri\Elastic\Model\GetAllBy $getAllBy + , \Spameri\Elastic\Model\Delete $delete + , \Spameri\Elastic\Model\Aggregate $aggregate ) { $this->client = $client->client(); @@ -82,7 +76,6 @@ public function __construct( $this->delete = $delete; $this->getBy = $getBy; $this->getAllBy = $getAllBy; - $this->entityProperties = $entityProperties; $this->entityFactory = $entityFactory; $this->collectionFactory = $collectionFactory; $this->aggregate = $aggregate; diff --git a/src/Model/EntitySettingsLocator.php b/src/Model/EntitySettingsLocator.php new file mode 100644 index 0000000..990031d --- /dev/null +++ b/src/Model/EntitySettingsLocator.php @@ -0,0 +1,35 @@ +indexConfig = $indexConfig; + } + + + public function locate($indexName) : \Spameri\ElasticQuery\Mapping\Settings + { + foreach ($this->indexConfig as $indexConfig) { + if ($indexConfig->provide()->indexName() === $indexName) { + return $indexConfig->provide(); + } + } + + throw new \Spameri\Elastic\Exception\SettingsNotLocated($indexName); + } + +} diff --git a/src/Model/Indices/Delete.php b/src/Model/Indices/Delete.php new file mode 100644 index 0000000..b0bae33 --- /dev/null +++ b/src/Model/Indices/Delete.php @@ -0,0 +1,44 @@ +clientProvider = $clientProvider; + } + + + public function execute( + string $index + ) : array + { + try { + /** @var array $result */ + $result = $this->clientProvider->client()->indices()->delete( + ( + new \Spameri\ElasticQuery\Document( + $index + ) + )->toArray() + ); + + return $result; + + } catch (\Elasticsearch\Common\Exceptions\ElasticsearchException $exception) { + throw new \Spameri\Elastic\Exception\ElasticSearch($exception->getMessage()); + } + } + +} diff --git a/src/Model/Insert/PrepareEntityArray.php b/src/Model/Insert/PrepareEntityArray.php index 645a8a9..cb0dbcb 100644 --- a/src/Model/Insert/PrepareEntityArray.php +++ b/src/Model/Insert/PrepareEntityArray.php @@ -91,10 +91,14 @@ public function iterateVariables( \is_string($property) || \is_int($property) || \is_bool($property) + || \is_float($property) || $property === NULL ) { $preparedArray[$key] = $property; + } elseif (\is_array($property)) { + $preparedArray[$key] = $this->iterateVariables($property); + } elseif ($property instanceof \Spameri\Elastic\Entity\DateTimeInterface) { $preparedArray[$key] = $property->format(); diff --git a/src/Settings/IndexConfigInterface.php b/src/Settings/IndexConfigInterface.php new file mode 100644 index 0000000..3cfac8f --- /dev/null +++ b/src/Settings/IndexConfigInterface.php @@ -0,0 +1,10 @@ + 1, + 'name' => 'Avengers', + 'year' => 2019, + 'released' => TRUE, + ]); + $this->clientProvider->client()->index($document->toArray()); + } + +} diff --git a/tests/SpameriTests/Elastic/Migration/migrations/2019/09/24/Structure/Initialize.php b/tests/SpameriTests/Elastic/Migration/migrations/2019/09/24/Structure/Initialize.php new file mode 100644 index 0000000..ce179c5 --- /dev/null +++ b/tests/SpameriTests/Elastic/Migration/migrations/2019/09/24/Structure/Initialize.php @@ -0,0 +1,65 @@ +delete = $delete; + $this->create = $create; + $this->settings = $settings; + $this->mapping = $mapping; + } + + + public function before(): void + { + parent::before(); + $this->delete->execute('spameri'); + } + + + public function migrate(): void + { + $this->output->writeln('Creating mapping'); + $this->create->execute('spameri', []); + + $this->output->writeln('Put settings'); + $this->settings->execute('spameri', []); + + $this->output->writeln('Put mapping'); + $this->mapping->execute('spameri', []); + } + +}