diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index d44e714..eb6abd2 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -265,6 +265,7 @@ class Configuration implements ConfigurationInterface ->end() ->end() ->append($this->getMappingsNode()) + ->append($this->getDynamicTemplateNode()) ->append($this->getSourceNode()) ->append($this->getBoostNode()) ->append($this->getRoutingNode()) @@ -296,6 +297,37 @@ class Configuration implements ConfigurationInterface return $node; } + /** + * Returns the array node used for "dynamic_templates". + */ + public function getDynamicTemplateNode() + { + $builder = new TreeBuilder(); + $node = $builder->root('dynamic_templates'); + + $node + ->useAttributeAsKey('name') + ->prototype('array') + ->children() + ->scalarNode('match')->isRequired()->end() + ->scalarNode('match_mapping_type')->end() + ->arrayNode('mapping') + ->isRequired() + ->children() + ->scalarNode('type')->end() + ->scalarNode('index')->end() + ->arrayNode('fields') + ->children() + ->end() + ->end() + ->end() + ->end() + ->end() + ; + + return $node; + } + /** * @param \Symfony\Component\Config\Definition\Builder\NodeBuilder $node The node to which to attach the field config to * @param array $nestings the nested mappings for the current field level diff --git a/DependencyInjection/FOSElasticaExtension.php b/DependencyInjection/FOSElasticaExtension.php index 6d38714..26cb209 100644 --- a/DependencyInjection/FOSElasticaExtension.php +++ b/DependencyInjection/FOSElasticaExtension.php @@ -205,6 +205,12 @@ class FOSElasticaExtension extends Extension if (isset($type['index'])) { $this->indexConfigs[$indexName]['config']['mappings'][$name]['index'] = $type['index']; } + if (!empty($type['dynamic_templates'])) { + $this->indexConfigs[$indexName]['config']['mappings'][$name]['dynamic_templates'] = array(); + foreach ($type['dynamic_templates'] as $templateName => $templateData) { + $this->indexConfigs[$indexName]['config']['mappings'][$name]['dynamic_templates'][] = array($templateName => $templateData); + } + } } } diff --git a/README.md b/README.md index 1c5c5f3..28359f6 100644 --- a/README.md +++ b/README.md @@ -684,3 +684,33 @@ fos_elastica: lastlogin: { type: date, format: basic_date_time } birthday: { type: date, format: "yyyy-MM-dd" } ``` + +#### Dynamic templates + +Dynamic templates allow to define mapping templates that will be +applied when dynamic introduction of fields / objects happens. + +[Documentation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-root-object-type.html#_dynamic_templates) + +```yaml +fos_elastica: + clients: + default: { host: localhost, port: 9200 } + indexes: + site: + types: + user: + dynamic_templates: + my_template_1: + match: apples_* + mapping: + type: float + my_template_2: + match: * + match_mapping_type: string + mapping: + type: string + index: not_analyzed + mappings: + username: { type: string } +``` diff --git a/Resetter.php b/Resetter.php index 7614675..5929cc8 100644 --- a/Resetter.php +++ b/Resetter.php @@ -78,6 +78,10 @@ class Resetter } } + if (isset($indexConfig['dynamic_templates'])) { + $mapping->setParam('dynamic_templates', $indexConfig['dynamic_templates']); + } + return $mapping; } diff --git a/Tests/DependencyInjection/ConfigurationTest.php b/Tests/DependencyInjection/ConfigurationTest.php index f410621..ead9977 100644 --- a/Tests/DependencyInjection/ConfigurationTest.php +++ b/Tests/DependencyInjection/ConfigurationTest.php @@ -31,4 +31,45 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mappings['format']); $this->assertNull($mappings['format']->getDefaultValue()); } + + public function testDynamicTemplateNodes() + { + $tree = $this->configuration->getConfigTree(); + $children = $tree->getChildren(); + $children = $children['indexes']->getPrototype()->getChildren(); + $typeNodes = $children['types']->getPrototype()->getChildren(); + $dynamicTemplates = $typeNodes['dynamic_templates']->getPrototype()->getChildren(); + + $this->assertArrayHasKey('match', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match']); + $this->assertNull($dynamicTemplates['match']->getDefaultValue()); + + $this->assertArrayHasKey('match_mapping_type', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $dynamicTemplates['match_mapping_type']); + $this->assertNull($dynamicTemplates['match_mapping_type']->getDefaultValue()); + + $this->assertArrayHasKey('mapping', $dynamicTemplates); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ArrayNode', $dynamicTemplates['mapping']); + } + + public function testDynamicTemplateMappingNodes() + { + $tree = $this->configuration->getConfigTree(); + $children = $tree->getChildren(); + $children = $children['indexes']->getPrototype()->getChildren(); + $typeNodes = $children['types']->getPrototype()->getChildren(); + $dynamicTemplates = $typeNodes['dynamic_templates']->getPrototype()->getChildren(); + $mapping = $dynamicTemplates['mapping']->getChildren(); + + $this->assertArrayHasKey('type', $mapping); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['type']); + $this->assertNull($mapping['type']->getDefaultValue()); + + $this->assertArrayHasKey('index', $mapping); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ScalarNode', $mapping['index']); + $this->assertNull($mapping['index']->getDefaultValue()); + + $this->assertArrayHasKey('fields', $mapping); + $this->assertInstanceOf('Symfony\Component\Config\Definition\ArrayNode', $mapping['fields']); + } } diff --git a/Tests/ResetterTest.php b/Tests/ResetterTest.php index 61a18a9..cc9154b 100644 --- a/Tests/ResetterTest.php +++ b/Tests/ResetterTest.php @@ -15,7 +15,10 @@ class ResetterTest extends \PHPUnit_Framework_TestCase 'index' => $this->getMockElasticaIndex(), 'config' => array( 'mappings' => array( - 'a' => array('properties' => array()), + 'a' => array( + 'dynamic_templates' => array(), + 'properties' => array(), + ), 'b' => array('properties' => array()), ), ), @@ -92,6 +95,7 @@ class ResetterTest extends \PHPUnit_Framework_TestCase ->method('delete'); $mapping = \Elastica_Type_Mapping::create($this->indexConfigsByName['foo']['config']['mappings']['a']['properties']); + $mapping->setParam('dynamic_templates', $this->indexConfigsByName['foo']['config']['mappings']['a']['dynamic_templates']); $type->expects($this->once()) ->method('setMapping') ->with($mapping);