1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* FullTextSearch_ElasticSearch - Use Elasticsearch to index the content of your nextcloud |
4
|
|
|
* |
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
6
|
|
|
* later. See the COPYING file. |
7
|
|
|
* |
8
|
|
|
* @author Maxence Lange <[email protected]> |
9
|
|
|
* @copyright 2018 |
10
|
|
|
* @license GNU AGPL version 3 or any later version |
11
|
|
|
* |
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
15
|
|
|
* License, or (at your option) any later version. |
16
|
|
|
* |
17
|
|
|
* This program is distributed in the hope that it will be useful, |
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20
|
|
|
* GNU Affero General Public License for more details. |
21
|
|
|
* |
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace OCA\FullTextSearch_ElasticSearch\Service; |
28
|
|
|
|
29
|
|
|
use Elasticsearch\Client; |
30
|
|
|
use Elasticsearch\Common\Exceptions\BadRequest400Exception; |
31
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
32
|
|
|
use OCA\FullTextSearch\IFullTextSearchPlatform; |
33
|
|
|
use OCA\FullTextSearch\IFullTextSearchProvider; |
34
|
|
|
use OCA\FullTextSearch\Model\Index; |
35
|
|
|
use OCA\FullTextSearch\Model\IndexDocument; |
36
|
|
|
use OCA\FullTextSearch_ElasticSearch\Exceptions\AccessIsEmptyException; |
37
|
|
|
use OCA\FullTextSearch_ElasticSearch\Exceptions\ConfigurationException; |
38
|
|
|
|
39
|
|
|
class IndexService { |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** @var IndexMappingService */ |
43
|
|
|
private $indexMappingService; |
44
|
|
|
|
45
|
|
|
/** @var MiscService */ |
46
|
|
|
private $miscService; |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* IndexService constructor. |
51
|
|
|
* |
52
|
|
|
* @param IndexMappingService $indexMappingService |
53
|
|
|
* @param MiscService $miscService |
54
|
|
|
*/ |
55
|
|
|
public function __construct( |
56
|
|
|
IndexMappingService $indexMappingService, MiscService $miscService |
57
|
|
|
) { |
58
|
|
|
$this->indexMappingService = $indexMappingService; |
59
|
|
|
$this->miscService = $miscService; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param Client $client |
65
|
|
|
* |
66
|
|
|
* @throws ConfigurationException |
67
|
|
|
*/ |
68
|
|
|
public function initializeIndex(Client $client) { |
69
|
|
|
try { |
70
|
|
|
$result = $client->indices() |
71
|
|
|
->exists($this->indexMappingService->generateGlobalMap(false)); |
72
|
|
|
} catch (BadRequest400Exception $e) { |
73
|
|
|
throw new ConfigurationException( |
74
|
|
|
'Check your user/password and the index assigned to that cloud' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
try { |
79
|
|
|
if (!$result) { |
80
|
|
|
$client->indices() |
81
|
|
|
->create($this->indexMappingService->generateGlobalMap()); |
82
|
|
|
$client->ingest() |
83
|
|
|
->putPipeline($this->indexMappingService->generateGlobalIngest()); |
84
|
|
|
} |
85
|
|
|
} catch (BadRequest400Exception $e) { |
86
|
|
|
$this->resetIndex($client); |
87
|
|
|
$this->parseBadRequest400($e); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param Client $client |
94
|
|
|
* |
95
|
|
|
* @throws ConfigurationException |
96
|
|
|
*/ |
97
|
|
|
public function resetIndex(Client $client) { |
98
|
|
|
try { |
99
|
|
|
$client->ingest() |
100
|
|
|
->deletePipeline($this->indexMappingService->generateGlobalIngest(false)); |
101
|
|
|
} catch (Missing404Exception $e) { |
102
|
|
|
/* 404Exception will means that the mapping for that provider does not exist */ |
103
|
|
|
} catch (BadRequest400Exception $e) { |
104
|
|
|
throw new ConfigurationException( |
105
|
|
|
'Check your user/password and the index assigned to that cloud' |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
try { |
110
|
|
|
$client->indices() |
111
|
|
|
->delete($this->indexMappingService->generateGlobalMap(false)); |
112
|
|
|
} catch (Missing404Exception $e) { |
113
|
|
|
/* 404Exception will means that the mapping for that provider does not exist */ |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param Client $client |
120
|
|
|
* @param Index[] $indexes |
121
|
|
|
* |
122
|
|
|
* @throws ConfigurationException |
123
|
|
|
*/ |
124
|
|
|
public function deleteIndexes($client, $indexes) { |
125
|
|
|
foreach ($indexes as $index) { |
126
|
|
|
$this->indexMappingService->indexDocumentRemove( |
127
|
|
|
$client, $index->getProviderId(), $index->getDocumentId() |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param IFullTextSearchPlatform $platform |
135
|
|
|
* @param Client $client |
136
|
|
|
* @param IFullTextSearchProvider $provider |
137
|
|
|
* @param IndexDocument $document |
138
|
|
|
* |
139
|
|
|
* @return array |
|
|
|
|
140
|
|
|
* @throws ConfigurationException |
141
|
|
|
* @throws AccessIsEmptyException |
142
|
|
|
*/ |
143
|
|
|
public function indexDocument( |
144
|
|
|
IFullTextSearchPlatform $platform, Client $client, IFullTextSearchProvider $provider, |
145
|
|
|
IndexDocument $document |
146
|
|
|
) { |
147
|
|
|
$index = $document->getIndex(); |
148
|
|
|
if ($index->isStatus(Index::INDEX_REMOVE)) { |
149
|
|
|
$result = $this->indexMappingService->indexDocumentRemove( |
150
|
|
|
$client, $provider->getId(), $document->getId() |
151
|
|
|
); |
152
|
|
|
} else if ($index->isStatus(Index::INDEX_OK) && !$index->isStatus(Index::INDEX_CONTENT)) { |
153
|
|
|
$result = $this->indexMappingService->indexDocumentUpdate( |
154
|
|
|
$client, $provider, $document, $platform |
155
|
|
|
); |
156
|
|
|
} else { |
157
|
|
|
$result = $this->indexMappingService->indexDocumentNew( |
158
|
|
|
$client, $provider, $document, $platform |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $result; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param Index $index |
168
|
|
|
* @param array $result |
169
|
|
|
* |
170
|
|
|
* @return Index |
171
|
|
|
*/ |
172
|
|
|
public function parseIndexResult(Index $index, array $result) { |
173
|
|
|
|
174
|
|
|
$index->setLastIndex(); |
175
|
|
|
|
176
|
|
|
if (array_key_exists('exception', $result)) { |
177
|
|
|
$index->setStatus(Index::INDEX_FAILED); |
178
|
|
|
$index->incrementError(); |
179
|
|
|
$index->setMessage(json_encode($result)); |
180
|
|
|
|
181
|
|
|
return $index; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
// TODO: parse result |
185
|
|
|
$index->setStatus(Index::INDEX_DONE); |
186
|
|
|
|
187
|
|
|
return $index; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param BadRequest400Exception $e |
193
|
|
|
* |
194
|
|
|
* @throws ConfigurationException |
195
|
|
|
*/ |
196
|
|
|
private function parseBadRequest400(BadRequest400Exception $e) { |
197
|
|
|
|
198
|
|
|
$data = json_decode($e->getMessage(), true); |
199
|
|
|
$rootCause = $data['error']['root_cause'][0]; |
200
|
|
|
|
201
|
|
|
if ($rootCause['type'] === 'parse_exception') { |
202
|
|
|
throw new ConfigurationException( |
203
|
|
|
'please add ingest-attachment plugin to elasticsearch' |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
throw new ConfigurationException($rootCause['reason']); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]
orarray<String>
.