Passed
Push — dev ( 535ca7...b7c17d )
by Darko
07:22
created

ElasticSearchSiteSearch::indexSearchApi()   A

Complexity

Conditions 5
Paths 19

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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