2014-04-21 01:21:50 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOS\ElasticaBundle\Index;
|
|
|
|
|
|
|
|
use Elastica\Index;
|
|
|
|
use Elastica\Exception\ResponseException;
|
|
|
|
use Elastica\Type\Mapping;
|
2014-06-18 09:13:29 +02:00
|
|
|
use FOS\ElasticaBundle\Configuration\ConfigManager;
|
2015-03-11 04:30:10 +01:00
|
|
|
use FOS\ElasticaBundle\Event\IndexResetEvent;
|
|
|
|
use FOS\ElasticaBundle\Event\TypeResetEvent;
|
2015-01-04 13:00:12 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2014-04-21 01:21:50 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-12 11:20:00 +01:00
|
|
|
* Deletes and recreates indexes.
|
2014-04-21 01:21:50 +02:00
|
|
|
*/
|
|
|
|
class Resetter
|
|
|
|
{
|
2014-06-18 09:13:29 +02:00
|
|
|
/**
|
|
|
|
* @var AliasProcessor
|
|
|
|
*/
|
|
|
|
private $aliasProcessor;
|
|
|
|
|
2014-06-17 15:22:58 +02:00
|
|
|
/***
|
2015-01-04 13:00:12 +01:00
|
|
|
* @var ConfigManager
|
2014-06-17 15:22:58 +02:00
|
|
|
*/
|
|
|
|
private $configManager;
|
2014-04-21 01:21:50 +02:00
|
|
|
|
2015-03-11 04:30:10 +01:00
|
|
|
/**
|
|
|
|
* @var EventDispatcherInterface
|
|
|
|
*/
|
|
|
|
private $dispatcher;
|
|
|
|
|
2014-04-21 01:21:50 +02:00
|
|
|
/**
|
2014-06-17 15:22:58 +02:00
|
|
|
* @var IndexManager
|
2014-04-21 01:21:50 +02:00
|
|
|
*/
|
2014-06-17 15:22:58 +02:00
|
|
|
private $indexManager;
|
|
|
|
|
2014-06-18 09:13:29 +02:00
|
|
|
/**
|
|
|
|
* @var MappingBuilder
|
|
|
|
*/
|
|
|
|
private $mappingBuilder;
|
|
|
|
|
2015-01-04 13:00:12 +01:00
|
|
|
/**
|
|
|
|
* @param ConfigManager $configManager
|
|
|
|
* @param IndexManager $indexManager
|
|
|
|
* @param AliasProcessor $aliasProcessor
|
|
|
|
* @param MappingBuilder $mappingBuilder
|
|
|
|
* @param EventDispatcherInterface $eventDispatcher
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConfigManager $configManager,
|
|
|
|
IndexManager $indexManager,
|
|
|
|
AliasProcessor $aliasProcessor,
|
|
|
|
MappingBuilder $mappingBuilder,
|
|
|
|
EventDispatcherInterface $eventDispatcher
|
|
|
|
) {
|
2015-03-11 04:30:10 +01:00
|
|
|
$this->aliasProcessor = $aliasProcessor;
|
|
|
|
$this->configManager = $configManager;
|
|
|
|
$this->dispatcher = $eventDispatcher;
|
|
|
|
$this->indexManager = $indexManager;
|
|
|
|
$this->mappingBuilder = $mappingBuilder;
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-12 11:20:00 +01:00
|
|
|
* Deletes and recreates all indexes.
|
2015-01-04 11:58:53 +01:00
|
|
|
*
|
|
|
|
* @param bool $populating
|
|
|
|
* @param bool $force
|
2014-04-21 01:21:50 +02:00
|
|
|
*/
|
2014-08-28 18:59:58 +02:00
|
|
|
public function resetAllIndexes($populating = false, $force = false)
|
2014-04-21 01:21:50 +02:00
|
|
|
{
|
2014-06-17 15:22:58 +02:00
|
|
|
foreach ($this->configManager->getIndexNames() as $name) {
|
2014-08-28 18:59:58 +02:00
|
|
|
$this->resetIndex($name, $populating, $force);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-18 09:13:29 +02:00
|
|
|
* Deletes and recreates the named index. If populating, creates a new index
|
|
|
|
* with a randomised name for an alias to be set after population.
|
2014-04-21 01:21:50 +02:00
|
|
|
*
|
|
|
|
* @param string $indexName
|
2015-03-12 11:20:00 +01:00
|
|
|
* @param bool $populating
|
|
|
|
* @param bool $force If index exists with same name as alias, remove it
|
|
|
|
*
|
2014-04-21 01:21:50 +02:00
|
|
|
* @throws \InvalidArgumentException if no index exists for the given name
|
|
|
|
*/
|
2014-08-28 18:59:58 +02:00
|
|
|
public function resetIndex($indexName, $populating = false, $force = false)
|
2014-04-21 01:21:50 +02:00
|
|
|
{
|
2014-06-17 15:22:58 +02:00
|
|
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
|
|
|
|
$index = $this->indexManager->getIndex($indexName);
|
|
|
|
|
2015-03-14 12:14:24 +01:00
|
|
|
$event = new IndexResetEvent($indexName, $populating, $force);
|
|
|
|
$this->dispatcher->dispatch(IndexResetEvent::PRE_INDEX_RESET, $event);
|
|
|
|
|
2014-06-17 15:22:58 +02:00
|
|
|
if ($indexConfig->isUseAlias()) {
|
2014-06-18 09:13:29 +02:00
|
|
|
$this->aliasProcessor->setRootName($indexConfig, $index);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:13:29 +02:00
|
|
|
$mapping = $this->mappingBuilder->buildIndexMapping($indexConfig);
|
|
|
|
$index->create($mapping, true);
|
2014-06-17 15:22:58 +02:00
|
|
|
|
2014-06-18 09:13:29 +02:00
|
|
|
if (!$populating and $indexConfig->isUseAlias()) {
|
2014-08-28 18:59:58 +02:00
|
|
|
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
|
2014-06-18 09:13:29 +02:00
|
|
|
}
|
2015-01-04 13:00:12 +01:00
|
|
|
|
2015-03-11 04:30:10 +01:00
|
|
|
$this->dispatcher->dispatch(IndexResetEvent::POST_INDEX_RESET, $event);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-12 11:20:00 +01:00
|
|
|
* Deletes and recreates a mapping type for the named index.
|
2014-04-21 01:21:50 +02:00
|
|
|
*
|
|
|
|
* @param string $indexName
|
|
|
|
* @param string $typeName
|
2015-03-12 11:20:00 +01:00
|
|
|
*
|
2014-04-21 01:21:50 +02:00
|
|
|
* @throws \InvalidArgumentException if no index or type mapping exists for the given names
|
|
|
|
* @throws ResponseException
|
|
|
|
*/
|
|
|
|
public function resetIndexType($indexName, $typeName)
|
|
|
|
{
|
2014-06-18 09:13:29 +02:00
|
|
|
$typeConfig = $this->configManager->getTypeConfiguration($indexName, $typeName);
|
|
|
|
$type = $this->indexManager->getIndex($indexName)->getType($typeName);
|
2014-04-21 01:21:50 +02:00
|
|
|
|
2015-03-14 12:14:24 +01:00
|
|
|
$event = new TypeResetEvent($indexName, $typeName);
|
|
|
|
$this->dispatcher->dispatch(TypeResetEvent::PRE_TYPE_RESET, $event);
|
|
|
|
|
2014-04-21 01:21:50 +02:00
|
|
|
try {
|
|
|
|
$type->delete();
|
|
|
|
} catch (ResponseException $e) {
|
|
|
|
if (strpos($e->getMessage(), 'TypeMissingException') === false) {
|
|
|
|
throw $e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 04:30:10 +01:00
|
|
|
$mapping = new Mapping();
|
2014-06-18 09:13:29 +02:00
|
|
|
foreach ($this->mappingBuilder->buildTypeMapping($typeConfig) as $name => $field) {
|
|
|
|
$mapping->setParam($name, $field);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
|
2014-06-18 09:13:29 +02:00
|
|
|
$type->setMapping($mapping);
|
2015-01-04 13:00:12 +01:00
|
|
|
|
2015-03-11 04:30:10 +01:00
|
|
|
$this->dispatcher->dispatch(TypeResetEvent::POST_TYPE_RESET, $event);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-18 09:13:29 +02:00
|
|
|
* A command run when a population has finished.
|
2014-04-21 01:21:50 +02:00
|
|
|
*
|
2014-08-24 11:50:56 +02:00
|
|
|
* @param string $indexName
|
2014-04-21 01:21:50 +02:00
|
|
|
*/
|
2014-06-18 09:13:29 +02:00
|
|
|
public function postPopulate($indexName)
|
2014-04-21 01:21:50 +02:00
|
|
|
{
|
2014-06-18 09:13:29 +02:00
|
|
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName);
|
2014-04-21 01:21:50 +02:00
|
|
|
|
2014-06-18 09:13:29 +02:00
|
|
|
if ($indexConfig->isUseAlias()) {
|
|
|
|
$index = $this->indexManager->getIndex($indexName);
|
|
|
|
$this->aliasProcessor->switchIndexAlias($indexConfig, $index);
|
2014-04-21 01:21:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|