1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codappix\SearchCore\Connection; |
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\Connection\Elasticsearch\SearchResult; |
25
|
|
|
use Codappix\SearchCore\Domain\Search\QueryFactory; |
26
|
|
|
use Elastica\Query; |
27
|
|
|
use Elastica\Type; |
28
|
|
|
use TYPO3\CMS\Core\SingletonInterface as Singleton; |
29
|
|
|
use TYPO3\CMS\Extbase\Object\ObjectManagerInterface; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Outer wrapper to elasticsearch. |
33
|
|
|
*/ |
34
|
|
|
class Elasticsearch implements Singleton, ConnectionInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var Elasticsearch\Connection |
38
|
|
|
*/ |
39
|
|
|
protected $connection; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Elasticsearch\IndexFactory |
43
|
|
|
*/ |
44
|
|
|
protected $indexFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var Elasticsearch\TypeFactory |
48
|
|
|
*/ |
49
|
|
|
protected $typeFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Elasticsearch\MappingFactory |
53
|
|
|
*/ |
54
|
|
|
protected $mappingFactory; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var Elasticsearch\DocumentFactory |
58
|
|
|
*/ |
59
|
|
|
protected $documentFactory; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var QueryFactory |
63
|
|
|
*/ |
64
|
|
|
protected $queryFactory; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var \TYPO3\CMS\Core\Log\Logger |
68
|
|
|
*/ |
69
|
|
|
protected $logger; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var ObjectManagerInterface |
73
|
|
|
*/ |
74
|
|
|
protected $objectManager; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Inject log manager to get concrete logger from it. |
78
|
|
|
* |
79
|
|
|
* @param \TYPO3\CMS\Core\Log\LogManager $logManager |
80
|
|
|
*/ |
81
|
52 |
|
public function injectLogger(\TYPO3\CMS\Core\Log\LogManager $logManager) |
82
|
|
|
{ |
83
|
52 |
|
$this->logger = $logManager->getLogger(__CLASS__); |
84
|
52 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param ObjectManagerInterface $objectManager |
88
|
|
|
*/ |
89
|
52 |
|
public function injectObjectManager(ObjectManagerInterface $objectManager) |
90
|
|
|
{ |
91
|
52 |
|
$this->objectManager = $objectManager; |
92
|
52 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Elasticsearch\Connection $connection |
96
|
|
|
* @param Elasticsearch\IndexFactory $indexFactory |
97
|
|
|
* @param Elasticsearch\TypeFactory $typeFactory |
98
|
|
|
* @param Elasticsearch\MappingFactory $mappingFactory |
99
|
|
|
* @param Elasticsearch\DocumentFactory $documentFactory |
100
|
|
|
* @param QueryFactory $queryFactory |
101
|
|
|
*/ |
102
|
52 |
|
public function __construct( |
103
|
|
|
Elasticsearch\Connection $connection, |
104
|
|
|
Elasticsearch\IndexFactory $indexFactory, |
105
|
|
|
Elasticsearch\TypeFactory $typeFactory, |
106
|
|
|
Elasticsearch\MappingFactory $mappingFactory, |
107
|
|
|
Elasticsearch\DocumentFactory $documentFactory, |
108
|
|
|
QueryFactory $queryFactory |
109
|
|
|
) { |
110
|
52 |
|
$this->connection = $connection; |
111
|
52 |
|
$this->indexFactory = $indexFactory; |
112
|
52 |
|
$this->typeFactory = $typeFactory; |
113
|
52 |
|
$this->mappingFactory = $mappingFactory; |
114
|
52 |
|
$this->documentFactory = $documentFactory; |
115
|
52 |
|
$this->queryFactory = $queryFactory; |
116
|
52 |
|
} |
117
|
|
|
|
118
|
2 |
|
public function addDocument(string $documentType, array $document) |
119
|
|
|
{ |
120
|
2 |
|
$this->withType( |
121
|
2 |
|
$documentType, |
122
|
2 |
|
function (Type $type, string $documentType) use ($document) { |
123
|
2 |
|
$type->addDocument($this->documentFactory->getDocument($documentType, $document)); |
124
|
2 |
|
} |
125
|
|
|
); |
126
|
2 |
|
} |
127
|
|
|
|
128
|
2 |
|
public function deleteDocument(string $documentType, string $identifier) |
129
|
|
|
{ |
130
|
|
|
try { |
131
|
2 |
|
$this->withType( |
132
|
2 |
|
$documentType, |
133
|
2 |
|
function (Type $type, string $documentType) use ($identifier) { |
|
|
|
|
134
|
2 |
|
$type->deleteById($identifier); |
135
|
2 |
|
} |
136
|
|
|
); |
137
|
|
|
} catch (\Elastica\Exception\NotFoundException $exception) { |
138
|
|
|
$this->logger->debug( |
139
|
|
|
'Tried to delete document in index, which does not exist.', |
140
|
|
|
[$documentType, $identifier] |
141
|
|
|
); |
142
|
|
|
} |
143
|
2 |
|
} |
144
|
|
|
|
145
|
2 |
|
public function deleteAllDocuments(string $documentType) |
146
|
|
|
{ |
147
|
2 |
|
$this->deleteDocumentsByQuery($documentType, Query::create([ |
148
|
|
|
'query' => [ |
149
|
|
|
'term' => [ |
150
|
2 |
|
'search_document_type' => $documentType, |
151
|
|
|
], |
152
|
|
|
], |
153
|
|
|
])); |
154
|
2 |
|
} |
155
|
|
|
|
156
|
2 |
|
public function deleteIndex(string $documentType) |
157
|
|
|
{ |
158
|
|
|
try { |
159
|
2 |
|
$this->indexFactory->getIndex($this->connection, $documentType)->delete(); |
160
|
|
|
} catch (\InvalidArgumentException $e) { |
161
|
|
|
$this->logger->notice( |
162
|
|
|
'Index did not exist, therefore was not deleted.', |
163
|
|
|
[$documentType, $e] |
164
|
|
|
); |
165
|
|
|
} |
166
|
2 |
|
} |
167
|
|
|
|
168
|
|
|
public function updateDocument(string $documentType, array $document) |
169
|
|
|
{ |
170
|
|
|
$this->withType( |
171
|
|
|
$documentType, |
172
|
|
|
function (Type $type, string $documentType) use ($document) { |
173
|
|
|
$type->updateDocument($this->documentFactory->getDocument($documentType, $document)); |
174
|
|
|
} |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
16 |
|
public function addDocuments(string $documentType, array $documents) |
179
|
|
|
{ |
180
|
16 |
|
$this->withType( |
181
|
16 |
|
$documentType, |
182
|
16 |
|
function (Type $type, string $documentType) use ($documents) { |
183
|
16 |
|
$type->addDocuments($this->documentFactory->getDocuments($documentType, $documents)); |
184
|
16 |
|
} |
185
|
|
|
); |
186
|
16 |
|
} |
187
|
|
|
|
188
|
4 |
|
public function search(SearchRequestInterface $searchRequest): SearchResultInterface |
189
|
|
|
{ |
190
|
4 |
|
$this->logger->debug('Search for', [$searchRequest->getSearchTerm()]); |
191
|
|
|
|
192
|
4 |
|
$search = new \Elastica\Search($this->connection->getClient()); |
193
|
4 |
|
$search->addIndex($this->indexFactory->getIndexName()); |
194
|
4 |
|
$search->setQuery($this->queryFactory->create($searchRequest)); |
195
|
|
|
|
196
|
4 |
|
return $this->objectManager->get(SearchResult::class, $searchRequest, $search->search()); |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Execute given callback with Elastica Type based on provided documentType |
201
|
|
|
*/ |
202
|
18 |
|
private function withType(string $documentType, callable $callback) |
203
|
|
|
{ |
204
|
18 |
|
$type = $this->typeFactory->getType($documentType); |
205
|
|
|
// TODO: Check whether it's to heavy to send it so often e.g. for every single document. |
206
|
|
|
// Perhaps add command controller to submit mapping?! |
207
|
|
|
// Also it's not possible to change mapping without deleting index first. |
208
|
|
|
// Mattes told about a solution. |
209
|
|
|
// So command looks like the best way so far, except we manage mattes solution. |
210
|
|
|
// Still then this should be done once. So perhaps singleton which tracks state and does only once? |
211
|
18 |
|
$this->mappingFactory->getMapping($documentType)->send(); |
212
|
18 |
|
$callback($type, $documentType); |
213
|
18 |
|
$type->getIndex()->refresh(); |
214
|
18 |
|
} |
215
|
|
|
|
216
|
2 |
|
private function deleteDocumentsByQuery(string $documentType, Query $query) |
217
|
|
|
{ |
218
|
|
|
try { |
219
|
2 |
|
$index = $this->indexFactory->getIndex($this->connection, $documentType); |
220
|
2 |
|
$response = $index->deleteByQuery($query); |
221
|
|
|
|
222
|
2 |
|
if ($response->getData()['deleted'] > 0) { |
223
|
|
|
// Refresh index when delete query is invoked |
224
|
2 |
|
$index->refresh(); |
225
|
|
|
} |
226
|
|
|
} catch (\InvalidArgumentException $e) { |
227
|
|
|
$this->logger->notice( |
228
|
|
|
'Index did not exist, therefore items can not be deleted by query.', |
229
|
|
|
[$documentType, $query->getQuery()] |
230
|
|
|
); |
231
|
|
|
} |
232
|
2 |
|
} |
233
|
|
|
} |
234
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.