Skip to content

Commit

Permalink
Initial implementation of index template support #916
Browse files Browse the repository at this point in the history
� Conflicts:
�	tests/Unit/DependencyInjection/ConfigurationTest.php
�	tests/Unit/Elastica/ClientTest.php
  • Loading branch information
dbalabka committed Jan 30, 2019
1 parent 8b30ba6 commit 9cfd0f1
Show file tree
Hide file tree
Showing 36 changed files with 1,529 additions and 111 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -35,7 +35,8 @@
"symfony/serializer": "^3.4|^4",
"symfony/yaml": "^3.4|^4",
"friendsofphp/php-cs-fixer": "^2.2",
"symfony/web-profiler-bundle": "^3.4|^4.0"
"symfony/web-profiler-bundle": "^3.4|^4.0",
"phpdocumentor/type-resolver": "~0.2.1"
},
"suggest": {
"enqueue/elastica-bundle": "The bundle adds extra features to FOSElasticaBundle bundle. Aimed to improve performance."
Expand Down
86 changes: 86 additions & 0 deletions src/Command/ResetTemplatesCommand.php
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of the FOSElasticaBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Command;

use FOS\ElasticaBundle\Index\TemplateResetter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

/**
* Reset search indexes templates.
*/
final class ResetTemplatesCommand extends Command
{
protected static $defaultName = 'fos:elastica:reset-templates';

/**
* Resetter
*
* @var TemplateResetter
*/
private $resetter;

public function __construct(
TemplateResetter $resetter
) {
parent::__construct();

$this->resetter = $resetter;
}

protected function configure()
{
$this
->setName('fos:elastica:reset-templates')
->addOption(
'index',
null,
InputOption::VALUE_OPTIONAL,
'The index template to reset. If no index template name specified than all templates will be reset',
true
)
->addOption(
'force-delete',
null,
InputOption::VALUE_NONE,
'Delete all indexes that matches index templates patterns. ' .
'Aware that pattern may match various indexes.'
)
->setDescription('Reset search indexes templates')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$indexTemplate = $input->hasParameterOption('--index') ? $input->getOption('index') : null;
$deleteByPattern = (bool) $input->getOption('force-delete');

if ($deleteByPattern) {
$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('You are going to remove all template indexes. Are you sure?', false);
if (!$helper->ask($input, $output, $question)) {
return;
}
}

if (is_string($indexTemplate)) {
$output->writeln(sprintf('<info>Resetting template</info> <comment>%s</comment>', $indexTemplate));
$this->resetter->resetIndex($indexTemplate, $deleteByPattern);
} else {
$output->writeln('<info>Resetting all templates</info>');
$this->resetter->resetAllIndexes($deleteByPattern);
}
}
}
79 changes: 2 additions & 77 deletions src/Configuration/IndexConfig.php
Expand Up @@ -20,36 +20,9 @@

namespace FOS\ElasticaBundle\Configuration;

class IndexConfig
class IndexConfig implements IndexConfigInterface
{
/**
* The name of the index for ElasticSearch.
*
* @var string
*/
private $elasticSearchName;

/**
* The internal name of the index. May not be the same as the name used in ElasticSearch,
* especially if aliases are enabled.
*
* @var string
*/
private $name;

/**
* An array of settings sent to ElasticSearch when creating the index.
*
* @var array
*/
private $settings;

/**
* All types that belong to this index.
*
* @var TypeConfig[]
*/
private $types;
use IndexConfigTrait;

/**
* Indicates if the index should use an alias, allowing an index repopulation to occur
Expand All @@ -75,54 +48,6 @@ public function __construct($name, array $types, array $config)
$this->useAlias = isset($config['useAlias']) ? $config['useAlias'] : false;
}

/**
* @return string
*/
public function getElasticSearchName()
{
return $this->elasticSearchName;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getSettings()
{
return $this->settings;
}

/**
* @param string $typeName
*
* @return TypeConfig
*
* @throws \InvalidArgumentException
*/
public function getType($typeName)
{
if (!array_key_exists($typeName, $this->types)) {
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
}

return $this->types[$typeName];
}

/**
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
*/
public function getTypes()
{
return $this->types;
}

/**
* @return bool
*/
Expand Down
40 changes: 40 additions & 0 deletions src/Configuration/IndexConfigInterface.php
@@ -0,0 +1,40 @@
<?php

namespace FOS\ElasticaBundle\Configuration;

/**
* Interface Index config interface
*
* @author Dmitry Balabka <dmitry.balabka@intexsys.lv>
*/
interface IndexConfigInterface
{
/**
* @return string
*/
public function getElasticSearchName();

/**
* @return string
*/
public function getName();

/**
* @return array
*/
public function getSettings();

/**
* @param string $typeName
*
* @return TypeConfig
*
* @throws \InvalidArgumentException
*/
public function getType($typeName);

/**
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
*/
public function getTypes();
}
96 changes: 96 additions & 0 deletions src/Configuration/IndexConfigTrait.php
@@ -0,0 +1,96 @@
<?php
/*
* This file is part of the OpCart software.
*
* (c) 2015, OpticsPlanet, Inc
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FOS\ElasticaBundle\Configuration;

/**
* Index configuration trait class
*
* @author Dmitry Balabka <dmitry.balabka@intexsys.lv>
*/
trait IndexConfigTrait
{
/**
* The name of the index for ElasticSearch.
*
* @var string
*/
protected $elasticSearchName;

/**
* The internal name of the index. May not be the same as the name used in ElasticSearch,
* especially if aliases are enabled.
*
* @var string
*/
protected $name;

/**
* An array of settings sent to ElasticSearch when creating the index.
*
* @var array
*/
protected $settings;

/**
* All types that belong to this index.
*
* @var TypeConfig[]
*/
protected $types;

/**
* @return string
*/
public function getElasticSearchName()
{
return $this->elasticSearchName;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @return array
*/
public function getSettings()
{
return $this->settings;
}

/**
* @param string $typeName
*
* @return TypeConfig
*
* @throws \InvalidArgumentException
*/
public function getType($typeName)
{
if (!array_key_exists($typeName, $this->types)) {
throw new \InvalidArgumentException(sprintf('Type "%s" does not exist on index "%s"', $typeName, $this->name));
}

return $this->types[$typeName];
}

/**
* @return \FOS\ElasticaBundle\Configuration\TypeConfig[]
*/
public function getTypes()
{
return $this->types;
}
}
49 changes: 49 additions & 0 deletions src/Configuration/IndexTemplateConfig.php
@@ -0,0 +1,49 @@
<?php

namespace FOS\ElasticaBundle\Configuration;

/**
* Index template configuration class
*
* @author Dmitry Balabka <dmitry.balabka@intexsys.lv>
*/
class IndexTemplateConfig implements IndexConfigInterface
{
use IndexConfigTrait;

/**
* Index name pattern
*
* @var string
*/
private $template;

/**
* Constructor expects an array as generated by the Container Configuration builder.
*
* @param string $name
* @param TypeConfig[] $types
* @param array $config
*/
public function __construct($name, array $types, array $config)
{
$this->elasticSearchName = isset($config['elasticSearchName']) ? $config['elasticSearchName'] : $name;
$this->name = $name;
$this->settings = isset($config['settings']) ? $config['settings'] : array();
if (!isset($config['template'])) {
throw new \InvalidArgumentException('Index template value must be set');
}
$this->template = $config['template'];
$this->types = $types;
}

/**
* Gets index name pattern
*
* @return string
*/
public function getTemplate()
{
return $this->template;
}
}
2 changes: 1 addition & 1 deletion src/Configuration/ManagerInterface.php
Expand Up @@ -19,7 +19,7 @@ interface ManagerInterface
/**
* Returns configuration for an index.
*
* @param $index
* @param string $index
*
* @return IndexConfig
*/
Expand Down

0 comments on commit 9cfd0f1

Please sign in to comment.