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
|
|
|
* Skip adding default information to certain fields. |
21
|
|
|
* |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
private $skipTypes = array('completion'); |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Builds mappings for an entire index. |
28
|
|
|
* |
29
|
|
|
* @param IndexConfig $indexConfig |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
*/ |
33
|
2 |
|
public function buildIndexMapping(IndexConfig $indexConfig) |
34
|
|
|
{ |
35
|
2 |
|
$typeMappings = array(); |
36
|
2 |
|
foreach ($indexConfig->getTypes() as $typeConfig) { |
37
|
2 |
|
$typeMappings[$typeConfig->getName()] = $this->buildTypeMapping($typeConfig); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
2 |
|
$mapping = array(); |
41
|
2 |
|
if (!empty($typeMappings)) { |
42
|
2 |
|
$mapping['mappings'] = $typeMappings; |
43
|
2 |
|
} |
44
|
|
|
// 'warmers' => $indexConfig->getWarmers(), |
45
|
|
|
|
46
|
2 |
|
$settings = $indexConfig->getSettings(); |
47
|
2 |
|
if (!empty($settings)) { |
48
|
1 |
|
$mapping['settings'] = $settings; |
49
|
1 |
|
} |
50
|
|
|
|
51
|
2 |
|
return $mapping; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Builds mappings for a single type. |
56
|
|
|
* |
57
|
|
|
* @param TypeConfig $typeConfig |
58
|
|
|
* |
59
|
|
|
* @return array |
60
|
|
|
*/ |
61
|
3 |
|
public function buildTypeMapping(TypeConfig $typeConfig) |
62
|
|
|
{ |
63
|
3 |
|
$mapping = $typeConfig->getMapping(); |
64
|
|
|
|
65
|
3 |
|
if (null !== $typeConfig->getDynamicDateFormats()) { |
66
|
3 |
|
$mapping['dynamic_date_formats'] = $typeConfig->getDynamicDateFormats(); |
67
|
3 |
|
} |
68
|
|
|
|
69
|
3 |
|
if (null !== $typeConfig->getDateDetection()) { |
70
|
2 |
|
$mapping['date_detection'] = $typeConfig->getDateDetection(); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
3 |
|
if (null !== $typeConfig->getNumericDetection()) { |
74
|
2 |
|
$mapping['numeric_detection'] = $typeConfig->getNumericDetection(); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
3 |
|
if ($typeConfig->getIndexAnalyzer()) { |
|
|
|
|
78
|
1 |
|
$mapping['index_analyzer'] = $typeConfig->getIndexAnalyzer(); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
3 |
|
if ($typeConfig->getSearchAnalyzer()) { |
|
|
|
|
82
|
2 |
|
$mapping['search_analyzer'] = $typeConfig->getSearchAnalyzer(); |
83
|
2 |
|
} |
84
|
|
|
|
85
|
3 |
|
if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) { |
86
|
2 |
|
unset($mapping['dynamic_templates']); |
87
|
2 |
|
} |
88
|
|
|
|
89
|
3 |
|
$this->fixProperties($mapping['properties']); |
90
|
3 |
|
if (!$mapping['properties']) { |
91
|
2 |
|
unset($mapping['properties']); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
3 |
|
if ($typeConfig->getModel()) { |
|
|
|
|
95
|
1 |
|
$mapping['_meta']['model'] = $typeConfig->getModel(); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
3 |
|
if (empty($mapping)) { |
99
|
|
|
// Empty mapping, we want it encoded as a {} instead of a [] |
100
|
|
|
$mapping = new \stdClass(); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return $mapping; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Fixes any properties and applies basic defaults for any field that does not have |
108
|
|
|
* required options. |
109
|
|
|
* |
110
|
|
|
* @param $properties |
111
|
|
|
*/ |
112
|
3 |
|
private function fixProperties(&$properties) |
113
|
|
|
{ |
114
|
3 |
|
foreach ($properties as $name => &$property) { |
115
|
3 |
|
unset($property['property_path']); |
116
|
|
|
|
117
|
3 |
|
if (!isset($property['type'])) { |
118
|
3 |
|
$property['type'] = 'string'; |
119
|
3 |
|
} |
120
|
3 |
|
if ($property['type'] == 'multi_field' && isset($property['fields'])) { |
121
|
|
|
$this->fixProperties($property['fields']); |
122
|
|
|
} |
123
|
3 |
|
if (isset($property['properties'])) { |
124
|
2 |
|
$this->fixProperties($property['properties']); |
125
|
2 |
|
} |
126
|
3 |
|
if (in_array($property['type'], $this->skipTypes)) { |
127
|
2 |
|
continue; |
128
|
|
|
} |
129
|
3 |
|
} |
130
|
3 |
|
} |
131
|
|
|
} |
132
|
|
|
|
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: