Completed
Push — master ( ea5aa2...ea5aa2 )
by Maxence
01:52
created

SearchService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 8
c 5
b 0
f 0
lcom 1
cbo 3
dl 0
loc 106
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B searchDocuments() 0 27 4
A generateSearchResultFromResult() 0 11 1
A parseSearchEntry() 0 16 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 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
	 * @return SearchResult
0 ignored issues
show
Documentation introduced by
Should the return type not be SearchResult|null?

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.

Loading history...
70
	 * @throws ConfigurationException
71
	 * @throws Exception
72
	 */
73
	public function searchDocuments(
74
		Client $client, IFullTextSearchProvider $provider, DocumentAccess $access,
75
		SearchRequest $request
76
	) {
77
		try {
78
			$query = $this->searchMappingService->generateSearchQuery($provider, $access, $request);
79
		} catch (SearchQueryGenerationException $e) {
80
			return null;
81
		}
82
83
		try {
84
			$result = $client->search($query['params']);
85
		} catch (Exception $e) {
86
			$this->miscService->log(
87
				'debug - request: ' . json_encode($request) . '   - query: ' . json_encode($query)
88
			);
89
			throw $e;
90
		}
91
92
		$searchResult = $this->generateSearchResultFromResult($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...
93
94
		foreach ($result['hits']['hits'] as $entry) {
95
			$searchResult->addDocument($this->parseSearchEntry($entry, $access->getViewerId()));
96
		}
97
98
		return $searchResult;
99
	}
100
101
102
	/**
103
	 * @param array $result
104
	 *
105
	 * @return SearchResult
106
	 */
107
	private function generateSearchResultFromResult($result) {
108
		$searchResult = new SearchResult();
109
		$searchResult->setRawResult(json_encode($result));
110
111
		$searchResult->setTotal($result['hits']['total']);
112
		$searchResult->setMaxScore($result['hits']['max_score']);
113
		$searchResult->setTime($result['took']);
114
		$searchResult->setTimedOut($result['timed_out']);
115
116
		return $searchResult;
117
	}
118
119
120
	/**
121
	 * @param array $entry
122
	 * @param string $viewerId
123
	 *
124
	 * @return IndexDocument
125
	 */
126
	private function parseSearchEntry($entry, $viewerId) {
127
		$access = new DocumentAccess();
128
		$access->setViewerId($viewerId);
129
130
		list($providerId, $documentId) = explode(':', $entry['_id'], 2);
131
		$document = new IndexDocument($providerId, $documentId);
132
		$document->setAccess($access);
133
		$document->setExcerpts(
134
			(array_key_exists('highlight', $entry)) ? $entry['highlight']['content'] : []
135
		);
136
		$document->setScore($entry['_score']);
137
		$document->setSource(MiscService::get($entry['_source'], 'source'));
138
		$document->setTitle(MiscService::get($entry['_source'], 'title'));
139
140
		return $document;
141
	}
142
143
144
}
145