1
|
|
|
<?php |
2
|
|
|
declare (strict_types = 1); |
3
|
|
|
namespace T3G\Elasticorn\Service; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use Elastica\Index; |
7
|
|
|
use Elastica\Type\Mapping; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use T3G\Elasticorn\Utility\ConfigurationParser; |
10
|
|
|
use T3G\Elasticorn\Utility\DiffUtility; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ConfigurationService |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class ConfigurationService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var \Elastica\Client |
20
|
|
|
*/ |
21
|
|
|
protected $client; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \T3G\Elasticorn\Utility\ConfigurationParser |
25
|
|
|
*/ |
26
|
|
|
private $configurationParser; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \Psr\Log\LoggerInterface |
30
|
|
|
*/ |
31
|
|
|
private $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ConfigurationService constructor. |
35
|
|
|
* |
36
|
|
|
* @param \Elastica\Client $client |
37
|
|
|
* @param \T3G\Elasticorn\Utility\ConfigurationParser $configurationParser |
38
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
Client $client, |
42
|
|
|
ConfigurationParser $configurationParser, |
43
|
|
|
LoggerInterface $logger |
44
|
|
|
) { |
45
|
|
|
$this->client = $client; |
46
|
|
|
$this->configurationParser = $configurationParser; |
47
|
|
|
$this->logger = $logger; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $indexName |
52
|
|
|
* @param Index $index |
53
|
|
|
*/ |
54
|
|
|
public function applyMapping(string $indexName, Index $index, string $language = '') |
55
|
|
|
{ |
56
|
|
|
$documentTypeConfigurations = $this->configurationParser->getDocumentTypeConfigurations($indexName, $language); |
57
|
|
|
$this->logger->debug('Loading mapping for ' . $indexName); |
58
|
|
|
foreach ($documentTypeConfigurations as $documentType => $configuration) { |
59
|
|
|
$type = $index->getType($documentType); |
60
|
|
|
$mapping = new Mapping(); |
61
|
|
|
$mapping->setType($type); |
62
|
|
|
$mapping->setProperties($configuration); |
63
|
|
|
$mapping->send(); |
64
|
|
|
$this->logger->debug('Applying mapping for ' . $documentType); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Compare mapping configurations (applied in elasticsearch and configured in file) |
70
|
|
|
* |
71
|
|
|
* @param string $indexName |
72
|
|
|
* @param Index $index |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function compareMappingConfiguration(string $indexName, Index $index = null) : string |
76
|
|
|
{ |
77
|
|
|
if ($index === null) { |
78
|
|
|
$index = $this->client->getIndex($indexName); |
79
|
|
|
} |
80
|
|
|
$mapping = $index->getMapping(); |
81
|
|
|
$this->logger->debug('Get mapping configuration for ' . $indexName); |
82
|
|
|
$documentTypeConfigurations = |
83
|
|
|
$this->configurationParser->convertDocumentTypeConfigurationToMappingFromElastica( |
84
|
|
|
$this->configurationParser->getDocumentTypeConfigurations($indexName) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return $this->compareConfigurations($mapping, $documentTypeConfigurations); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Creates configuration directories and files from settings and mappings of an existing index |
92
|
|
|
* |
93
|
|
|
* @param string $indexName |
94
|
|
|
* @param Index $index |
95
|
|
|
*/ |
96
|
|
|
public function createConfigurationFromExistingIndex(string $indexName, Index $index) |
97
|
|
|
{ |
98
|
|
|
$settings = $index->getSettings(); |
99
|
|
|
$mapping = $index->getMapping(); |
100
|
|
|
$cleanSettings = $this->configurationParser->cleanSettingsArray($settings->get()); |
|
|
|
|
101
|
|
|
$this->configurationParser->createConfigurationForIndex($indexName, $mapping, $cleanSettings); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $indexName |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function getIndexConfiguration(string $indexName) : array |
110
|
|
|
{ |
111
|
|
|
return $this->configurationParser->getIndexConfiguration($indexName); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getIndexConfigurations() : array |
118
|
|
|
{ |
119
|
|
|
return $this->configurationParser->getIndexConfigurations(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $configuration1 |
124
|
|
|
* @param $configuration2 |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
private function compareConfigurations($configuration1, $configuration2) : string |
128
|
|
|
{ |
129
|
|
|
$result = ''; |
130
|
|
|
if ($configuration1 === $configuration2) { |
131
|
|
|
$this->logger->info('No difference between configurations.'); |
132
|
|
|
} else { |
133
|
|
|
$result = $this->compareDocTypeConfiguration($configuration1, $configuration2); |
134
|
|
|
} |
135
|
|
|
return $result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $configuration1 |
140
|
|
|
* @param $configuration2 |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
private function compareDocTypeConfiguration(array $configuration1, array $configuration2) : string |
144
|
|
|
{ |
145
|
|
|
$result = ''; |
146
|
|
|
$differ = new DiffUtility(); |
147
|
|
|
foreach ($configuration2 as $documentType => $configuration) { |
148
|
|
|
if (array_key_exists($documentType, $configuration1)) { |
149
|
|
|
$documentTypeMapping = $configuration1[$documentType]['properties']; |
150
|
|
|
$configuration = $configuration['properties']; |
151
|
|
|
ksort($documentTypeMapping); |
152
|
|
|
ksort($configuration); |
153
|
|
|
if ($documentTypeMapping === $configuration) { |
154
|
|
|
$this->logger->info( |
155
|
|
|
'No difference between configurations of document type "' . $documentType . '"' |
156
|
|
|
); |
157
|
|
|
} else { |
158
|
|
|
$diff = "Document Type \"$documentType\": \n" . |
159
|
|
|
$differ->diff($documentTypeMapping, $configuration); |
160
|
|
|
$this->logger->info($diff); |
161
|
|
|
$result .= $diff; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
return $result; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.