ScoutElasticEngine::paginate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 6
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Alhoqbani\Elastic;
4
5
use Elasticsearch\Client;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Support\Arr;
8
use Laravel\Scout\Builder;
9
use Laravel\Scout\Engines\Engine;
10
11
class ScoutElasticEngine extends Engine
12
{
13
14
    /**
15
     * The Elasticsearch client.
16
     *
17
     * @var \Elasticsearch\Client
18
     */
19
    private $client;
20
21
    /**
22
     * ElasticSearchScoutEngine constructor.
23
     *
24
     * @param \Elasticsearch\Client $client
25
     */
26 39
    public function __construct(Client $client)
27
    {
28 39
        $this->client = $client;
29 39
    }
30
31
    /**
32
     * Update the given model in the index.
33
     *
34
     * @param  \Illuminate\Database\Eloquent\Collection $models
35
     *
36
     * @return void
37
     */
38 3
    public function update($models)
39
    {
40 3
        $index = $models->first()->searchableAs();
41
42 3
        $params = ['body' => []];
43 3
        foreach ($models as $model) {
44 3
            $array = $model->toSearchableArray();
45
46 3
            if (empty($array)) {
47
                return;
48
            }
49
50 3
            $params['body'][] = [
51
                'index' => [
52 3
                    '_index' => $index,
53 3
                    '_type'  => $index,
54 3
                    '_id'    => $model->getKey(),
55
                ],
56
            ];
57 3
            $params['body'][] = $array;
58
        }
59
60 3
        $this->client->bulk($params);
61 3
    }
62
63
    /**
64
     * Remove the given model from the index.
65
     *
66
     * @param  \Illuminate\Database\Eloquent\Collection $models
67
     *
68
     * @return void
69
     */
70 3
    public function delete($models)
71
    {
72 3
        $index = $models->first()->searchableAs();
73
74 3
        $params = ['body' => []];
75 3
        foreach ($models as $model) {
76 3
            $params['body'][] = [
77
                'delete' => [
78 3
                    '_index' => $index,
79 3
                    '_type'  => $index,
80 3
                    '_id'    => $model->getKey(),
81
                ],
82
            ];
83
        }
84
85 3
        $this->client->bulk($params);
86 3
    }
87
88
    /**
89
     * Perform the given search on the engine.
90
     *
91
     * @param  \Laravel\Scout\Builder $builder
92
     *
93
     * @return mixed
94
     */
95 15
    public function search(Builder $builder)
96
    {
97 15
        $params = $this->prepareParams($builder);
98
99 15
        return $this->client->search($params);
100
    }
101
102
    /**
103
     * Perform the given search on the engine.
104
     *
105
     * @param  \Laravel\Scout\Builder $builder
106
     * @param  int                    $perPage
107
     * @param  int                    $page
108
     *
109
     * @return mixed
110
     */
111 15
    public function paginate(Builder $builder, $perPage, $page)
112
    {
113 15
        $params = $this->prepareParams($builder);
114
115 15
        $params['size'] = $perPage;
116 15
        $params['from'] = ($page - 1) * $perPage;
117 15
        unset($params['body']['size'], $params['body']['from']);
118
119 15
        return $this->client->search($params);
120
    }
121
122
    /**
123
     * Pluck and return the primary keys of the given results.
124
     *
125
     * @param  mixed $results
126
     *
127
     * @return \Illuminate\Support\Collection
128
     */
129
    public function mapIds($results)
130
    {
131
        return collect($results['hits']['hits'])->pluck('_id')->values();
132
    }
133
134
    /**
135
     * Map the given results to instances of the given model.
136
     *
137
     * @param  mixed                               $results
138
     * @param  \Illuminate\Database\Eloquent\Model $model
139
     *
140
     * @return \Illuminate\Database\Eloquent\Collection
141
     */
142 3
    public function map($results, $model)
143
    {
144 3
        if ($results['hits']['total'] === 0) {
145
            return Collection::make();
146
        }
147
148 3
        $keys = collect($results['hits']['hits'])
149 3
            ->pluck('_id')->values()->all();
150
151 3
        $models = $model->whereIn(
152 3
            $model->getQualifiedKeyName(),
153 3
            $keys
154 3
        )->get()->keyBy($model->getKeyName());
155
156 3
        return Collection::make($results['hits']['hits'])->map(function ($hit) use ($model, $models) {
157 3
            $key = $hit['_id'];
158
159 3
            if (isset($models[$key])) {
160 3
                return $models[$key];
161
            }
162 3
        })->filter()->values();
163
    }
164
165
    /**
166
     * Get the total count from a raw result returned by the engine.
167
     *
168
     * @param  mixed $results
169
     *
170
     * @return int
171
     */
172
    public function getTotalCount($results)
173
    {
174
        return $results['hits']['total'];
175
    }
176
177
    /**
178
     * Parse and prepare the params to send to Elasticsearch client
179
     *
180
     * @param \Laravel\Scout\Builder $builder
181
     *
182
     * @return array
183
     */
184 30
    protected function prepareParams(Builder $builder)
185
    {
186
        $params = [
187 30
            'index' => $builder->index ?? $builder->model->searchableAs(),
188
//            'type'  => $builder->index ?? $builder->model->searchableAs(),
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
189 30
            'size'  => $builder->limit ?? $builder->model->getPerPage(),
190
            'body'  => [
191
                'query' => [
192
                    'multi_match' => [
193 30
                        'query'  => $builder->query,
194 30
                        'fields' => '_all',
195
                    ],
196
                ],
197
            ],
198
        ];
199
200 30
        if ($builder->callback) {
201 12
            $params = array_merge($params,
202 12
                call_user_func(
203 12
                    $builder->callback,
204 12
                    $this->client,
205 12
                    $builder->query
206
                ));
207
        }
208
209 30
        return $params;
210
    }
211
}
212