Passed
Push — dev ( c0d036...50e7e9 )
by Darko
06:53
created

ElasticSearchSiteSearch::sanitize()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 14
c 2
b 1
f 0
dl 0
loc 20
rs 9.4888
cc 5
nc 8
nop 1
1
<?php
2
3
namespace Blacklight;
4
5
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
6
use Illuminate\Support\Str;
7
use sspat\ESQuerySanitizer\Sanitizer;
8
9
class ElasticSearchSiteSearch
10
{
11
    /**
12
     * @param $phrases
13
     * @param $limit
14
     * @return mixed
15
     */
16
    public function indexSearch($phrases, $limit)
17
    {
18
        $keywords = $this->sanitize($phrases);
19
        try {
20
            $search = [
21
                'scroll' => '30s',
22
                'index' => 'releases',
23
                'body' => [
24
                    'query' => [
25
                        'query_string' => [
26
                            'query' => $keywords,
27
                            'fields' => ['searchname', 'plainsearchname', 'fromname', 'filename', 'name'],
28
                            'analyze_wildcard' => true,
29
                            'default_operator' => 'and',
30
                        ],
31
                    ],
32
                    'size' => $limit,
33
                    'sort' => [
34
                        'add_date' => [
35
                            'order' => 'desc',
36
                        ],
37
                        'post_date' => [
38
                            'order' => 'desc',
39
                        ],
40
                    ],
41
                ],
42
            ];
43
44
            $results = \Elasticsearch::search($search);
45
46
            $searchResult = [];
47
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
48
                foreach ($results['hits']['hits'] as $result) {
49
                    $searchResult[] = $result['_source']['id'];
50
                }
51
52
                // When done, get the new scroll_id
53
                // You must always refresh your _scroll_id!  It can change sometimes
54
                $scroll_id = $results['_scroll_id'];
55
56
                // Execute a Scroll request and repeat
57
                $results = \Elasticsearch::scroll([
58
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
59
                    'scroll' => '30s',        // and the same timeout window
60
                ]
61
                );
62
            }
63
64
            return $searchResult;
65
        } catch (BadRequest400Exception $request400Exception) {
66
            return [];
67
        }
68
    }
69
70
    /**
71
     * @param $searchName
72
     * @param $limit
73
     * @return array
74
     */
75
    public function indexSearchApi($searchName, $limit)
76
    {
77
        $keywords = $this->sanitize($searchName);
78
        try {
79
            $search = [
80
                'scroll' => '30s',
81
                'index' => 'releases',
82
                'body' => [
83
                    'query' => [
84
                        'query_string' => [
85
                            'query' => $keywords,
86
                            'fields' => ['searchname', 'plainsearchname'],
87
                            'analyze_wildcard' => true,
88
                            'default_operator' => 'and',
89
                        ],
90
                    ],
91
                    'size' => $limit,
92
                    'sort' => [
93
                        'add_date' => [
94
                            'order' => 'desc',
95
                        ],
96
                        'post_date' => [
97
                            'order' => 'desc',
98
                        ],
99
                    ],
100
                ],
101
            ];
102
103
            $results = \Elasticsearch::search($search);
104
105
            $searchResult = [];
106
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
107
                foreach ($results['hits']['hits'] as $result) {
108
                    $searchResult[] = $result['_source']['id'];
109
                }
110
111
                // When done, get the new scroll_id
112
                // You must always refresh your _scroll_id!  It can change sometimes
113
                $scroll_id = $results['_scroll_id'];
114
115
                // Execute a Scroll request and repeat
116
                $results = \Elasticsearch::scroll([
117
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
118
                    'scroll' => '30s',        // and the same timeout window
119
                ]
120
                );
121
            }
122
123
            return $searchResult;
124
        } catch (BadRequest400Exception $request400Exception) {
125
            return [];
126
        }
127
    }
128
129
    /**
130
     * Search function used in TV, TV API, Movies and Anime searches.
131
     * @param $name
132
     * @param $limit
133
     * @return array
134
     */
