1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codappix\SearchCore\Connection\Elasticsearch; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* Copyright (C) 2016 Daniel Siepmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This program is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU General Public License |
10
|
|
|
* as published by the Free Software Foundation; either version 2 |
11
|
|
|
* of the License, or (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* This program is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with this program; if not, write to the Free Software |
20
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
21
|
|
|
* 02110-1301, USA. |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
use Codappix\SearchCore\Configuration\ConfigurationContainerInterface; |
25
|
|
|
use Codappix\SearchCore\Configuration\InvalidArgumentException; |
26
|
|
|
use TYPO3\CMS\Core\SingletonInterface as Singleton; |
27
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Factory to get indexes. |
31
|
|
|
* |
32
|
|
|
* The factory will take care of configuration and creation of index if necessary. |
33
|
|
|
*/ |
34
|
|
|
class IndexFactory implements Singleton |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var ConfigurationContainerInterface |
38
|
|
|
*/ |
39
|
|
|
protected $configuration; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger |
43
|
|
|
*/ |
44
|
|
|
protected $logger; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Inject log manager to get concrete logger from it. |
48
|
|
|
* |
49
|
|
|
* @param \TYPO3\CMS\Core\Log\LogManager $logManager |
50
|
|
|
*/ |
51
|
52 |
|
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager) |
52
|
|
|
{ |
53
|
52 |
|
$this->logger = $logManager->getLogger(__CLASS__); |
54
|
52 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ConfigurationContainerInterface $configuration |
58
|
|
|
*/ |
59
|
52 |
|
public function __construct(ConfigurationContainerInterface $configuration) |
60
|
|
|
{ |
61
|
52 |
|
$this->configuration = $configuration; |
62
|
52 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the index name from the typoscript settings. |
66
|
|
|
*/ |
67
|
20 |
|
public function getIndexName(): string |
68
|
|
|
{ |
69
|
20 |
|
return $this->configuration->get('connections.elasticsearch.index'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @throws \InvalidArgumentException If index does not exist. |
74
|
|
|
*/ |
75
|
4 |
|
public function getIndex(Connection $connection, string $documentType): \Elastica\Index |
|
|
|
|
76
|
|
|
{ |
77
|
4 |
|
$index = $connection->getClient()->getIndex($this->getIndexName()); |
78
|
|
|
|
79
|
4 |
|
if ($index->exists() === false) { |
80
|
|
|
throw new \InvalidArgumentException('The requested index does not exist.', 1546173102); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
return $index; |
84
|
|
|
} |
85
|
|
|
|
86
|
18 |
|
public function createIndex(Connection $connection, string $documentType): \Elastica\Index |
87
|
|
|
{ |
88
|
18 |
|
$index = $connection->getClient()->getIndex($this->getIndexName()); |
89
|
|
|
|
90
|
18 |
|
if ($index->exists() === true) { |
91
|
18 |
|
return $index; |
92
|
|
|
} |
93
|
|
|
|
94
|
16 |
|
$config = $this->getConfigurationFor($documentType); |
95
|
16 |
|
$this->logger->debug(sprintf('Create index %s.', $documentType), [$documentType, $config]); |
96
|
16 |
|
$index->create($config); |
97
|
16 |
|
$this->logger->debug(sprintf('Created index %s.', $documentType), [$documentType]); |
98
|
|
|
|
99
|
16 |
|
return $index; |
100
|
|
|
} |
101
|
|
|
|
102
|
16 |
|
protected function getConfigurationFor(string $documentType): array |
103
|
|
|
{ |
104
|
|
|
try { |
105
|
16 |
|
$configuration = $this->configuration->get('indexing.' . $documentType . '.index'); |
106
|
|
|
|
107
|
|
|
foreach (['analyzer', 'filter'] as $optionsToExpand) { |
108
|
|
|
if (isset($configuration['analysis'][$optionsToExpand])) { |
109
|
|
|
foreach ($configuration['analysis'][$optionsToExpand] as $key => $options) { |
110
|
|
|
$configuration['analysis'][$optionsToExpand][$key] = $this->prepareAnalyzerConfiguration( |
111
|
|
|
$options |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $configuration; |
118
|
16 |
|
} catch (InvalidArgumentException $e) { |
119
|
16 |
|
return []; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function prepareAnalyzerConfiguration(array $analyzer): array |
124
|
|
|
{ |
125
|
|
|
$fieldsToExplode = ['char_filter', 'filter', 'word_list']; |
126
|
|
|
|
127
|
|
|
foreach ($fieldsToExplode as $fieldToExplode) { |
128
|
|
|
if (isset($analyzer[$fieldToExplode])) { |
129
|
|
|
$analyzer[$fieldToExplode] = GeneralUtility::trimExplode(',', $analyzer[$fieldToExplode], true); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $analyzer; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.