Passed
Push — dev ( 6bdfb6...0bfd47 )
by Darko
07:29
created

ElasticSearchSiteSearch::indexSearchTMA()   A

Complexity

Conditions 5
Paths 19

Size

Total Lines 51
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 2 Features 0
Metric Value
eloc 30
c 4
b 2
f 0
dl 0
loc 51
rs 9.1288
cc 5
nc 19
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Blacklight;
4
5
use App\Models\Release;
6
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
7
use Illuminate\Support\Facades\DB;
8
use Illuminate\Support\Str;
9
use sspat\ESQuerySanitizer\Sanitizer;
10
11
class ElasticSearchSiteSearch
12
{
13
    /**
14
     * @param string|array $phrases
15
     * @param int $limit
16
     * @return mixed
17
     */
18
    public function indexSearch($phrases, int $limit)
19
    {
20
        $keywords = $this->sanitize($phrases);
21
        if (Str::length($phrases) === 1) {
0 ignored issues
show
Bug introduced by
It seems like $phrases can also be of type array; however, parameter $value of Illuminate\Support\Str::length() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

21
        if (Str::length(/** @scrutinizer ignore-type */ $phrases) === 1) {
Loading history...
22
            try {
23
                $search = [
24
                'scroll' => '30s',
25
                'index' => 'releases',
26
                'body' => [
27
                    'query' => [
28
                        'query_string' => [
29
                            'query' => $keywords,
30
                            'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name'],
31
                            'analyze_wildcard' => true,
32
                            'default_operator' => 'and',
33
                        ],
34
                    ],
35
                    'size' => $limit,
36
                    'sort' => [
37
                        'add_date' => [
38
                            'order' => 'desc',
39
                        ],
40
                        'post_date' => [
41
                            'order' => 'desc',
42
                        ],
43
                    ],
44
                ],
45
            ];
46
47
                $results = \Elasticsearch::search($search);
48
49
                $searchResult = [];
50
                while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
51
                    foreach ($results['hits']['hits'] as $result) {
52
                        $searchResult[] = $result['_source']['id'];
53
                    }
54
55
                    // When done, get the new scroll_id
56
                    // You must always refresh your _scroll_id!  It can change sometimes
57
                    $scroll_id = $results['_scroll_id'];
58
59
                    // Execute a Scroll request and repeat
60
                    $results = \Elasticsearch::scroll([
61
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
62
                    'scroll' => '30s',        // and the same timeout window
63
                ]
64
                );
65
                }
66
67
                return $searchResult;
68
            } catch (BadRequest400Exception $request400Exception) {
69
                return [];
70
            }
71
        }
72
    }
73
74
    /**
75
     * @param string|array $searchName
76
     * @param int $limit
77
     * @return array
78
     */
79
    public function indexSearchApi($searchName, int $limit)
80
    {
81
        $keywords = $this->sanitize($searchName);
82
        try {
83
            $search = [
84
                'scroll' => '30s',
85
                'index' => 'releases',
86
                'body' => [
87
                    'query' => [
88
                        'query_string' => [
89
                            'query' => $keywords,
90
                            'fields' => ['searchname', 'plainsearchname'],
91
                            'analyze_wildcard' => true,
92
                            'default_operator' => 'and',
93
                        ],
94
                    ],
95
                    'size' => $limit,
96
                    'sort' => [
97
                        'add_date' => [
98
                            'order' => 'desc',
99
                        ],
100
                        'post_date' => [
101
                            'order' => 'desc',
102
                        ],
103
                    ],
104
                ],
105
            ];
106
107
            $results = \Elasticsearch::search($search);
108
109
            $searchResult = [];
110
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
111
                foreach ($results['hits']['hits'] as $result) {
112
                    $searchResult[] = $result['_source']['id'];
113
                }
114
115
                // When done, get the new scroll_id
116
                // You must always refresh your _scroll_id!  It can change sometimes
117
                $scroll_id = $results['_scroll_id'];
118
119
                // Execute a Scroll request and repeat
120
                $results = \Elasticsearch::scroll([
121
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
122
                    'scroll' => '30s',        // and the same timeout window
123
                ]
124
                );
125
            }
126
127
            return $searchResult;
128
        } catch (BadRequest400Exception $request400Exception) {
129
            return [];
130
        }
