Rename mappings to properties maintaining BC

Fixes #407
This commit is contained in:
Tim Nagel 2014-05-25 17:34:44 +10:00
parent be89ccf825
commit c38dc107e7
5 changed files with 74 additions and 41 deletions

View file

@ -175,6 +175,16 @@ class Configuration implements ConfigurationInterface
->useAttributeAsKey('name')
->prototype('array')
->treatNullLike(array())
// BC - Renaming 'mappings' node to 'properties'
->beforeNormalization()
->ifTrue(function($v) { return isset($v['mappings']); })
->then(function($v) {
$v['properties'] = $v['mappings'];
unset($v['mappings']);
return $v;
})
->end()
->children()
->scalarNode('index_analyzer')->end()
->scalarNode('search_analyzer')->end()
@ -182,7 +192,7 @@ class Configuration implements ConfigurationInterface
->append($this->getSerializerNode())
->end()
->append($this->getIdNode())
->append($this->getMappingsNode())
->append($this->getPropertiesNode())
->append($this->getDynamicTemplateNode())
->append($this->getSourceNode())
->append($this->getBoostNode())
@ -198,12 +208,12 @@ class Configuration implements ConfigurationInterface
}
/**
* Returns the array node used for "mappings".
* Returns the array node used for "properties".
*/
protected function getMappingsNode()
protected function getPropertiesNode()
{
$builder = new TreeBuilder();
$node = $builder->root('mappings');
$node = $builder->root('properties');
$nestings = $this->getNestings();

View file

@ -4,7 +4,6 @@ namespace FOS\ElasticaBundle\DependencyInjection;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
@ -129,7 +128,7 @@ class FOSElasticaExtension extends Extension
'index' => new Reference($indexId),
'name_or_alias' => $indexName,
'config' => array(
'mappings' => array()
'properties' => array()
)
);
if ($index['finder']) {
@ -217,30 +216,30 @@ class FOSElasticaExtension extends Extension
}
$container->setDefinition($typeId, $typeDef);
$this->indexConfigs[$indexName]['config']['mappings'][$name] = array(
$this->indexConfigs[$indexName]['config']['properties'][$name] = array(
"_source" => array("enabled" => true), // Add a default setting for empty mapping settings
);
if (isset($type['_id'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_id'] = $type['_id'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_id'] = $type['_id'];
}
if (isset($type['_source'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_source'] = $type['_source'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_source'] = $type['_source'];
}
if (isset($type['_boost'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_boost'] = $type['_boost'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_boost'] = $type['_boost'];
}
if (isset($type['_routing'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_routing'] = $type['_routing'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_routing'] = $type['_routing'];
}
if (isset($type['mappings']) && !empty($type['mappings'])) {
$this->cleanUpMapping($type['mappings']);
$this->indexConfigs[$indexName]['config']['mappings'][$name]['properties'] = $type['mappings'];
if (isset($type['properties']) && !empty($type['properties'])) {
$this->cleanUpProperties($type['properties']);
$this->indexConfigs[$indexName]['config']['properties'][$name]['properties'] = $type['properties'];
$typeName = sprintf('%s/%s', $indexName, $name);
$this->typeFields[$typeName] = $type['mappings'];
$this->typeFields[$typeName] = $type['properties'];
}
if (isset($type['_parent'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_parent'] = array('type' => $type['_parent']['type']);
$this->indexConfigs[$indexName]['config']['properties'][$name]['_parent'] = array('type' => $type['_parent']['type']);
$typeName = sprintf('%s/%s', $indexName, $name);
$this->typeFields[$typeName]['_parent'] = $type['_parent'];
}
@ -248,27 +247,27 @@ class FOSElasticaExtension extends Extension
$this->loadTypePersistenceIntegration($type['persistence'], $container, $typeDef, $indexName, $name);
}
if (isset($type['index_analyzer'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['index_analyzer'] = $type['index_analyzer'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['index_analyzer'] = $type['index_analyzer'];
}
if (isset($type['search_analyzer'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['search_analyzer'] = $type['search_analyzer'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['search_analyzer'] = $type['search_analyzer'];
}
if (isset($type['index'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['index'] = $type['index'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['index'] = $type['index'];
}
if (isset($type['_all'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_all'] = $type['_all'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_all'] = $type['_all'];
}
if (isset($type['_timestamp'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_timestamp'] = $type['_timestamp'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_timestamp'] = $type['_timestamp'];
}
if (isset($type['_ttl'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['_ttl'] = $type['_ttl'];
$this->indexConfigs[$indexName]['config']['properties'][$name]['_ttl'] = $type['_ttl'];
}
if (!empty($type['dynamic_templates'])) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['dynamic_templates'] = array();
$this->indexConfigs[$indexName]['config']['properties'][$name]['dynamic_templates'] = array();
foreach ($type['dynamic_templates'] as $templateName => $templateData) {
$this->indexConfigs[$indexName]['config']['mappings'][$name]['dynamic_templates'][] = array($templateName => $templateData);
$this->indexConfigs[$indexName]['config']['properties'][$name]['dynamic_templates'][] = array($templateName => $templateData);
}
}
}
@ -569,17 +568,17 @@ class FOSElasticaExtension extends Extension
$container->setAlias('fos_elastica.manager', sprintf('fos_elastica.manager.%s', $defaultManagerService));
}
protected function cleanUpMapping(&$mappings)
protected function cleanUpProperties(&$properties)
{
foreach ($mappings as &$fieldProperties) {
foreach ($properties as &$fieldProperties) {
if (empty($fieldProperties['fields'])) {
unset($fieldProperties['fields']);
} else {
$this->cleanUpMapping($fieldProperties['fields']);
$this->cleanUpProperties($fieldProperties['fields']);
}
if (!empty($fieldProperties['properties'])) {
$this->cleanUpMapping($fieldProperties['properties']);
$this->cleanUpProperties($fieldProperties['properties']);
}
}
}

View file

@ -68,7 +68,7 @@ class Resetter
{
$indexConfig = $this->getIndexConfig($indexName);
if (!isset($indexConfig['config']['mappings'][$typeName]['properties'])) {
if (!isset($indexConfig['config']['properties'][$typeName]['properties'])) {
throw new \InvalidArgumentException(sprintf('The mapping for index "%s" and type "%s" does not exist.', $indexName, $typeName));
}
@ -80,7 +80,7 @@ class Resetter
throw $e;
}
}
$mapping = $this->createMapping($indexConfig['config']['mappings'][$typeName]);
$mapping = $this->createMapping($indexConfig['config']['properties'][$typeName]);
$type->setMapping($mapping);
}

View file

@ -160,8 +160,8 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
)
));
$this->assertEquals('string', $configuration['indexes']['test']['types']['test']['mappings']['title']['type']);
$this->assertTrue($configuration['indexes']['test']['types']['test']['mappings']['title']['include_in_all']);
$this->assertEquals('string', $configuration['indexes']['test']['types']['test']['properties']['title']['type']);
$this->assertTrue($configuration['indexes']['test']['types']['test']['properties']['title']['include_in_all']);
}
public function testEmptyPropertiesIndexIsUnset()
@ -210,7 +210,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
$configuration = $processor->processConfiguration(new Configuration(array($config), false), array($config));
$mapping = $configuration['indexes']['test']['types']['test']['mappings'];
$mapping = $configuration['indexes']['test']['types']['test']['properties'];
$this->assertArrayNotHasKey('properties', $mapping['content']);
$this->assertArrayNotHasKey('properties', $mapping['title']);
$this->assertArrayHasKey('properties', $mapping['children']);
@ -233,4 +233,28 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
$this->assertTrue(empty($configuration['clients']['default']['servers'][0]['url']));
}
public function testMappingsRenamedToProperties()
{
$configuration = $this->getConfigs(array(
'clients' => array(
'default' => array('url' => 'http://localhost:9200'),
),
'indexes' => array(
'test' => array(
'types' => array(
'test' => array(
'mappings' => array(
'title' => array(),
'published' => array('type' => 'datetime'),
'body' => null,
)
)
)
)
)
));
$this->assertCount(3, $configuration['indexes']['test']['types']['test']['properties']);
}
}

View file

@ -18,7 +18,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
'foo' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'mappings' => array(
'properties' => array(
'a' => array(
'dynamic_templates' => array(),
'properties' => array(),
@ -30,7 +30,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
'bar' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'mappings' => array(
'properties' => array(
'a' => array('properties' => array()),
'b' => array('properties' => array()),
),
@ -39,7 +39,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
'parent' => array(
'index' => $this->getMockElasticaIndex(),
'config' => array(
'mappings' => array(
'properties' => array(
'a' => array(
'properties' => array(
'field_2' => array()
@ -105,8 +105,8 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
$type->expects($this->once())
->method('delete');
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['mappings']['a']['dynamic_templates']);
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['properties']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['properties']['a']['dynamic_templates']);
$type->expects($this->once())
->method('setMapping')
->with($mapping);
@ -149,8 +149,8 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
new Response(array('error' => 'TypeMissingException[[de_20131022] type[bla] missing]', 'status' => 404)))
));
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['mappings']['a']['dynamic_templates']);
$mapping = Mapping::create($this->indexConfigsByName['foo']['config']['properties']['a']['properties']);
$mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['properties']['a']['dynamic_templates']);
$type->expects($this->once())
->method('setMapping')
->with($mapping);
@ -171,7 +171,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase
$type->expects($this->once())
->method('delete');
$mapping = Mapping::create($this->indexConfigsByName['parent']['config']['mappings']['a']['properties']);
$mapping = Mapping::create($this->indexConfigsByName['parent']['config']['properties']['a']['properties']);
$mapping->setParam('_parent', array('type' => 'b'));
$type->expects($this->once())
->method('setMapping')