Completed
Push — master ( 408a59...f99325 )
by Maxence
01:45
created

IndexService::deleteIndexes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
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\ConfigurationException;
37
38
class IndexService {
39
40
41
	/** @var IndexMappingService */
42
	private $indexMappingService;
43
44
	/** @var MiscService */
45
	private $miscService;
46
47
48
	/**
49
	 * IndexService constructor.
50
	 *
51
	 * @param IndexMappingService $indexMappingService
52
	 * @param MiscService $miscService
53
	 */
54
	public function __construct(
55
		IndexMappingService $indexMappingService, MiscService $miscService
56
	) {
57
		$this->indexMappingService = $indexMappingService;
58
		$this->miscService = $miscService;
59
	}
60
61
62
	/**
63
	 * @param Client $client
64
	 *
65
	 * @throws ConfigurationException
66
	 */
67
	public function initializeIndex(Client $client) {
68
		try {
69
			$result = $client->indices()
70
							 ->exists($this->indexMappingService->generateGlobalMap(false));
71
		} catch (BadRequest400Exception $e) {
72
			throw new ConfigurationException(
73
				'Check your user/password and the index assigned to that cloud'
74
			);
75
		}
76
77
		try {
78
			if (!$result) {
79
				$client->indices()
80
					   ->create($this->indexMappingService->generateGlobalMap());
81
				$client->ingest()
82
					   ->putPipeline($this->indexMappingService->generateGlobalIngest());
83
			}
84
		} catch (BadRequest400Exception $e) {
85
			throw new ConfigurationException(
86
				'please add ingest-attachment plugin to elasticsearch'
87
			);
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
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...
140
	 * @throws ConfigurationException
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