131
    }
132
133
    /**
134
     * Search function used in TV, TV API, Movies and Anime searches.
135
     * @param string|array $name
136
     * @param int $limit
137
     * @return array
138
     */
139
    public function indexSearchTMA($name, $limit)
140
    {
141
        $keywords = $this->sanitize($name);
142
        try {
143
            $search = [
144
                'scroll' => '30s',
145
                'index' => 'releases',
146
                'body' => [
147
                    'query' => [
148
                        'query_string' => [
149
                            'query' => $keywords,
150
                            'fields' => ['searchname', 'plainsearchname'],
151
                            'analyze_wildcard' => true,
152
                            'default_operator' => 'and',
153
                        ],
154
                    ],
155
                    'size' => $limit,
156
                    'sort' => [
157
                        'add_date' => [
158
                            'order' =>'desc',
159
                        ],
160
                        'post_date' => [
161
                            'order' => 'desc',
162
                        ],
163
                    ],
164
                ],
165
            ];
166
167
            $results = \Elasticsearch::search($search);
168
169
            $searchResult = [];
170
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
171
                foreach ($results['hits']['hits'] as $result) {
172
                    $searchResult[] = $result['_source']['id'];
173
                }
174
175
                // When done, get the new scroll_id
176
                // You must always refresh your _scroll_id!  It can change sometimes
177
                $scroll_id = $results['_scroll_id'];
178
179
                // Execute a Scroll request and repeat
180
                $results = \Elasticsearch::scroll([
181
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
182
                    'scroll'    => '30s',        // and the same timeout window
183
                ]
184
                );
185
            }
186
187
            return $searchResult;
188
        } catch (BadRequest400Exception $request400Exception) {
189
            return [];
190
        }
191
    }
192
193
    /**
194
     * @param string|array $search
195
     * @return array|\Illuminate\Support\Collection
196
     */
197
    public function predbIndexSearch($search)
198
    {
199
        try {
200
            $search = [
201
                'scroll' => '30s',
202
                'index' => 'predb',
203
                'body' => [
204
                    'query' => [
205
                        'query_string' => [
206
                            'query' => $search,
207
                            'fields' => ['title'],
208
                            'analyze_wildcard' => true,
209
                            'default_operator' => 'and',
210
                        ],
211
                    ],
212
                    'size' => 1000,
213
                ],
214
            ];
215
216
            $results = \Elasticsearch::search($search);
217
218
            $ids = [];
219
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
220
                foreach ($results['hits']['hits'] as $result) {
221
                    $ids[] = $result['_source']['id'];
222
                }
223
                if (empty($ids)) {
224
                    return collect();
225
                }
226
                // When done, get the new scroll_id
227
                // You must always refresh your _scroll_id!  It can change sometimes
228
                $scroll_id = $results['_scroll_id'];
229
230
                // Execute a Scroll request and repeat
231
                $results = \Elasticsearch::scroll([
232
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
233
                    'scroll' => '30s',        // and the same timeout window
234
                ]
235
                );
236
            }
237
238
            return $ids;
239
        } catch (BadRequest400Exception $request400Exception) {
240
            return [];
241
        }
242
    }
243
244
    /**
245
     * @param array $parameters
246
     */
247
    public function insertRelease(array $parameters): void
248
    {
249
        $searchNameDotless = str_replace(['.', '-'], ' ', $parameters['searchname']);
250
        $data = [
251
            'body' => [
252
                'id' => $parameters['id'],
253
                'name' => $parameters['name'],
254
                'searchname' => $parameters['searchname'],
255
                'plainsearchname' => $searchNameDotless,
256
                'fromname' => $parameters['fromname'],
257
                'filename' => $parameters['filename'] ?? '',
258
                'add_date' => now()->format('Y-m-d H:i:s'),
259
                'post_date' => $parameters['postdate'],
260
            ],
261
            'index' => 'releases',
262
            'id' => $parameters['id'],
263
        ];
264
265
        \Elasticsearch::index($data);
266
    }
267
268
    /**
269
     * @param int $id
270
     */
271
    public function updateRelease(int $id)
