1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blacklight; |
4
|
|
|
|
5
|
|
|
use App\Models\Release; |
6
|
|
|
use Elasticsearch\Common\Exceptions\BadRequest400Exception; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use sspat\ESQuerySanitizer\Sanitizer; |
9
|
|
|
|
10
|
|
|
class ElasticSearchSiteSearch |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string|array $phrases |
14
|
|
|
* @param int $limit |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
|
|
public function indexSearch($phrases, int $limit) |
18
|
|
|
{ |
19
|
|
|
$keywords = $this->sanitize($phrases); |
20
|
|
|
try { |
21
|
|
|
$search = [ |
22
|
|
|
'scroll' => '30s', |
23
|
|
|
'index' => 'releases', |
24
|
|
|
'body' => [ |
25
|
|
|
'query' => [ |
26
|
|
|
'query_string' => [ |
27
|
|
|
'query' => $keywords, |
28
|
|
|
'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name'], |
29
|
|
|
'analyze_wildcard' => true, |
30
|
|
|
'default_operator' => 'and', |
31
|
|
|
], |
32
|
|
|
], |
33
|
|
|
'size' => $limit, |
34
|
|
|
'sort' => [ |
35
|
|
|
'add_date' => [ |
36
|
|
|
'order' => 'desc', |
37
|
|
|
], |
38
|
|
|
'post_date' => [ |
39
|
|
|
'order' => 'desc', |
40
|
|
|
], |
41
|
|
|
], |
42
|
|
|
], |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
$results = \Elasticsearch::search($search); |
46
|
|
|
|
47
|
|
|
$searchResult = []; |
48
|
|
|
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
49
|
|
|
foreach ($results['hits']['hits'] as $result) { |
50
|
|
|
$searchResult[] = $result['_source']['id']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// When done, get the new scroll_id |
54
|
|
|
// You must always refresh your _scroll_id! It can change sometimes |
55
|
|
|
$scroll_id = $results['_scroll_id']; |
56
|
|
|
|
57
|
|
|
// Execute a Scroll request and repeat |
58
|
|
|
$results = \Elasticsearch::scroll([ |
59
|
|
|
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
60
|
|
|
'scroll' => '30s', // and the same timeout window |
61
|
|
|
] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $searchResult; |
66
|
|
|
} catch (BadRequest400Exception $request400Exception) { |
67
|
|
|
return []; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string|array $searchName |
73
|
|
|
* @param int $limit |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function indexSearchApi($searchName, int $limit) |
77
|
|
|
{ |
78
|
|
|
$keywords = $this->sanitize($searchName); |
79
|
|
|
try { |
80
|
|
|
$search = [ |
81
|
|
|
'scroll' => '30s', |
82
|
|
|
'index' => 'releases', |
83
|
|
|
'body' => [ |
84
|
|
|
'query' => [ |
85
|
|
|
'query_string' => [ |
86
|
|
|
'query' => $keywords, |
87
|
|
|
'fields' => ['searchname', 'plainsearchname'], |
88
|
|
|
'analyze_wildcard' => true, |
89
|
|
|
'default_operator' => 'and', |
90
|
|
|
], |
91
|
|
|
], |
92
|
|
|
'size' => $limit, |
93
|
|
|
'sort' => [ |
94
|
|
|
'add_date' => [ |
95
|
|
|
'order' => 'desc', |
96
|
|
|
], |
97
|
|
|
'post_date' => [ |
98
|
|
|
'order' => 'desc', |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
], |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
$results = \Elasticsearch::search($search); |
105
|
|
|
|
106
|
|
|
$searchResult = []; |
107
|
|
|
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
108
|
|
|
foreach ($results['hits']['hits'] as $result) { |
109
|
|
|
$searchResult[] = $result['_source']['id']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// When done, get the new scroll_id |
113
|
|
|
// You must always refresh your _scroll_id! It can change sometimes |
114
|
|
|
$scroll_id = $results['_scroll_id']; |
115
|
|
|
|
116
|
|
|
// Execute a Scroll request and repeat |
117
|
|
|
$results = \Elasticsearch::scroll([ |
118
|
|
|
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
119
|
|
|
'scroll' => '30s', // and the same timeout window |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return $searchResult; |
125
|
|
|
} catch (BadRequest400Exception $request400Exception) { |
126
|
|
|
return []; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Search function used in TV, TV API, Movies and Anime searches. |
132
|
|
|
* @param string|array $name |
133
|
|
|
* @param int $limit |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function indexSearchTMA($name, $limit) |
137
|
|
|
{ |
138
|
|
|
$keywords = $this->sanitize($name); |
139
|
|
|
try { |
140
|
|
|
$search = [ |
141
|
|
|
'scroll' => '30s', |
142
|
|
|
'index' => 'releases', |
143
|
|
|
'body' => [ |
144
|
|
|
'query' => [ |
145
|
|
|
'query_string' => [ |
146
|
|
|
'query' => $keywords, |
147
|
|
|
'fields' => ['searchname', 'plainsearchname'], |
148
|
|
|
'analyze_wildcard' => true, |
149
|
|
|
'default_operator' => 'and', |
150
|
|
|
], |
151
|
|
|
], |
152
|
|
|
'size' => $limit, |
153
|
|
|
'sort' => [ |
154
|
|
|
'add_date' => [ |
155
|
|
|
'order' =>'desc', |
156
|
|
|
], |
157
|
|
|
'post_date' => [ |
158
|
|
|
'order' => 'desc', |
159
|
|
|
], |
160
|
|
|
], |
161
|
|
|
], |
162
|
|
|
]; |
163
|
|
|
|
164
|
|
|
$results = \Elasticsearch::search($search); |
165
|
|
|
|
166
|
|
|
$searchResult = []; |
167
|
|
|
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
168
|
|
|
foreach ($results['hits']['hits'] as $result) { |
169
|
|
|
$searchResult[] = $result['_source']['id']; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// When done, get the new scroll_id |
173
|
|
|
// You must always refresh your _scroll_id! It can change sometimes |
174
|
|
|
$scroll_id = $results['_scroll_id']; |
175
|
|
|
|
176
|
|
|
// Execute a Scroll request and repeat |
177
|
|
|
$results = \Elasticsearch::scroll([ |
178
|
|
|
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
179
|
|
|
'scroll' => '30s', // and the same timeout window |
180
|
|
|
] |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $searchResult; |
185
|
|
|
} catch (BadRequest400Exception $request400Exception) { |
186
|
|
|
return []; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param string|array $search |
192
|
|
|
* @return array|\Illuminate\Support\Collection |
193
|
|
|
*/ |
194
|
|
|
public function predbIndexSearch($search) |
195
|
|
|
{ |
196
|
|
|
$keywords = $this->sanitize($search); |
197
|
|
|
try { |
198
|
|
|
$search = [ |
199
|
|
|
'scroll' => '30s', |
200
|
|
|
'index' => 'predb', |
201
|
|
|
'body' => [ |
202
|
|
|
'query' => [ |
203
|
|
|
'query_string' => [ |
204
|
|
|
'query' => $keywords, |
205
|
|
|
'fields' => ['title'], |
206
|
|
|
'analyze_wildcard' => true, |
207
|
|
|
'default_operator' => 'and', |
208
|
|
|
], |
209
|
|
|
], |
210
|
|
|
'size' => 1000, |
211
|
|
|
], |
212
|
|
|
]; |
213
|
|
|
|
214
|
|
|
$results = \Elasticsearch::search($search); |
215
|
|
|
|
216
|
|
|
$ids = []; |
217
|
|
|
while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) { |
218
|
|
|
foreach ($results['hits']['hits'] as $result) { |
219
|
|
|
$ids[] = $result['_source']['id']; |
220
|
|
|
} |
221
|
|
|
if (empty($ids)) { |
222
|
|
|
return collect(); |
223
|
|
|
} |
224
|
|
|
// When done, get the new scroll_id |
225
|
|
|
// You must always refresh your _scroll_id! It can change sometimes |
226
|
|
|
$scroll_id = $results['_scroll_id']; |
227
|
|
|
|
228
|
|
|
// Execute a Scroll request and repeat |
229
|
|
|
$results = \Elasticsearch::scroll([ |
230
|
|
|
'scroll_id' => $scroll_id, //...using our previously obtained _scroll_id |
231
|
|
|
'scroll' => '30s', // and the same timeout window |
232
|
|
|
] |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
return $ids; |
237
|
|
|
} catch (BadRequest400Exception $request400Exception) { |
238
|
|
|
return []; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param array $parameters |
244
|
|
|
*/ |
245
|
|
|
public function insertRelease(array $parameters): void |
246
|
|
|
{ |
247
|
|
|
$searchNameDotless = str_replace(['.', '-'], ' ', $parameters['searchname']); |
248
|
|
|
$data = [ |
249
|
|
|
'body' => [ |
250
|
|
|
'id' => $parameters['id'], |
251
|
|
|
'name' => $parameters['name'], |
252
|
|
|
'searchname' => $parameters['searchname'], |
253
|
|
|
'plainsearchname' => $searchNameDotless, |
254
|
|
|
'fromname' => $parameters['fromname'], |
255
|
|
|
'filename' => $parameters['filename'] ?? '', |
256
|
|
|
'add_date' => now()->format('Y-m-d H:i:s'), |
257
|
|
|
'post_date' => $parameters['postdate'], |
258
|
|
|
], |
259
|
|
|
'index' => 'releases', |
260
|
|
|
'id' => $parameters['id'], |
261
|
|
|
]; |
262
|
|
|
|
263
|
|
|
\Elasticsearch::index($data); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param int $id |
268
|
|
|
*/ |
269
|
|
|
public function updateRelease(int $id) |
270
|
|
|
{ |
271
|
|
|
$new = Release::query() |
272
|
|
|
->where('releases.id', $id) |
273
|
|
|
->leftJoin('release_files as rf', 'releases.id', '=', 'rf.releases_id') |
274
|
|
|
->select(['releases.id', 'releases.name', 'releases.searchname', 'releases.fromname', DB::raw('IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename')]) |
275
|
|
|
->groupBy('releases.id') |
276
|
|
|
->first(); |
277
|
|
|
if ($new !== null) { |
278
|
|
|
$searchNameDotless = str_replace(['.', '-'], ' ', $new->searchname); |
279
|
|
|
$data = [ |
280
|
|
|
'body' => [ |
281
|
|
|
'doc' => [ |
282
|
|
|
'id' => $new->id, |
283
|
|
|
'name' => $new->name, |
284
|
|
|
'searchname' => $new->searchname, |
285
|
|
|
'plainsearchname' => $searchNameDotless, |
286
|
|
|
'fromname' => $new->fromname, |
287
|
|
|
'filename' => $new->filename, |
|
|
|
|
288
|
|
|
], |
289
|
|
|
'doc_as_upsert' => true, |
290
|
|
|
], |
291
|
|
|
|
292
|
|
|
'index' => 'releases', |
293
|
|
|
'id' => $new->id, |
294
|
|
|
]; |
295
|
|
|
|
296
|
|
|
\Elasticsearch::update($data); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @param $searchTerm |
302
|
|
|
* @return array |
303
|
|
|
*/ |
304
|
|
|
public function searchPreDb($searchTerm) |
305
|
|
|
{ |
306
|
|
|
$keywords = $this->sanitize($searchTerm); |
307
|
|
|
$search = [ |
308
|
|
|
'index' => 'predb', |
309
|
|
|
'body' => [ |
310
|
|
|
'query' => [ |
311
|
|
|
'query_string' => [ |
312
|
|
|
'query' => $keywords, |
313
|
|
|
'fields' => ['title', 'filename'], |
314
|
|
|
'analyze_wildcard' => true, |
315
|
|
|
'default_operator' => 'and', |
316
|
|
|
], |
317
|
|
|
], |
318
|
|
|
], |
319
|
|
|
]; |
320
|
|
|
|
321
|
|
|
try { |
322
|
|
|
$primaryResults = \Elasticsearch::search($search); |
323
|
|
|
|
324
|
|
|
$results = []; |
325
|
|
|
foreach ($primaryResults['hits']['hits'] as $primaryResult) { |
326
|
|
|
$results[] = $primaryResult['_source']; |
327
|
|
|
} |
328
|
|
|
} catch (BadRequest400Exception $badRequest400Exception) { |
329
|
|
|
return []; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return $results; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param $parameters |
337
|
|
|
*/ |
338
|
|
|
public function insertPreDb($parameters) |
339
|
|
|
{ |
340
|
|
|
$data = [ |
341
|
|
|
'body' => [ |
342
|
|
|
'id' => $parameters['id'], |
343
|
|
|
'title' => $parameters['title'], |
344
|
|
|
'source' => $parameters['source'], |
345
|
|
|
'filename' => $parameters['filename'], |
346
|
|
|
], |
347
|
|
|
'index' => 'predb', |
348
|
|
|
'id' => $parameters['id'], |
349
|
|
|
]; |
350
|
|
|
|
351
|
|
|
\Elasticsearch::index($data); |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @param $parameters |
356
|
|
|
*/ |
357
|
|
|
public function updatePreDb($parameters) |
358
|
|
|
{ |
359
|
|
|
$data = [ |
360
|
|
|
'body' => [ |
361
|
|
|
'doc' => [ |
362
|
|
|
'id' => $parameters['id'], |
363
|
|
|
'title' => $parameters['title'], |
364
|
|
|
'filename' => $parameters['filename'], |
365
|
|
|
'source' => $parameters['source'], |
366
|
|
|
], |
367
|
|
|
'doc_as_upsert' => true, |
368
|
|
|
], |
369
|
|
|
|
370
|
|
|
'index' => 'predb', |
371
|
|
|
'id' => $parameters['id'], |
372
|
|
|
]; |
373
|
|
|
|
374
|
|
|
\Elasticsearch::update($data); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* @param array|string $phrases |
379
|
|
|
* @return string |
380
|
|
|
*/ |
381
|
|
|
private function sanitize($phrases): string |
382
|
|
|
{ |
383
|
|
|
if (! is_array($phrases)) { |
384
|
|
|
$wordArray = explode(' ', str_replace('.', ' ', $phrases)); |
385
|
|
|
} else { |
386
|
|
|
$wordArray = $phrases; |
387
|
|
|
} |
388
|
|
|
$keywords = []; |
389
|
|
|
foreach ($wordArray as $words) { |
390
|
|
|
$tempWords = []; |
391
|
|
|
$words = preg_split('/\s+/', $words); |
392
|
|
|
foreach ($words as $st) { |
393
|
|
|
if (Str::startsWith($st, ['!', '+', '-', '?', '*'])) { |
394
|
|
|
$str = $st; |
395
|
|
|
} elseif (Str::endsWith($st, ['+', '-', '?', '*'])) { |
396
|
|
|
$str = $st; |
397
|
|
|
} else { |
398
|
|
|
$str = Sanitizer::escape($st); |
399
|
|
|
} |
400
|
|
|
$tempWords[] = $str; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
$keywords = $tempWords; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
return implode(' ', $keywords); |
407
|
|
|
} |
408
|
|
|
} |
409
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.