FOSElasticaBundle/Index/Resetter.php

125 lines
3.5 KiB
PHP
Raw Normal View History

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;
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
/***
* @var \FOS\ElasticaBundle\Configuration\Manager
*/
private $configManager;
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;
public function __construct(ConfigManager $configManager, IndexManager $indexManager, AliasProcessor $aliasProcessor, MappingBuilder $mappingBuilder)
2014-04-21 01:21:50 +02:00
{
2014-06-18 09:13:29 +02:00
$this->aliasProcessor = $aliasProcessor;
2014-06-17 15:22:58 +02:00
$this->configManager = $configManager;
$this->indexManager = $indexManager;
2014-06-18 09:13:29 +02:00
$this->mappingBuilder = $mappingBuilder;
2014-04-21 01:21:50 +02:00
}
/**
2015-03-12 11:20:00 +01:00
* Deletes and recreates all indexes.
2014-04-21 01:21:50 +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) {
$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
*/
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);
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()) {
$this->aliasProcessor->switchIndexAlias($indexConfig, $index, $force);
2014-06-18 09:13:29 +02:00
}
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
try {
$type->delete();
} catch (ResponseException $e) {
if (strpos($e->getMessage(), 'TypeMissingException') === false) {
throw $e;
}
}
2015-03-12 11:20:00 +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);
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
}
}
}