diff --git a/Client/Connection.php b/Client/Connection.php index db96d74d..f1ad9d68 100644 --- a/Client/Connection.php +++ b/Client/Connection.php @@ -245,14 +245,17 @@ public function scroll($scrollId, $scrollDuration) * Creates fresh elasticsearch index. * * @param bool $putWarmers Determines if warmers should be loaded. + * @param bool $noMapping Determines if mapping should be included. */ - public function createIndex($putWarmers = false) + public function createIndex($putWarmers = false, $noMapping = false) { $this->isReadOnly('Create index'); $settings = $this->settings; - unset($settings['body']['mappings']); + if ($noMapping) { + unset($settings['body']['mappings']); + } $this->client->indices()->create($settings); if ($putWarmers) { @@ -361,8 +364,9 @@ public function updateTypes(array $types = []) * Tries to drop and create fresh elasticsearch index. * * @param bool $putWarmers Determines if warmers should be loaded. + * @param bool $noMapping Determines if mapping should be included. */ - public function dropAndCreateIndex($putWarmers = false) + public function dropAndCreateIndex($putWarmers = false, $noMapping = false) { try { $this->dropIndex(); @@ -370,7 +374,7 @@ public function dropAndCreateIndex($putWarmers = false) // Do nothing because I'm only trying. } - $this->createIndex($putWarmers); + $this->createIndex($putWarmers, $noMapping); } /** diff --git a/Command/IndexCreateCommand.php b/Command/IndexCreateCommand.php index 48689bbf..caaf413f 100644 --- a/Command/IndexCreateCommand.php +++ b/Command/IndexCreateCommand.php @@ -32,7 +32,8 @@ protected function configure() ->setName('ongr:es:index:create') ->setDescription('Creates elasticsearch index.') ->addOption('time', 't', InputOption::VALUE_NONE, 'Adds date suffix to new index name') - ->addOption('with-warmers', 'w', InputOption::VALUE_NONE, 'Puts warmers into index'); + ->addOption('with-warmers', 'w', InputOption::VALUE_NONE, 'Puts warmers into index') + ->addOption('no-mapping', 'm', InputOption::VALUE_NONE, 'Do not include mapping'); } /** @@ -47,9 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $finder = $this->getContainer()->get('es.client.index_suffix_finder'); $finder->setNextFreeIndex($connection); } - - $connection->createIndex($input->getOption('with-warmers')); - + $connection->createIndex($input->getOption('with-warmers'), $input->getOption('no-mapping') ? true : false); $output->writeln( sprintf( 'Created index for manager named `%s`', diff --git a/Test/AbstractElasticsearchTestCase.php b/Test/AbstractElasticsearchTestCase.php index 60a1bcd4..7342471d 100644 --- a/Test/AbstractElasticsearchTestCase.php +++ b/Test/AbstractElasticsearchTestCase.php @@ -244,7 +244,6 @@ protected function getManager($name = 'default', $createIndex = true, array $cus // Drops and creates index. if ($createIndex) { $connection->dropAndCreateIndex(); - $connection->createTypes(); } // Populates elasticsearch index with data. diff --git a/Tests/Functional/Client/ConnectionTest.php b/Tests/Functional/Client/ConnectionTest.php index d84e24a1..e89e8660 100644 --- a/Tests/Functional/Client/ConnectionTest.php +++ b/Tests/Functional/Client/ConnectionTest.php @@ -204,7 +204,7 @@ public function testWarmers() { $manager = $this->getManager('default', false); $connection = $manager->getConnection(); - $connection->dropAndCreateIndex(true); + $connection->dropAndCreateIndex(true, false); $warmers = $connection->getClient()->indices()->getWarmer( [ diff --git a/Tests/Functional/Command/CreateIndexCommandTest.php b/Tests/Functional/Command/CreateIndexCommandTest.php index ad390d0f..1ac60b41 100644 --- a/Tests/Functional/Command/CreateIndexCommandTest.php +++ b/Tests/Functional/Command/CreateIndexCommandTest.php @@ -30,6 +30,7 @@ public function getTestExecuteData() [ 'timestamp' => false, 'warm' => false, + 'noMapping' => true, ], ], [ @@ -37,6 +38,15 @@ public function getTestExecuteData() [ 'timestamp' => false, 'warm' => true, + 'noMapping' => false, + ], + ], + [ + 'default', + [ + 'timestamp' => false, + 'warm' => true, + 'noMapping' => true, ], ], ]; @@ -75,9 +85,12 @@ public function testExecute($argument, $options) if ($options['warm']) { $arguments['--with-warmers'] = null; } - + if ($options['noMapping']) { + $arguments['--no-mapping'] = null; + } $commandTester->execute($arguments); - + $mapping = $connection->getMappingFromIndex(); + $this->assertEquals($options['noMapping'], empty($mapping)); $this->assertTrue($connection->indexExists(), 'Index should exist.'); $connection->dropIndex(); } diff --git a/Tests/Functional/Command/TypeCreateCommandTest.php b/Tests/Functional/Command/TypeCreateCommandTest.php index 7df422e7..77ad2072 100644 --- a/Tests/Functional/Command/TypeCreateCommandTest.php +++ b/Tests/Functional/Command/TypeCreateCommandTest.php @@ -74,7 +74,7 @@ public function testExecute($arguments, $message, $checkMapping = true) ->getContainer() ->get('es.manager.' . $manager) ->getConnection(); - $connection->dropAndCreateIndex(); + $connection->dropAndCreateIndex(false, true); $this->assertEquals($message, $this->runCreateCommand($arguments)); @@ -134,7 +134,7 @@ public function testExecuteOnExistingTypes($arguments, $message, $createTypes = ->getContainer() ->get('es.manager.' . $manager) ->getConnection(); - $connection->dropAndCreateIndex(); + $connection->dropAndCreateIndex(false, true); $connection->createTypes($createTypes); $this->assertEquals($message, $this->runCreateCommand($arguments));