Completed
Push — master ( d0fbf4...294c9f )
by Maxence
02:56
created

IndexService   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 6
dl 0
loc 180
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A indexDocument() 0 21 4
A parseIndexResult() 0 17 2
A __construct() 0 6 1
A initializeIndex() 0 20 4
A resetIndex() 0 19 4
A deleteIndexes() 0 7 2
A parseBadRequest400() 0 21 4
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
	 * @throws BadRequest400Exception
68
	 */
69
	public function initializeIndex(Client $client) {
70
		try {
71
			if ($client->indices()
72
					   ->exists($this->indexMappingService->generateGlobalMap(false))) {
73
				return;
74
			}
75
		} catch (BadRequest400Exception $e) {
76
			$this->parseBadRequest400($e);
77
		}
78
79
		try {
80
			$client->indices()
81
				   ->create($this->indexMappingService->generateGlobalMap());
82
			$client->ingest()
83
				   ->putPipeline($this->indexMappingService->generateGlobalIngest());
84
		} catch (BadRequest400Exception $e) {
85
			$this->resetIndex($client);
86
			$this->parseBadRequest400($e);
87
		}
88
	}
89
90
91
	/**
92
	 * @param Client $client
93
	 *
94
	 * @throws ConfigurationException
95
	 */
96
	public function resetIndex(Client $client) {
97
		try {
98
			$client->ingest()
99
				   ->deletePipeline($this->indexMappingService->generateGlobalIngest(false));
100
		} catch (Missing404Exception $e) {
101
			/* 404Exception will means that the mapping for that provider does not exist */
102
		} catch (BadRequest400Exception $e) {
103
			throw new ConfigurationException(
104
				'Check your user/password and the index assigned to that cloud'
105
			);
106
		}
107
108
		try {
109
			$client->indices()
110
				   ->delete($this->indexMappingService->generateGlobalMap(false));
111
		} catch (Missing404Exception $e) {
112
			/* 404Exception will means that the mapping for that provider does not exist */
113
		}
114
	}
115
116
117
	/**
118
	 * @param Client $client
119
	 * @param Index[] $indexes
120
	 *
121
	 * @throws ConfigurationException
122
	 */
123
	public function deleteIndexes($client, $indexes) {
124
		foreach ($indexes as $index) {
125
			$this->indexMappingService->indexDocumentRemove(
126
				$client, $index->getProviderId(), $index->getDocumentId()
127
			);
128
		}
129
	}
130
131
132
	/**
133
	 * @param IFullTextSearchPlatform $platform
134
	 * @param Client $client
135
	 * @param IFullTextSearchProvider $provider
136
	 * @param IndexDocument $document
137
	 *
138
	 * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be callable? Also, consider making the array more specific, something like array<String>, or String[].

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[] or array<String>.

Loading history...
139
	 * @throws ConfigurationException
140
	 * @throws AccessIsEmptyException
141
	 */
142
	public function indexDocument(
143
		IFullTextSearchPlatform $platform, Client $client, IFullTextSearchProvider $provider,
144
		IndexDocument $document
145
	) {
146
		$index = $document->getIndex();
147
		if ($index->isStatus(Index::INDEX_REMOVE)) {
148
			$result = $this->indexMappingService->indexDocumentRemove(
149
				$client, $provider->getId(), $document->getId()
150
			);
151
		} else if ($index->isStatus(Index::INDEX_OK) && !$index->isStatus(Index::INDEX_CONTENT)) {
152
			$result = $this->indexMappingService->indexDocumentUpdate(
153
				$client, $provider, $document, $platform
154
			);
155
		} else {
156
			$result = $this->indexMappingService->indexDocumentNew(
157
				$client, $provider, $document, $platform
158
			);
159
		}
160
161
		return $result;
162
	}
163
164
165
	/**
166
	 * @param Index $index
167
	 * @param array $result
168
	 *
169
	 * @return Index
170
	 */
171
	public function parseIndexResult(Index $index, array $result) {
172
173
		$index->setLastIndex();
174
175
		if (array_key_exists('exception', $result)) {
176
			$index->setStatus(Index::INDEX_FAILED);
177
			$index->incrementError();
178
			$index->setMessage(json_encode($result));
179
180
			return $index;
181
		}
182
183
		// TODO: parse result
184
		$index->setStatus(Index::INDEX_DONE);
185
186
		return $index;
187
	}
188
189
190
	/**
191
	 * @param BadRequest400Exception $e
192
	 *
193
	 * @throws ConfigurationException
194
	 * @throws BadRequest400Exception
195
	 */
196
	private function parseBadRequest400(BadRequest400Exception $e) {
197
198
		if ($e->getMessage() === '') {
199
			throw new ConfigurationException(
200
				'Check your user/password and the index assigned to that cloud'
201
			);
202
		}
203
204
205
		$error = json_decode($e->getMessage(), true)['error'];
206
207
		if ($error['type'] === 'parse_exception') {
208
			if ($error['reason'] === 'No processor type exists with name [attachment]') {
209
				throw new ConfigurationException(
210
					'please add ingest-attachment plugin to elasticsearch'
211
				);
212
			}
213
		}
214
215
		throw $e;
216
	}
217
218
}
219