1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the FOSElasticaBundle project. |
5
|
|
|
* |
6
|
|
|
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au> |
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\IndexConfig; |
15
|
|
|
use FOS\ElasticaBundle\Configuration\TypeConfig; |
16
|
|
|
|
17
|
|
|
class MappingBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Builds mappings for an entire index. |
21
|
|
|
* |
22
|
|
|
* @param IndexConfig $indexConfig |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
1 |
|
public function buildIndexMapping(IndexConfig $indexConfig) |
27
|
|
|
{ |
28
|
1 |
|
$typeMappings = array(); |
29
|
1 |
|
foreach ($indexConfig->getTypes() as $typeConfig) { |
30
|
1 |
|
$typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig); |
31
|
|
|
} |
32
|
|
|
|
33
|
1 |
|
$mapping = array(); |
34
|
1 |
|
if (!empty($typeMappings)) { |
35
|
1 |
|
$mapping['mappings'] = $typeMappings; |
36
|
|
|
} |
37
|
|
|
// 'warmers' => $indexConfig->getWarmers(), |
38
|
|
|
|
39
|
1 |
|
$settings = $indexConfig->getSettings(); |
40
|
1 |
|
if (!empty($settings)) { |
41
|
|
|
$mapping['settings'] = $settings; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
return $mapping; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Builds mappings for a single type. |
49
|
|
|
* |
50
|
|
|
* @param TypeConfig $typeConfig |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
2 |
|
public function buildTypeMapping(TypeConfig $typeConfig) |
55
|
|
|
{ |
56
|
2 |
|
$mapping = $typeConfig->getMapping(); |
57
|
|
|
|
58
|
2 |
|
if (null !== $typeConfig->getDynamicDateFormats()) { |
59
|
1 |
|
$mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
if (null !== $typeConfig->getDateDetection()) { |
63
|
|
|
$mapping['date_detection'] = $typeConfig->getDateDetection(); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
if (null !== $typeConfig->getNumericDetection()) { |
67
|
|
|
$mapping['numeric_detection'] = $typeConfig->getNumericDetection(); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
if ($typeConfig->getAnalyzer()) { |
|
|
|
|
71
|
|
|
$mapping['analyzer'] = $typeConfig->getAnalyzer(); |
72
|
|
|
} |
73
|
|
|
|
74
|
2 |
|
if ($typeConfig->getDynamic() !== null) { |
75
|
|
|
$mapping['dynamic'] = $typeConfig->getDynamic(); |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
79
|
1 |
|
unset($mapping['dynamic_templates']); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
$this->fixProperties($mapping['properties']); |
83
|
2 |
|
if (!$mapping['properties']) { |
84
|
1 |
|
unset($mapping['properties']); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
if ($typeConfig->getModel()) { |
|
|
|
|
88
|
1 |
|
$mapping['_meta']['model'] = $typeConfig->getModel(); |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
unset($mapping['_parent']['identifier'], $mapping['_parent']['property']); |
92
|
|
|
|
93
|
2 |
|
if (empty($mapping)) { |
94
|
|
|
// Empty mapping, we want it encoded as a {} instead of a [] |
95
|
|
|
$mapping = new \ArrayObject(); |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
return $mapping; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
103
|
|
|
* required options. |
104
|
|
|
* |
105
|
|
|
* @param $properties |
106
|
|
|
*/ |
107
|
2 |
|
private function fixProperties(&$properties) |
108
|
|
|
{ |
109
|
2 |
|
foreach ($properties as $name => &$property) { |
110
|
2 |
|
unset($property['property_path']); |
111
|
|
|
|
112
|
2 |
|
if (!isset($property['type'])) { |
113
|
1 |
|
$property['type'] = 'text'; |
114
|
|
|
} |
115
|
2 |
|
if (isset($property['fields'])) { |
116
|
|
|
$this->fixProperties($property['fields']); |
117
|
|
|
} |
118
|
2 |
|
if (isset($property['properties'])) { |
119
|
2 |
|
$this->fixProperties($property['properties']); |
120
|
|
|
} |
121
|
|
|
} |
122
|
2 |
|
} |
123
|
|
|
} |
124
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: