Skip to content

Commit 5d65676

Browse files
committed
Add tests and normalisation to support old dynamic_templates format
1 parent c9a2443 commit 5d65676

File tree

3 files changed

+39
-25
lines changed

3 files changed

+39
-25
lines changed

DependencyInjection/Configuration.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ protected function getTypesNode()
189189
return $v;
190190
})
191191
->end()
192+
// BC - Support the old is_indexable_callback property
192193
->beforeNormalization()
193194
->ifTrue(function ($v) {
194195
return isset($v['persistence']) &&
@@ -202,6 +203,25 @@ protected function getTypesNode()
202203
return $v;
203204
})
204205
->end()
206+
// Support multiple dynamic_template formats to match the old bundle style
207+
// and the way ElasticSearch expects them
208+
->beforeNormalization()
209+
->ifTrue(function ($v) { return isset($v['dynamic_templates']); })
210+
->then(function ($v) {
211+
$dt = array();
212+
foreach ($v['dynamic_templates'] as $key => $type) {
213+
if (is_numeric($key)) {
214+
$dt[$key] = $type;
215+
} else {
216+
$dt[][$key] = $type;
217+
}
218+
}
219+
220+
$v['dynamic_templates'] = $dt;
221+
222+
return $v;
223+
})
224+
->end()
205225
->children()
206226
->scalarNode('index_analyzer')->end()
207227
->scalarNode('search_analyzer')->end()

Index/MappingBuilder.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ public function buildTypeMapping(TypeConfig $typeConfig)
6060
// 'search_analyzer' => $typeConfig->getSearchAnalyzer(),
6161
));
6262

63+
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
64+
unset($mapping['dynamic_templates']);
65+
}
66+
6367
$this->fixProperties($mapping['properties']);
6468

6569
if ($typeConfig->getModel()) {
@@ -69,31 +73,6 @@ public function buildTypeMapping(TypeConfig $typeConfig)
6973
return $mapping;
7074
}
7175

72-
/**
73-
* create type mapping object
74-
*
75-
* @param array $indexConfig
76-
* @return Mapping
77-
*/
78-
protected function createMapping($indexConfig)
79-
{
80-
/*$mapping = $this->createMapping($indexConfig['config']['properties'][$typeName]);*/
81-
$mapping = Mapping::create($indexConfig['properties']);
82-
83-
$mappingSpecialFields = array('_uid', '_id', '_source', '_all', '_analyzer', '_boost', '_routing', '_index', '_size', '_timestamp', '_ttl', 'dynamic_templates');
84-
foreach ($mappingSpecialFields as $specialField) {
85-
if (isset($indexConfig[$specialField])) {
86-
$mapping->setParam($specialField, $indexConfig[$specialField]);
87-
}
88-
}
89-
90-
if (isset($indexConfig['_parent'])) {
91-
$mapping->setParam('_parent', array('type' => $indexConfig['_parent']['type']));
92-
}
93-
94-
return $mapping;
95-
}
96-
9776
/**
9877
* Fixes any properties and applies basic defaults for any field that does not have
9978
* required options.

Tests/Functional/app/Basic/config.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,26 @@ fos_elastica:
2929
max_gram: 5
3030
types:
3131
parent:
32+
dynamic_templates:
33+
dates:
34+
match: "date_*"
35+
mapping:
36+
type: date
3237
mappings:
3338
field1: ~
3439
field2: ~
3540
type:
3641
search_analyzer: my_analyzer
42+
dynamic_templates:
43+
- dates:
44+
match: "date_*"
45+
mapping:
46+
type: date
47+
- strings:
48+
match: "*"
49+
mapping:
50+
analyzer: english
51+
type: string
3752
mappings:
3853
field1: ~
3954
field2:

0 commit comments

Comments
 (0)