272
    {
273
        $new = Release::query()
274
            ->where('releases.id', $id)
275
            ->leftJoin('release_files as rf', 'releases.id', '=', 'rf.releases_id')
276
            ->select(['releases.id', 'releases.name', 'releases.searchname', 'releases.fromname', DB::raw('IFNULL(GROUP_CONCAT(rf.name SEPARATOR " "),"") filename')])
277
            ->groupBy('releases.id')
278
            ->first();
279
        if ($new !== null) {
280
            $searchNameDotless = str_replace(['.', '-'], ' ', $new->searchname);
281
            $data = [
282
                'body' => [
283
                    'doc' => [
284
                        'id' => $new->id,
285
                        'name' => $new->name,
286
                        'searchname' => $new->searchname,
287
                        'plainsearchname' => $searchNameDotless,
288
                        'fromname' => $new->fromname,
289
                        'filename' => $new->filename,
0 ignored issues
show
Bug introduced by
The property filename does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
290
                    ],
291
                    'doc_as_upsert' => true,
292
                ],
293
294
                'index' => 'releases',
295
                'id' => $new->id,
296
            ];
297
298
            \Elasticsearch::update($data);
299
        }
300
    }
301
302
    /**
303
     * @param $searchTerm
304
     * @return array
305
     */
306
    public function searchPreDb($searchTerm)
307
    {
308
        $search = [
309
            'index' => 'predb',
310
            'body' => [
311
                'query' => [
312
                    'query_string' => [
313
                        'query' => $searchTerm,
314
                        'fields' => ['title', 'filename'],
315
                        'analyze_wildcard' => true,
316
                        'default_operator' => 'and',
317
                    ],
318
                ],
319
            ],
320
        ];
321
322
        try {
323
            $primaryResults = \Elasticsearch::search($search);
324
325
            $results = [];
326
            foreach ($primaryResults['hits']['hits'] as $primaryResult) {
327
                $results[] = $primaryResult['_source'];
328
            }
329
        } catch (BadRequest400Exception $badRequest400Exception) {
330
            return [];
331
        }
332
333
        return $results;
334
    }
335
336
    /**
337
     * @param $parameters
338
     */
339
    public function insertPreDb($parameters)
340
    {
341
        $data = [
342
            'body' => [
343
                'id' => $parameters['id'],
344
                'title' => $parameters['title'],
345
                'source' => $parameters['source'],
346
                'filename' => $parameters['filename'],
347
            ],
348
            'index' => 'predb',
349
            'id' => $parameters['id'],
350
        ];
351
352
        \Elasticsearch::index($data);
353
    }
354
355
    /**
356
     * @param $parameters
357
     */
358
    public function updatePreDb($parameters)
359
    {
360
        $data = [
361
            'body' => [
362
                'doc' => [
363
                    'id' => $parameters['id'],
364
                    'title' => $parameters['title'],
365
                    'filename' => $parameters['filename'],
366
                    'source' => $parameters['source'],
367
                ],
368
                'doc_as_upsert' => true,
369
            ],
370
371
            'index' => 'predb',
372
            'id' => $parameters['id'],
373
        ];
374
375
        \Elasticsearch::update($data);
376
    }
377
378
    /**
379
     * @param array|string $phrases
380
     * @return string
381
     */
382
    private function sanitize($phrases): string
383
    {
384
        if (! is_array($phrases)) {
385
            $wordArray = explode(' ', str_replace('.', ' ', $phrases));
386
        } else {
387
            $wordArray = $phrases;
388
        }
389
        $keywords = [];
390
        foreach ($wordArray as $words) {
391
            $tempWords = [];
392
            $words = preg_split('/\s+/', $words);
393
            foreach ($words as $st) {
394
                if (Str::startsWith($st, ['!', '+', '-', '?', '*']) && Str::length($st) > 1 && ! preg_match('/(!|\+|\?|-|\*){2,}/', $st)) {
395
                    $str = $st;
396
                } elseif (Str::endsWith($st, ['+', '-', '?', '*']) && Str::length($st) > 1 && ! preg_match('/(!|\+|\?|-|\*){2,}/', $st)) {
397
                    $str = $st;
398
                } else {
399
                    $str = Sanitizer::escape($st);
400
                }
401
                $tempWords[] = $str;
402
            }
403
404
            $keywords = $tempWords;
405
        }
406
407
        return implode(' ', $keywords);
408
    }
409
}
410