Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions Client/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -361,16 +364,17 @@ 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();
} catch (\Exception $e) {
// Do nothing because I'm only trying.
}

$this->createIndex($putWarmers);
$this->createIndex($putWarmers, $noMapping);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions Command/IndexCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand All @@ -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(
'<info>Created index for manager named `</info><comment>%s</comment><info>`</info>',
Expand Down
1 change: 0 additions & 1 deletion Test/AbstractElasticsearchTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Client/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down
17 changes: 15 additions & 2 deletions Tests/Functional/Command/CreateIndexCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,23 @@ public function getTestExecuteData()
[
'timestamp' => false,
'warm' => false,
'noMapping' => true,
],
],
[
'default',
[
'timestamp' => false,
'warm' => true,
'noMapping' => false,
],
],
[
'default',
[
'timestamp' => false,
'warm' => true,
'noMapping' => true,
],
],
];
Expand Down Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Command/TypeCreateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down Expand Up @@ -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));
Expand Down