Add support for dynamic templates
This commit is contained in:
parent
8f49b246bc
commit
c68bb411ac
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
30
README.md
30
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 }
|
||||
```
|
||||
|
|
|
@ -78,6 +78,10 @@ class Resetter
|
|||
}
|
||||
}
|
||||
|
||||
if (isset($indexConfig['dynamic_templates'])) {
|
||||
$mapping->setParam('dynamic_templates', $indexConfig['dynamic_templates']);
|
||||
}
|
||||
|
||||
return $mapping;
|
||||
}
|
||||
|
||||
|
|
|
@ -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']);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue