1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
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 ONGR\ElasticsearchBundle\Service; |
13
|
|
|
|
14
|
|
|
use Elasticsearch\ClientBuilder; |
15
|
|
|
use ONGR\ElasticsearchBundle\Event\Events; |
16
|
|
|
use ONGR\ElasticsearchBundle\Event\PostCreateManagerEvent; |
17
|
|
|
use ONGR\ElasticsearchBundle\Event\PreCreateManagerEvent; |
18
|
|
|
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector; |
19
|
|
|
use ONGR\ElasticsearchBundle\Result\Converter; |
20
|
|
|
use Psr\Log\LoggerInterface; |
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
22
|
|
|
use Symfony\Component\Stopwatch\Stopwatch; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Elasticsearch Manager factory class. |
26
|
|
|
*/ |
27
|
|
|
class ManagerFactory |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var MetadataCollector |
31
|
|
|
*/ |
32
|
|
|
private $metadataCollector; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Converter |
36
|
|
|
*/ |
37
|
|
|
private $converter; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var LoggerInterface |
46
|
|
|
*/ |
47
|
|
|
private $tracer; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var EventDispatcherInterface |
51
|
|
|
*/ |
52
|
|
|
private $eventDispatcher; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Stopwatch |
56
|
|
|
*/ |
57
|
|
|
private $stopwatch; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param MetadataCollector $metadataCollector Metadata collector service. |
61
|
|
|
* @param Converter $converter Converter service to transform arrays to objects and visa versa. |
62
|
|
|
* @param LoggerInterface $tracer |
63
|
|
|
* @param LoggerInterface $logger |
64
|
|
|
*/ |
65
|
|
|
public function __construct($metadataCollector, $converter, $tracer = null, $logger = null) |
66
|
|
|
{ |
67
|
|
|
$this->metadataCollector = $metadataCollector; |
68
|
|
|
$this->converter = $converter; |
69
|
|
|
$this->tracer = $tracer; |
70
|
|
|
$this->logger = $logger; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
75
|
|
|
*/ |
76
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
77
|
|
|
{ |
78
|
|
|
$this->eventDispatcher = $eventDispatcher; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Stopwatch $stopwatch |
83
|
|
|
*/ |
84
|
|
|
public function setStopwatch(Stopwatch $stopwatch) |
85
|
|
|
{ |
86
|
|
|
$this->stopwatch = $stopwatch; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Factory function to create a manager instance. |
91
|
|
|
* |
92
|
|
|
* @param string $managerName Manager name. |
93
|
|
|
* @param array $connection Connection configuration. |
94
|
|
|
* @param array $analysis Analyzers, filters and tokenizers config. |
95
|
|
|
* @param array $managerConfig Manager configuration. |
96
|
|
|
* |
97
|
|
|
* @return Manager |
98
|
|
|
*/ |
99
|
|
|
public function createManager($managerName, $connection, $analysis, $managerConfig) |
100
|
|
|
{ |
101
|
|
|
$managerAnalysis = []; |
102
|
|
|
$mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); |
103
|
|
|
|
104
|
|
|
foreach ($mappings as $type) { |
|
|
|
|
105
|
|
|
$this->extractAnalysisFromProperties($type['properties'], $analysis, $managerAnalysis); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$connection['settings']['analysis'] = array_filter($managerAnalysis); |
109
|
|
|
|
110
|
|
|
if (!isset($connection['settings']['number_of_replicas'])) { |
111
|
|
|
$connection['settings']['number_of_replicas'] = 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (!isset($connection['settings']['number_of_shards'])) { |
115
|
|
|
$connection['settings']['number_of_shards'] = 1; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']); |
119
|
|
|
|
120
|
|
|
$client = ClientBuilder::create(); |
121
|
|
|
$client->setHosts($connection['hosts']); |
122
|
|
|
$client->setTracer($this->tracer); |
123
|
|
|
|
124
|
|
|
if ($this->logger && $managerConfig['logger']['enabled']) { |
125
|
|
|
$client->setLogger($this->logger); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
$indexSettings = [ |
129
|
|
|
'index' => $connection['index_name'], |
130
|
|
|
'body' => array_filter( |
131
|
|
|
[ |
132
|
|
|
'settings' => $connection['settings'], |
133
|
|
|
'mappings' => $mappings, |
134
|
|
|
] |
135
|
|
|
), |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
$this->eventDispatcher && |
139
|
|
|
$this->eventDispatcher->dispatch( |
140
|
|
|
Events::PRE_MANAGER_CREATE, |
141
|
|
|
new PreCreateManagerEvent($client, $indexSettings) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
$manager = new Manager( |
145
|
|
|
$managerName, |
146
|
|
|
$managerConfig, |
147
|
|
|
$client->build(), |
148
|
|
|
$indexSettings, |
149
|
|
|
$this->metadataCollector, |
150
|
|
|
$this->converter |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
if (isset($this->stopwatch)) { |
154
|
|
|
$manager->setStopwatch($this->stopwatch); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$manager->setCommitMode($managerConfig['commit_mode']); |
158
|
|
|
$manager->setEventDispatcher($this->eventDispatcher); |
159
|
|
|
$manager->setBulkCommitSize($managerConfig['bulk_size']); |
160
|
|
|
|
161
|
|
|
$this->eventDispatcher && |
162
|
|
|
$this->eventDispatcher->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager)); |
163
|
|
|
|
164
|
|
|
return $manager; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Extracts analysis configuration from all the documents |
169
|
|
|
* |
170
|
|
|
* @param array $properties Properties of a type or an object |
171
|
|
|
* @param array $analysis The full analysis node from configuration |
172
|
|
|
* @param array $managerAnalysis The data that is being formed for the manager |
173
|
|
|
*/ |
174
|
|
|
private function extractAnalysisFromProperties($properties, $analysis, &$managerAnalysis) |
175
|
|
|
{ |
176
|
|
|
foreach ($properties as $property) { |
177
|
|
|
if (isset($property['analyzer'])) { |
178
|
|
|
$analyzer = $analysis['analyzer'][$property['analyzer']]; |
179
|
|
|
$managerAnalysis['analyzer'][$property['analyzer']] = $analyzer; |
180
|
|
|
|
181
|
|
|
$this->extractSubData('filter', $analyzer, $analysis, $managerAnalysis); |
182
|
|
|
$this->extractSubData('char_filter', $analyzer, $analysis, $managerAnalysis); |
183
|
|
|
$this->extractSubData('tokenizer', $analyzer, $analysis, $managerAnalysis); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if (isset($property['properties'])) { |
187
|
|
|
$this->extractAnalysisFromProperties($property['properties'], $analysis, $managerAnalysis); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Extracts tokenizers and filters from analysis configuration |
194
|
|
|
* |
195
|
|
|
* @param string $type Either filter or tokenizer |
196
|
|
|
* @param array $analyzer The current analyzer |
197
|
|
|
* @param array $analysis The full analysis node from configuration |
198
|
|
|
* @param array $data The data that is being formed for the manager |
199
|
|
|
*/ |
200
|
|
|
private function extractSubData($type, $analyzer, $analysis, &$data) |
201
|
|
|
{ |
202
|
|
|
if (!isset($analyzer[$type])) { |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if (is_array($analyzer[$type])) { |
207
|
|
|
foreach ($analyzer[$type] as $name) { |
208
|
|
View Code Duplication |
if (isset($analysis[$type][$name])) { |
|
|
|
|
209
|
|
|
$data[$type][$name] = $analysis[$type][$name]; |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
} else { |
213
|
|
|
$name = $analyzer[$type]; |
214
|
|
|
|
215
|
|
View Code Duplication |
if (isset($analysis[$type][$name])) { |
|
|
|
|
216
|
|
|
$data[$type][$name] = $analysis[$type][$name]; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.