135
    public function indexSearchTMA($name, $limit)
136
    {
137
        $keywords = $this->sanitize($name);
138
        try {
139
            $search = [
140
                'scroll' => '30s',
141
                'index' => 'releases',
142
                'body' => [
143
                    'query' => [
144
                        'query_string' => [
145
                            'query' => $keywords,
146
                            'fields' => ['searchname', 'plainsearchname'],
147
                            'analyze_wildcard' => true,
148
                            'default_operator' => 'and',
149
                        ],
150
                    ],
151
                    'size' => $limit,
152
                    'sort' => [
153
                        'add_date' => [
154
                            'order' =>'desc',
155
                        ],
156
                        'post_date' => [
157
                            'order' => 'desc',
158
                        ],
159
                    ],
160
                ],
161
            ];
162
163
            $results = \Elasticsearch::search($search);
164
165
            $searchResult = [];
166
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
167
                foreach ($results['hits']['hits'] as $result) {
168
                    $searchResult[] = $result['_source']['id'];
169
                }
170
171
                // When done, get the new scroll_id
172
                // You must always refresh your _scroll_id!  It can change sometimes
173
                $scroll_id = $results['_scroll_id'];
174
175
                // Execute a Scroll request and repeat
176
                $results = \Elasticsearch::scroll([
177
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
178
                    'scroll'    => '30s',        // and the same timeout window
179
                ]
180
                );
181
            }
182
183
            return $searchResult;
184
        } catch (BadRequest400Exception $request400Exception) {
185
            return [];
186
        }
187
    }
188
189
    /**
190
     * @param $search
191
     * @return array|\Illuminate\Support\Collection
192
     */
193
    public function predbIndexSearch($search)
194
    {
195
        $keywords = $this->sanitize($search);
196
        try {
197
            $search = [
198
                'scroll' => '30s',
199
                'index' => 'predb',
200
                'body' => [
201
                    'query' => [
202
                        'query_string' => [
203
                            'query' => $keywords,
204
                            'fields' => ['title'],
205
                            'analyze_wildcard' => true,
206
                            'default_operator' => 'and',
207
                        ],
208
                    ],
209
                    'size' => 1000,
210
                ],
211
            ];
212
213
            $results = \Elasticsearch::search($search);
214
215
            $ids = [];
216
            while (isset($results['hits']['hits']) && count($results['hits']['hits']) > 0) {
217
                foreach ($results['hits']['hits'] as $result) {
218
                    $ids[] = $result['_source']['id'];
219
                }
220
                if (empty($ids)) {
221
                    return collect();
222
                }
223
                // When done, get the new scroll_id
224
                // You must always refresh your _scroll_id!  It can change sometimes
225
                $scroll_id = $results['_scroll_id'];
226
227
                // Execute a Scroll request and repeat
228
                $results = \Elasticsearch::scroll([
229
                    'scroll_id' => $scroll_id,  //...using our previously obtained _scroll_id
230
                    'scroll' => '30s',        // and the same timeout window
231
                ]
232
                );
233
            }
234
235
            return $ids;
236
        } catch (BadRequest400Exception $request400Exception) {
237
            return [];
238
        }
239
    }
240
241
    /**
242
     * @param array|string $phrases
243
     * @return string
244
     */
245
    private function sanitize($phrases): string
246
    {
247
        if (! is_array($phrases)) {
248
            $wordArray = explode(' ', str_replace('.', ' ', $phrases));
249
        } else {
250
            $wordArray = $phrases;
251
        }
252
        $words = [];
253
        foreach ($wordArray as $st) {
254
            if (Str::startsWith($st, ['!', '+', '-', '?', '*'])) {
255
                $str = $st;
256
            } elseif (Str::endsWith($st, ['+', '-', '?', '*'])) {
257
                $str = $st;
258
            } else {
259
                $str = Sanitizer::escape($st);
260
            }
261
            $words[] = $str;
262
        }
263
264
        return implode(' ', $words);
265
    }
266
}
267