2011-04-13 00:14:00 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FOQ\ElasticaBundle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes and recreates indexes
|
2012-03-09 19:02:26 +01:00
|
|
|
*/
|
2012-03-02 06:28:38 +01:00
|
|
|
class Resetter
|
2011-04-13 00:14:00 +02:00
|
|
|
{
|
2012-03-09 19:02:26 +01:00
|
|
|
protected $indexConfigsByName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param array $indexConfigsByName
|
|
|
|
*/
|
|
|
|
public function __construct(array $indexConfigsByName)
|
2011-04-13 00:14:00 +02:00
|
|
|
{
|
2012-03-09 19:02:26 +01:00
|
|
|
$this->indexConfigsByName = $indexConfigsByName;
|
2011-04-13 00:14:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-09 19:02:26 +01:00
|
|
|
* Deletes and recreates all indexes
|
|
|
|
*/
|
|
|
|
public function resetAllIndexes()
|
2011-04-13 00:14:00 +02:00
|
|
|
{
|
2012-03-09 19:02:26 +01:00
|
|
|
foreach ($this->indexConfigsByName as $indexConfig) {
|
2011-09-12 18:28:59 +02:00
|
|
|
$indexConfig['index']->create($indexConfig['config'], true);
|
2011-04-13 00:14:00 +02:00
|
|
|
}
|
|
|
|
}
|
2012-03-09 19:02:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes and recreates the named index
|
|
|
|
*
|
|
|
|
* @param string $indexName
|
|
|
|
* @throws InvalidArgumentException if no index exists for the given name
|
|
|
|
*/
|
|
|
|
public function resetIndex($indexName)
|
|
|
|
{
|
|
|
|
$indexConfig = $this->getIndexConfig($indexName);
|
|
|
|
$indexConfig['index']->create($indexConfig['config'], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes and recreates a mapping type for the named index
|
|
|
|
*
|
|
|
|
* @param string $indexName
|
|
|
|
* @param string $typeName
|
|
|
|
* @throws InvalidArgumentException if no index or type mapping exists for the given names
|
|
|
|
*/
|
|
|
|
public function resetIndexType($indexName, $typeName)
|
|
|
|
{
|
|
|
|
$indexConfig = $this->getIndexConfig($indexName);
|
|
|
|
|
|
|
|
if (!isset($indexConfig['config']['mappings'][$typeName])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The mapping for index "%s" and type "%s" does not exist.', $indexName, $typeName));
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $indexConfig['index']->getType($typeName);
|
|
|
|
$type->delete();
|
|
|
|
$type->setMapping($indexConfig['config']['mappings'][$typeName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets an index config by its name
|
|
|
|
*
|
|
|
|
* @param string $index Index name
|
|
|
|
* @return array
|
|
|
|
* @throws InvalidArgumentException if no index config exists for the given name
|
|
|
|
*/
|
|
|
|
protected function getIndexConfig($indexName)
|
|
|
|
{
|
|
|
|
if (!isset($this->indexConfigsByName[$indexName])) {
|
|
|
|
throw new \InvalidArgumentException(sprintf('The configuration for index "%s" does not exist.', $indexName));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->indexConfigsByName[$indexName];
|
|
|
|
}
|
2011-04-13 00:14:00 +02:00
|
|
|
}
|