forked from ongr-io/ElasticsearchBundle
-
Notifications
You must be signed in to change notification settings - Fork 10
/
ManagerFactory.php
191 lines (164 loc) · 5.74 KB
/
ManagerFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\Service;
use Elasticsearch\Client;
use Elasticsearch\ClientBuilder;
use ONGR\ElasticsearchBundle\Event\Events;
use ONGR\ElasticsearchBundle\Event\PostCreateManagerEvent;
use ONGR\ElasticsearchBundle\Event\PreCreateManagerEvent;
use ONGR\ElasticsearchBundle\Mapping\MetadataCollector;
use ONGR\ElasticsearchBundle\Result\Converter;
use PackageVersions\Versions;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as ContractsEventDispatcherInterface;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Stopwatch\Stopwatch;
/**
* Elasticsearch Manager factory class.
*/
class ManagerFactory
{
/**
* @var MetadataCollector
*/
private $metadataCollector;
/**
* @var Converter
*/
private $converter;
/**
* @var LoggerInterface
*/
private $logger;
/**
* @var LoggerInterface
*/
private $tracer;
/**
* @var EventDispatcherInterface
*/
private $eventDispatcher;
/**
* @var Stopwatch
*/
private $stopwatch;
/**
* @param MetadataCollector $metadataCollector Metadata collector service.
* @param Converter $converter Converter service to transform arrays to objects and visa versa.
* @param LoggerInterface $tracer
* @param LoggerInterface $logger
*/
public function __construct($metadataCollector, $converter, $tracer = null, $logger = null)
{
$this->metadataCollector = $metadataCollector;
$this->converter = $converter;
$this->tracer = $tracer;
$this->logger = $logger;
}
/**
* @param EventDispatcherInterface $eventDispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
{
$this->eventDispatcher = $eventDispatcher;
}
/**
* @param Stopwatch $stopwatch
*/
public function setStopwatch(Stopwatch $stopwatch)
{
$this->stopwatch = $stopwatch;
}
/**
* Factory function to create a manager instance.
*
* @param string $managerName Manager name.
* @param array $connection Connection configuration.
* @param array $analysis Analyzers, filters and tokenizers config.
* @param array $managerConfig Manager configuration.
*
* @return Manager
*/
public function createManager($managerName, $connection, $analysis, $managerConfig)
{
$mappings = $this->metadataCollector->getClientMapping($managerConfig['mappings']);
$client = ClientBuilder::create();
$client->setHosts($connection['hosts']);
if ($this->tracer) {
$client->setTracer($this->tracer);
}
if ($this->logger && $managerConfig['logger']['enabled']) {
$client->setLogger($this->logger);
}
$indexSettings = [
'index' => $connection['index_name'],
'body' => array_filter(
[
'settings' => array_merge(
$connection['settings'],
[
'analysis' =>
$this->metadataCollector->getClientAnalysis($managerConfig['mappings'], $analysis),
]
),
'mappings' => $mappings,
]
),
];
// set elasticsearch specific settings
$elasticSearchVersion = defined(Client::class . '::VERSION') ? Client::VERSION : '5.0';
if (version_compare($elasticSearchVersion, '7.0.0', '>=')) {
$indexSettings['include_type_name'] = true;
if (version_compare($elasticSearchVersion, '7.11.0', '>=')) {
$client->setConnectionParams([
'client' => [
'headers' => [
'Accept' => ['application/vnd.elasticsearch+json;compatible-with=7'],
'Content-Type' => ['application/vnd.elasticsearch+json;compatible-with=7'],
]
]
]);
}
}
$this->eventDispatcher &&
$this->dispatch(
Events::PRE_MANAGER_CREATE,
$preCreateEvent = new PreCreateManagerEvent($client, $indexSettings)
);
$manager = new Manager(
$managerName,
$managerConfig,
$client->build(),
$preCreateEvent->getIndexSettings(),
$this->metadataCollector,
$this->converter
);
if (isset($this->stopwatch)) {
$manager->setStopwatch($this->stopwatch);
}
$manager->setCommitMode($managerConfig['commit_mode']);
$manager->setEventDispatcher($this->eventDispatcher);
$manager->setCommitMode($managerConfig['commit_mode']);
$manager->setBulkCommitSize($managerConfig['bulk_size']);
$this->eventDispatcher &&
$this->dispatch(Events::POST_MANAGER_CREATE, new PostCreateManagerEvent($manager));
return $manager;
}
private function dispatch($eventName, $event)
{
if ($this->eventDispatcher instanceof ContractsEventDispatcherInterface
|| class_exists(LegacyEventDispatcherProxy::class)) {
return $this->eventDispatcher->dispatch($event, $eventName);
} else {
return $this->eventDispatcher->dispatch($eventName, $event);
}
}
}