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
|
|
|
|
60
|
5 |
|
return $mapping; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Builds mappings for a single type. |
65
|
|
|
* |
66
|
|
|
* @param TypeConfig $typeConfig |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
11 |
|
public function buildTypeMapping(TypeConfig $typeConfig) |
71
|
|
|
{ |
72
|
11 |
|
$mapping = $typeConfig->getMapping(); |
73
|
|
|
|
74
|
11 |
|
if (null !== $typeConfig->getDynamicDateFormats()) { |
75
|
9 |
|
$mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); |
76
|
|
|
} |
77
|
|
|
|
78
|
11 |
|
if (null !== $typeConfig->getDateDetection()) { |
79
|
2 |
|
$mapping['date_detection'] = $typeConfig->getDateDetection(); |
80
|
|
|
} |
81
|
|
|
|
82
|
11 |
|
if (null !== $typeConfig->getNumericDetection()) { |
83
|
2 |
|
$mapping['numeric_detection'] = $typeConfig->getNumericDetection(); |
84
|
|
|
} |
85
|
|
|
|
86
|
11 |
|
if ($typeConfig->getAnalyzer()) { |
87
|
|
|
$mapping['analyzer'] = $typeConfig->getAnalyzer(); |
88
|
|
|
} |
89
|
|
|
|
90
|
11 |
|
if (null !== $typeConfig->getDynamic()) { |
91
|
2 |
|
$mapping['dynamic'] = $typeConfig->getDynamic(); |
92
|
|
|
} |
93
|
|
|
|
94
|
11 |
|
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
95
|
9 |
|
unset($mapping['dynamic_templates']); |
96
|
|
|
} |
97
|
|
|
|
98
|
11 |
|
$this->fixProperties($mapping['properties']); |
99
|
11 |
|
if (!$mapping['properties']) { |
100
|
3 |
|
unset($mapping['properties']); |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
if ($typeConfig->getModel()) { |
104
|
3 |
|
$mapping['_meta']['model'] = $typeConfig->getModel(); |
105
|
|
|
} |
106
|
|
|
|
107
|
11 |
|
unset($mapping['_parent']['identifier'], $mapping['_parent']['property']); |
108
|
|
|
|
109
|
11 |
|
if (empty($mapping)) { |
110
|
|
|
// Empty mapping, we want it encoded as a {} instead of a [] |
111
|
|
|
$mapping = new \ArrayObject(); |
112
|
|
|
} |
113
|
|
|
|
114
|
11 |
|
return $mapping; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
119
|
|
|
* required options. |
120
|
|
|
* |
121
|
|
|
* @param $properties |
122
|
|
|
*/ |
123
|
11 |
|
private function fixProperties(&$properties) |
124
|
|
|
{ |
125
|
11 |
|
foreach ($properties as $name => &$property) { |
126
|
11 |
|
unset($property['property_path']); |
127
|
|
|
|
128
|
11 |
|
if (!isset($property['type'])) { |
129
|
5 |
|
$property['type'] = 'text'; |
130
|
|
|
} |
131
|
11 |
|
if (isset($property['fields'])) { |
132
|
2 |
|
$this->fixProperties($property['fields']); |
133
|
|
|
} |
134
|
11 |
|
if (isset($property['properties'])) { |
135
|
11 |
|
$this->fixProperties($property['properties']); |
136
|
|
|
} |
137
|
|
|
} |
138
|
11 |
|
} |
139
|
|
|
} |
140
|
|
|
|