|
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 |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
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
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.