1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Index; |
13
|
|
|
|
14
|
|
|
use FOS\ElasticaBundle\Configuration\IndexConfigInterface; |
15
|
|
|
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig; |
16
|
|
|
use FOS\ElasticaBundle\Configuration\TypeConfig; |
17
|
|
|
|
18
|
|
|
class MappingBuilder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Builds mappings for an entire index. |
22
|
|
|
* |
23
|
|
|
* @param IndexConfigInterface $indexConfig |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
10 |
|
public function buildIndexMapping(IndexConfigInterface $indexConfig) |
28
|
|
|
{ |
29
|
10 |
|
$typeMappings = []; |
30
|
10 |
|
foreach ($indexConfig->getTypes() as $typeConfig) { |
31
|
10 |
|
$typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig); |
32
|
|
|
} |
33
|
|
|
|
34
|
10 |
|
$mapping = []; |
35
|
10 |
|
if (!empty($typeMappings)) { |
36
|
10 |
|
$mapping['mappings'] = $typeMappings; |
37
|
|
|
} |
38
|
|
|
// 'warmers' => $indexConfig->getWarmers(), |
39
|
|
|
|
40
|
10 |
|
$settings = $indexConfig->getSettings(); |
41
|
10 |
|
if (!empty($settings)) { |
42
|
6 |
|
$mapping['settings'] = $settings; |
43
|
|
|
} |
44
|
|
|
|
45
|
10 |
|
return $mapping; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Builds mappings for an entire index template. |
50
|
|
|
* |
51
|
|
|
* @param IndexTemplateConfig $indexTemplateConfig |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
5 |
|
public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig) |
56
|
|
|
{ |
57
|
5 |
|
$mapping = $this->buildIndexMapping($indexTemplateConfig); |
58
|
5 |
|
$mapping['template'] = $indexTemplateConfig->getTemplate(); |
59
|
5 |
|
return $mapping; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Builds mappings for a single type. |
64
|
|
|
* |
65
|
|
|
* @param TypeConfig $typeConfig |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
11 |
|
public function buildTypeMapping(TypeConfig $typeConfig) |
70
|
|
|
{ |
71
|
11 |
|
$mapping = $typeConfig->getMapping(); |
72
|
|
|
|
73
|
11 |
|
if (null !== $typeConfig->getDynamicDateFormats()) { |
74
|
9 |
|
$mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); |
75
|
|
|
} |
76
|
|
|
|
77
|
11 |
|
if (null !== $typeConfig->getDateDetection()) { |
78
|
2 |
|
$mapping['date_detection'] = $typeConfig->getDateDetection(); |
79
|
|
|
} |
80
|
|
|
|
81
|
11 |
|
if (null !== $typeConfig->getNumericDetection()) { |
82
|
2 |
|
$mapping['numeric_detection'] = $typeConfig->getNumericDetection(); |
83
|
|
|
} |
84
|
|
|
|
85
|
11 |
|
if ($typeConfig->getAnalyzer()) { |
86
|
|
|
$mapping['analyzer'] = $typeConfig->getAnalyzer(); |
87
|
|
|
} |
88
|
|
|
|
89
|
11 |
|
if (null !== $typeConfig->getDynamic()) { |
90
|
2 |
|
$mapping['dynamic'] = $typeConfig->getDynamic(); |
91
|
|
|
} |
92
|
|
|
|
93
|
11 |
|
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
94
|
9 |
|
unset($mapping['dynamic_templates']); |
95
|
|
|
} |
96
|
|
|
|
97
|
11 |
|
$this->fixProperties($mapping['properties']); |
98
|
11 |
|
if (!$mapping['properties']) { |
99
|
3 |
|
unset($mapping['properties']); |
100
|
|
|
} |
101
|
|
|
|
102
|
11 |
|
if ($typeConfig->getModel()) { |
103
|
3 |
|
$mapping['_meta']['model'] = $typeConfig->getModel(); |
104
|
|
|
} |
105
|
|
|
|
106
|
11 |
|
unset($mapping['_parent']['identifier'], $mapping['_parent']['property']); |
107
|
|
|
|
108
|
11 |
|
if (empty($mapping)) { |
109
|
|
|
// Empty mapping, we want it encoded as a {} instead of a [] |
110
|
|
|
$mapping = new \ArrayObject(); |
111
|
|
|
} |
112
|
|
|
|
113
|
11 |
|
return $mapping; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
118
|
|
|
* required options. |
119
|
|
|
* |
120
|
|
|
* @param $properties |
121
|
|
|
*/ |
122
|
11 |
|
private function fixProperties(&$properties) |
123
|
|
|
{ |
124
|
11 |
|
foreach ($properties as $name => &$property) { |
125
|
11 |
|
unset($property['property_path']); |
126
|
|
|
|
127
|
11 |
|
if (!isset($property['type'])) { |
128
|
5 |
|
$property['type'] = 'text'; |
129
|
|
|
} |
130
|
11 |
|
if (isset($property['fields'])) { |
131
|
2 |
|
$this->fixProperties($property['fields']); |
132
|
|
|
} |
133
|
11 |
|
if (isset($property['properties'])) { |
134
|
11 |
|
$this->fixProperties($property['properties']); |
135
|
|
|
} |
136
|
|
|
} |
137
|
11 |
|
} |
138
|
|
|
} |
139
|
|
|
|