Completed
Push — master ( 2675cf...85e5b9 )
by Maxence
01:49
created

SearchService::getDocument()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 3
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 Exception;
31
use OCA\FullTextSearch\IFullTextSearchProvider;
32
use OCA\FullTextSearch\Model\DocumentAccess;
33
use OCA\FullTextSearch\Model\IndexDocument;
34
use OCA\FullTextSearch\Model\SearchRequest;
35
use OCA\FullTextSearch\Model\SearchResult;
36
use OCA\FullTextSearch_ElasticSearch\Exceptions\ConfigurationException;
37
use OCA\FullTextSearch_ElasticSearch\Exceptions\SearchQueryGenerationException;
38
39
class SearchService {
40
41
42
	/** @var SearchMappingService */
43
	private $searchMappingService;
44
45
	/** @var MiscService */
46
	private $miscService;
47
48
49
	/**
50
	 * SearchService constructor.
51
	 *
52
	 * @param SearchMappingService $searchMappingService
53
	 * @param MiscService $miscService
54
	 */
55
	public function __construct(
56
		SearchMappingService $searchMappingService, MiscService $miscService
57
	) {
58
		$this->searchMappingService = $searchMappingService;
59
		$this->miscService = $miscService;
60
	}
61
62
63
//	/**
64
//	 * @param Client $client
65
//	 * @param IFullTextSearchProvider $provider
66
//	 * @param DocumentAccess $access
67
//	 * @param SearchRequest $request
68
//	 *
69
//	 * @throws ConfigurationException
70
//	 * @throws Exception
71
//	 */
72
//	public function searchDocuments(
73
//		Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
74
//		SearchRequest $request
75
//	) {
76
//		try {
77
//			$query = $this->searchMappingService->generateSearchQuery($provider, $access, $request);
78
//		} catch (SearchQueryGenerationException $e) {
79
//			return null;
80
//		}
81
//
82
//		try {
83
//			$result = $client->search($query['params']);
84
//		} catch (Exception $e) {
85
//			$this->miscService->log(
86
//				'debug - request: ' . json_encode($request) . '   - query: ' . json_encode($query)
87
//			);
88
//			throw $e;
89
//		}
90
//
91
//		$searchResult = $this->generateSearchResultFromResult($result);
92
//
93
//		foreach ($result['hits']['hits'] as $entry) {
94
//			$searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
95
//		}
96
//	}
97
98
	/**
99
	 * @param Client $client
100
	 * @param SearchResult $searchResult
101
	 * @param DocumentAccess $access
102
	 *
103
	 * @throws Exception
104
	 */
105
	public function searchRequest(Client $client, SearchResult $searchResult, DocumentAccess $access
106
	) {
107
		try {
108
			$query = $this->searchMappingService->generateSearchQuery(
109
				$searchResult->getRequest(), $access, $searchResult->getProvider()
110
			);
111
		} catch (SearchQueryGenerationException $e) {
112
			return;
113
		}
114
115
		try {
116
			$result = $client->search($query['params']);
117
		} catch (Exception $e) {
118
			$this->miscService->log(
119
				'debug - request: ' . json_encode($searchResult->getRequest()) . '   - query: '
120
				. json_encode($query)
121
			);
122
			throw $e;
123
		}
124
125
		$this->updateSearchResult($searchResult, $result);
0 ignored issues
show
Documentation introduced by
$result is of type callable, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
126
127
		foreach ($result['hits']['hits'] as $entry) {
128
			$searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
129
		}
130
	}
131
//	/**
132
//	 * @param Client $client
133
//	 * @param IFullTextSearchProvider $provider
134
//	 * @param DocumentAccess $access
135
//	 * @param SearchResult $result
136
//	 *
137
//	 * @return SearchResult
138
//	 * @throws ConfigurationException
139
//	 */
140
//	public function fillSearchResult(
141
//		Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
142
//		SearchResult $searchResult
143
//	) {
144
//		try {
145
//			$query = $this->searchMappingService->generateSearchQuery(
146
//				$provider, $access, $searchResult->getRequest()
147
//			);
148
//		} catch (SearchQueryGenerationException $e) {
149
//			return null;
150
//		}
151
//
152
//		try {
153
//			$result = $client->search($query['params']);
154
//		} catch (Exception $e) {
155
//			$this->miscService->log(
156
//				'debug - request: ' . json_encode($searchResult->getRequest()) . '   - query: '
157
//				. json_encode($query)
158
//			);
159
//			throw $e;
160
//		}
161
//
162
//		$this->updateSearchResult($searchResult, $result);
163
//
164
//		foreach ($result['hits']['hits'] as $entry) {
165
//			$searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
166
//		}
167
//
168
//		return $searchResult;
169
//	}
170
171
172
	/**
173
	 * @param Client $client
174
	 * @param string $providerId
175
	 * @param string $documentId
176
	 *
177
	 * @return IndexDocument
178
	 * @throws ConfigurationException
179
	 */
180
	public function getDocument(Client $client, $providerId, $documentId) {
181
		$query = $this->searchMappingService->getDocumentQuery($providerId, $documentId);
182
		$result = $client->get($query);
183
184
		$access = new DocumentAccess($result['_source']['owner']);
185
		$access->setUsers($result['_source']['users']);
186
		$access->setGroups($result['_source']['groups']);
187
		$access->setCircles($result['_source']['circles']);
188
189
		$index = new IndexDocument($providerId, $documentId);
190
		$index->setAccess($access);
191
		$index->setMetaTags($result['_source']['metatags']);
192
		$index->setSubTags($result['_source']['subtags']);
193
		$index->setTags($result['_source']['tags']);
194
		$index->setMore($result['_source']['more']);
195
//		$index->setInfo($result['_source']['info']);
196
		$index->setHash($result['_source']['hash']);
197
		$index->setSource($result['_source']['source']);
198
		$index->setTitle($result['_source']['title']);
199
		$index->setParts($result['_source']['parts']);
200
		$index->setContent($result['_source']['content']);
201
202
		return $index;
203
	}
204
205
206
	/**
207
	 * @param SearchResult $searchResult
208
	 * @param array $result
209
	 */
210
	private function updateSearchResult(SearchResult $searchResult, $result) {
211
		$searchResult->setRawResult(json_encode($result));
212
213
		$searchResult->setTotal($result['hits']['total']);
214
		$searchResult->setMaxScore($result['hits']['max_score']);
215
		$searchResult->setTime($result['took']);
216
		$searchResult->setTimedOut($result['timed_out']);
217
	}
218
219
220
	/**
221
	 * @param array $entry
222
	 * @param string $viewerId
223
	 *
224
	 * @return IndexDocument
225
	 */
226
	private function parseSearchEntry($entry, $viewerId) {
227
		$access = new DocumentAccess();
228
		$access->setViewerId($viewerId);
229
230
		list($providerId, $documentId) = explode(':', $entry['_id'], 2);
231
		$document = new IndexDocument($providerId, $documentId);
232
		$document->setAccess($access);
233
		$document->setExcerpts(
234
			(array_key_exists('highlight', $entry)) ? $entry['highlight']['content'] : []
235
		);
236
		$document->setHash(MiscService::get($entry['_source'], 'hash'));
237
		$document->setScore($entry['_score']);
238
		$document->setSource(MiscService::get($entry['_source'], 'source'));
239
		$document->setTitle(MiscService::get($entry['_source'], 'title'));
240
241
		return $document;
242
	}
243
244
245
}
246