|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* FullTextSearch - Full text search framework for Nextcloud |
|
7
|
|
|
* |
|
8
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
9
|
|
|
* later. See the COPYING file. |
|
10
|
|
|
* |
|
11
|
|
|
* @author Maxence Lange <[email protected]> |
|
12
|
|
|
* @copyright 2018 |
|
13
|
|
|
* @license GNU AGPL version 3 or any later version |
|
14
|
|
|
* |
|
15
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
16
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
17
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
18
|
|
|
* License, or (at your option) any later version. |
|
19
|
|
|
* |
|
20
|
|
|
* This program is distributed in the hope that it will be useful, |
|
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
23
|
|
|
* GNU Affero General Public License for more details. |
|
24
|
|
|
* |
|
25
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
26
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
27
|
|
|
* |
|
28
|
|
|
*/ |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
namespace OCA\FullTextSearch\Model; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
use JsonSerializable; |
|
35
|
|
|
use OCP\FullTextSearch\IFullTextSearchPlatform; |
|
36
|
|
|
use OCP\FullTextSearch\IFullTextSearchProvider; |
|
37
|
|
|
use OCP\FullTextSearch\Model\IIndexDocument; |
|
38
|
|
|
use OCP\FullTextSearch\Model\ISearchRequest; |
|
39
|
|
|
use OCP\FullTextSearch\Model\ISearchResult; |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Class SearchResult |
|
44
|
|
|
* |
|
45
|
|
|
* @package OCA\FullTextSearch\Model |
|
46
|
|
|
*/ |
|
47
|
|
|
class SearchResult implements ISearchResult, JsonSerializable { |
|
48
|
|
|
|
|
49
|
|
|
/** @var IIndexDocument[] */ |
|
50
|
|
|
private $documents = []; |
|
51
|
|
|
|
|
52
|
|
|
/** @var string */ |
|
53
|
|
|
private $rawResult; |
|
54
|
|
|
|
|
55
|
|
|
/** @var IFullTextSearchProvider */ |
|
56
|
|
|
private $provider; |
|
57
|
|
|
|
|
58
|
|
|
/** @var IFullTextSearchPlatform */ |
|
59
|
|
|
private $platform; |
|
60
|
|
|
|
|
61
|
|
|
/** @var int */ |
|
62
|
|
|
private $total = 0; |
|
63
|
|
|
|
|
64
|
|
|
/** @var int */ |
|
65
|
|
|
private $maxScore = 0; |
|
66
|
|
|
|
|
67
|
|
|
/** @var int */ |
|
68
|
|
|
private $time = 0; |
|
69
|
|
|
|
|
70
|
|
|
/** @var boolean */ |
|
71
|
|
|
private $timedOut = false; |
|
72
|
|
|
|
|
73
|
|
|
/** @var ISearchRequest */ |
|
74
|
|
|
private $request; |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* SearchResult constructor. |
|
79
|
|
|
* |
|
80
|
|
|
* @param SearchRequest $searchRequest |
|
81
|
|
|
*/ |
|
82
|
|
|
public function __construct(SearchRequest $searchRequest) { |
|
83
|
|
|
$this->request = $searchRequest; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param IIndexDocument[] $documents |
|
89
|
|
|
* |
|
90
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
91
|
|
|
*/ |
|
92
|
|
|
public function setDocuments(array $documents): ISearchResult { |
|
93
|
|
|
$this->documents = $documents; |
|
94
|
|
|
|
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return IIndexDocument[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getDocuments(): array { |
|
102
|
|
|
return $this->documents; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param IIndexDocument $document |
|
107
|
|
|
* |
|
108
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
109
|
|
|
*/ |
|
110
|
|
|
public function addDocument(IIndexDocument $document): ISearchResult { |
|
111
|
|
|
$this->documents[] = $document; |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return int |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getCount(): int { |
|
120
|
|
|
return count($this->documents); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $result |
|
126
|
|
|
* |
|
127
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
128
|
|
|
*/ |
|
129
|
|
|
public function setRawResult(string $result): ISearchResult { |
|
130
|
|
|
$this->rawResult = $result; |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getRawResult(): string { |
|
139
|
|
|
return $this->rawResult; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param IFullTextSearchProvider $provider |
|
145
|
|
|
* |
|
146
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
147
|
|
|
*/ |
|
148
|
|
|
public function setProvider(IFullTextSearchProvider $provider): ISearchResult { |
|
149
|
|
|
$this->provider = $provider; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return IFullTextSearchProvider |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getProvider(): IFullTextSearchProvider { |
|
158
|
|
|
return $this->provider; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return IFullTextSearchPlatform |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getPlatform(): IFullTextSearchPlatform { |
|
166
|
|
|
return $this->platform; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param IFullTextSearchPlatform $platform |
|
171
|
|
|
* |
|
172
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
173
|
|
|
*/ |
|
174
|
|
|
public function setPlatform(IFullTextSearchPlatform $platform): ISearchResult { |
|
175
|
|
|
$this->platform = $platform; |
|
176
|
|
|
|
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return int |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getTotal(): int { |
|
185
|
|
|
return $this->total; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param int $total |
|
190
|
|
|
* |
|
191
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
192
|
|
|
*/ |
|
193
|
|
|
public function setTotal(int $total): ISearchResult { |
|
194
|
|
|
$this->total = $total; |
|
195
|
|
|
|
|
196
|
|
|
return $this; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return int |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getMaxScore() { |
|
204
|
|
|
return $this->maxScore; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param int $maxScore |
|
209
|
|
|
* |
|
210
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
211
|
|
|
*/ |
|
212
|
|
|
public function setMaxScore(int $maxScore): ISearchResult { |
|
213
|
|
|
$this->maxScore = $maxScore; |
|
214
|
|
|
|
|
215
|
|
|
return $this; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return int |
|
221
|
|
|
*/ |
|
222
|
|
|
public function getTime(): int { |
|
223
|
|
|
return $this->time; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param int $time |
|
228
|
|
|
* |
|
229
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
230
|
|
|
*/ |
|
231
|
|
|
public function setTime(int $time): ISearchResult { |
|
232
|
|
|
$this->time = $time; |
|
233
|
|
|
|
|
234
|
|
|
return $this; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @return bool |
|
240
|
|
|
*/ |
|
241
|
|
|
public function isTimedOut(): bool { |
|
242
|
|
|
return $this->timedOut; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param bool $timedOut |
|
247
|
|
|
* |
|
248
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
249
|
|
|
*/ |
|
250
|
|
|
public function setTimedOut(bool $timedOut): ISearchResult { |
|
251
|
|
|
$this->timedOut = $timedOut; |
|
252
|
|
|
|
|
253
|
|
|
return $this; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return ISearchRequest |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getRequest(): ISearchRequest { |
|
261
|
|
|
return $this->request; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* @param ISearchRequest $request |
|
266
|
|
|
* |
|
267
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
268
|
|
|
*/ |
|
269
|
|
|
public function setRequest(ISearchRequest $request): ISearchResult { |
|
270
|
|
|
$this->request = $request; |
|
271
|
|
|
|
|
272
|
|
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* @param string $category |
|
278
|
|
|
* @param string $value |
|
279
|
|
|
* @param int $count |
|
280
|
|
|
* |
|
281
|
|
|
* @return ISearchResult |
|
|
|
|
|
|
282
|
|
|
* @since 15.0.0 |
|
283
|
|
|
* |
|
284
|
|
|
*/ |
|
285
|
|
|
public function addAggregation(string $category, string $value, int $count): ISearchResult { |
|
286
|
|
|
// TODO: Implement addAggregation() method. |
|
287
|
|
|
|
|
288
|
|
|
return $this; |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
|
|
/** |
|
292
|
|
|
* @param string $category |
|
293
|
|
|
* |
|
294
|
|
|
* @return array |
|
295
|
|
|
* @since 15.0.0 |
|
296
|
|
|
* |
|
297
|
|
|
*/ |
|
298
|
|
|
public function getAggregations(string $category): array { |
|
299
|
|
|
// TODO: Implement getAggregations() method. |
|
300
|
|
|
|
|
301
|
|
|
return []; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* @return array |
|
307
|
|
|
*/ |
|
308
|
|
|
public function jsonSerialize(): array { |
|
309
|
|
|
|
|
310
|
|
|
$providerObj = $this->getProvider(); |
|
311
|
|
|
$provider = []; |
|
312
|
|
|
if ($providerObj !== null) { |
|
313
|
|
|
$provider = [ |
|
314
|
|
|
'id' => $providerObj->getId(), |
|
315
|
|
|
'name' => $providerObj->getName() |
|
316
|
|
|
]; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
$platformObj = $this->getPlatform(); |
|
320
|
|
|
$platform = []; |
|
321
|
|
|
if ($platformObj !== null) { |
|
322
|
|
|
$platform = [ |
|
323
|
|
|
'id' => $platformObj->getId(), |
|
324
|
|
|
'name' => $platformObj->getName() |
|
325
|
|
|
]; |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
return [ |
|
329
|
|
|
'provider' => $provider, |
|
330
|
|
|
'platform' => $platform, |
|
331
|
|
|
'documents' => $this->getDocuments(), |
|
332
|
|
|
'info' => $this->getInfosAll(), |
|
333
|
|
|
'meta' => |
|
334
|
|
|
[ |
|
335
|
|
|
'timedOut' => $this->isTimedOut(), |
|
336
|
|
|
'time' => $this->getTime(), |
|
337
|
|
|
'count' => $this->getCount(), |
|
338
|
|
|
'total' => $this->getTotal(), |
|
339
|
|
|
'maxScore' => $this->getMaxScore() |
|
340
|
|
|
] |
|
341
|
|
|
]; |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
public function addInfo(string $k, string $value): ISearchResult { |
|
345
|
|
|
return $this; |
|
346
|
|
|
} |
|
347
|
|
|
|
|
348
|
|
|
public function getInfo(string $k): string { |
|
349
|
|
|
return ''; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
public function getInfosAll(): array { |
|
353
|
|
|
return []; |
|
354
|
|
|
} |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.