Completed
Pull Request — master (#259)
by
unknown
01:13
created

TNTSearchEngine::initIndex()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace TeamTNT\Scout\Engines;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\SoftDeletes;
7
use Illuminate\Support\Facades\DB;
8
use Laravel\Scout\Builder;
9
use Laravel\Scout\Engines\Engine;
10
use TeamTNT\TNTSearch\Exceptions\IndexNotFoundException;
11
use TeamTNT\TNTSearch\TNTGeoSearch;
12
use TeamTNT\TNTSearch\TNTSearch;
13
14
class TNTSearchEngine extends Engine
15
{
16
    /**
17
     * @var TNTSearch
18
     */
19
    protected $tnt;
20
21
    /**
22
     * @var TNTGeoSearch
23
     */
24
    protected $geotnt;
25
26
    /**
27
     * @var Builder
28
     */
29
    protected $builder;
30
31
    /**
32
     * Create a new engine instance.
33
     *
34
     * @param TNTSearch $tnt
35
     * @param TNTGeoSearch $geotnt
36
     */
37
    public function __construct(TNTSearch $tnt, TNTGeoSearch $geotnt)
38
    {
39
        $this->tnt = $tnt;
40
        $this->geotnt = $geotnt;
41
    }
42
43
    /**
44
     * Update the given model in the index.
45
     *
46
     * @param Collection $models
47
     *
48
     * @return void
49
     */
50
    public function update($models)
51
    {
52
        $this->initIndex($models->first());
53
        $this->tnt->selectIndex("{$models->first()->searchableAs()}.index");
54
        $index = $this->tnt->getIndex();
55
        $index->setPrimaryKey($models->first()->getKeyName());
56
57
        $this->geotnt->selectIndex("{$models->first()->searchableAs()}.geoindex");
58
        $geoindex = $this->geotnt->getIndex();
59
        $geoindex->setPrimaryKey($models->first()->getKeyName());
60
61
        $index->indexBeginTransaction();
62
        $geoindex->indexBeginTransaction();
63
        $models->each(function ($model) use ($index, $geoindex) {
64
            $array = $model->toSearchableArray();
65
66
            if (empty($array)) {
67
                return;
68
            }
69
70
            if ($model->getKey()) {
71
                $index->update($model->getKey(), $array);
72
                $geoindex->prepareAndExecuteStatement('DELETE FROM locations WHERE doc_id = :documentId;',[
73
                    ['key' => ':documentId', 'value' => $model->getKey()]
74
                ]);
75
            } else {
76
                $index->insert($array);
77
            }
78
79
            if (empty($array['longitude']) || empty($array['latitude'])) {
80
                return;
81
            }
82
83
            $array['longitude'] = (float) $array['longitude'];
84
            $array['latitude'] = (float) $array['latitude'];
85
86
            $geoindex->insert($array);
87
        });
88
        $index->indexEndTransaction();
89
        $geoindex->indexEndTransaction();
90
    }
91
92
    /**
93
     * Remove the given model from the index.
94
     *
95
     * @param Collection $models
96
     *
97
     * @return void
98
     */
99
    public function delete($models)
100
    {
101
        $this->initIndex($models->first());
102
        $models->each(function ($model) {
103
            $this->tnt->selectIndex("{$model->searchableAs()}.index");
104
            $index = $this->tnt->getIndex();
105
            $index->setPrimaryKey($model->getKeyName());
106
            $index->delete($model->getKey());
107
108
            $this->geotnt->selectIndex("{$model->searchableAs()}.geoindex");
109
            $geoindex = $this->geotnt->getIndex();
110
            $geoindex->prepareAndExecuteStatement('DELETE FROM locations WHERE doc_id = :documentId;',[
111
                ['key' => ':documentId', 'value' => $model->getKey()]
112
            ]);
113
        });
114
    }
115
116
    /**
117
     * Perform the given search on the engine.
118
     *
119
     * @param Builder $builder
120
     *
121
     * @return mixed
122
     */
123
    public function search(Builder $builder)
124
    {
125
        try {
126
            return $this->performSearch($builder);
127
        } catch (IndexNotFoundException $e) {
128
            $this->initIndex($builder->model);
129
        }
130
    }
131
132
    /**
133
     * Perform the given search on the engine.
134
     *
135
     * @param Builder $builder
136
     * @param int     $perPage
137
     * @param int     $page
138
     *
139
     * @return mixed
140
     */
141
    public function paginate(Builder $builder, $perPage, $page)
142
    {
143
        $results = $this->performSearch($builder);
144
145
        if ($builder->limit) {
146
            $results['hits'] = $builder->limit;
147
        }
148
149
        $filtered = $this->discardIdsFromResultSetByConstraints($builder, $results['ids']);
150
151
        $results['hits'] = $filtered->count();
152
153
        $chunks = array_chunk($filtered->toArray(), $perPage);
154
155
        if (empty($chunks)) {
156
            return $results;
157
        }
158
159
        if (array_key_exists($page - 1, $chunks)) {
160
            $results['ids'] = $chunks[$page - 1];
161
        } else {
162
            $results['ids'] = [];
163
        }
164
165
        return $results;
166
    }
167
168
    /**
169
     * Perform the given search on the engine.
170
     *
171
     * @param Builder $builder
172
     *
173
     * @return mixed
174
     */
175
    protected function performSearch(Builder $builder, array $options = [])
176
    {
177
        $index = $builder->index ?: $builder->model->searchableAs();
178
        $limit = $builder->limit ?: 10000;
179
        $this->tnt->selectIndex("{$index}.index");
180
        $this->geotnt->selectIndex("{$index}.geoindex");
181
182
        $this->builder = $builder;
183
184
        if (isset($builder->model->asYouType)) {
185
            $this->tnt->asYouType = $builder->model->asYouType;
186
        }
187
188
        if ($builder->callback) {
189
            return call_user_func(
190
                $builder->callback,
191
                $this->tnt,
192
                $builder->query,
193
                $options
194
            );
195
        }
196
197
        if (is_array($builder->query)) {
198
            $location = $builder->query['location'];
199
            $distance = $builder->query['distance'];
200
            $limit = array_key_exists('limit', $builder->query) ? $builder->query['limit'] : 10;
201
            return $this->geotnt->findNearest($location, $distance, $limit);
202
        }
203
204
        if (isset($this->tnt->config['searchBoolean']) ? $this->tnt->config['searchBoolean'] : false) {
205
            return $this->tnt->searchBoolean($builder->query, $limit);
206
        } else {
207
            return $this->tnt->search($builder->query, $limit);
208
        }
209
    }
210
211
    /**
212
     * Map the given results to instances of the given model.
213
     *
214
     * @param mixed                               $results
215
     * @param \Illuminate\Database\Eloquent\Model $model
216
     *
217
     * @return Collection
218
     */
219
    public function map(Builder $builder, $results, $model)
220
    {
221
        if (is_null($results['ids']) || count($results['ids']) === 0) {
222
            return Collection::make();
223
        }
224
225
        $keys = collect($results['ids'])->values()->all();
226
227
        $builder = $this->getBuilder($model);
228
229
        if ($this->builder->queryCallback) {
230
            call_user_func($this->builder->queryCallback, $builder);
231
        }
232
233
        $models = $builder->whereIn(
234
            $model->getQualifiedKeyName(), $keys
235
        )->get()->keyBy($model->getKeyName());
236
237
        // sort models by user choice
238
        if (!empty($this->builder->orders)) {
239
            return $models->values();
240
        }
241
242
        // sort models by tnt search result set
243
        return collect($results['ids'])->map(function ($hit) use ($models) {
244
            if (isset($models[$hit])) {
245
                return $models[$hit];
246
            }
247
        })->filter()->values();
248
    }
249
250
    /**
251
     * Return query builder either from given constraints, or as
252
     * new query. Add where statements to builder when given.
253
     *
254
     * @param \Illuminate\Database\Eloquent\Model $model
255
     *
256
     * @return Builder
257
     */
258
    public function getBuilder($model)
259
    {
260
        // get query as given constraint or create a new query
261
        $builder = isset($this->builder->constraints) ? $this->builder->constraints : $model->newQuery();
0 ignored issues
show
Bug introduced by
The property constraints does not seem to exist in Laravel\Scout\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
262
263
        $builder = $this->handleSoftDeletes($builder, $model);
264
265
        $builder = $this->applyWheres($builder);
266
267
        $builder = $this->applyOrders($builder);
268
269
        return $builder;
270
    }
271
272
    /**
273
     * Pluck and return the primary keys of the given results.
274
     *
275
     * @param mixed $results
276
     * @return \Illuminate\Support\Collection
277
     */
278
    public function mapIds($results)
279
    {
280
        return collect($results['ids'])->values();
281
    }
282
283
    /**
284
     * Get the total count from a raw result returned by the engine.
285
     *
286
     * @param mixed $results
287
     *
288
     * @return int
289
     */
290
    public function getTotalCount($results)
291
    {
292
        return $results['hits'];
293
    }
294
295
    public function initIndex($model)
296
    {
297
        $indexName = $model->searchableAs();
298
299
        if (!file_exists($this->tnt->config['storage']."/{$indexName}.index")) {
300
            $indexer = $this->tnt->createIndex("$indexName.index");
301
            $indexer->setDatabaseHandle($model->getConnection()->getPdo());
302
            $indexer->setPrimaryKey($model->getKeyName());
303
        }
304
305
        if (!file_exists($this->geotnt->config['storage']."/{$indexName}.geoindex")) {
306
            $indexer = $this->geotnt->getIndex();
307
            $indexer->loadConfig($this->geotnt->config);
308
            $indexer->createIndex("$indexName.geoindex");
309
            $indexer->setDatabaseHandle($model->getConnection()->getPdo());
310
            $indexer->setPrimaryKey($model->getKeyName());
311
        }
312
    }
313
314
    /**
315
     * The search index results ($results['ids']) need to be compared against our query
316
     * that contains the constraints.
317
     *
318
     * To get the correct results and counts for the pagination, we remove those ids
319
     * from the search index results that were found by the search but are not part of
320
     * the query ($sub) that is constrained.
321
     *
322
     * This is achieved with self joining the constrained query as subquery and selecting
323
     * the ids which are not matching to anything (i.e., is null).
324
     *
325
     * The constraints usually remove only a small amount of results, which is why the non
326
     * matching results are looked up and removed, instead of returning a collection with
327
     * all the valid results.
328
     */
329
    private function discardIdsFromResultSetByConstraints($builder, $searchResults)
330
    {
331
        $qualifiedKeyName    = $builder->model->getQualifiedKeyName(); // tableName.id
332
        $subQualifiedKeyName = 'sub.'.$builder->model->getKeyName(); // sub.id
333
334
        $sub = $this->getBuilder($builder->model)->whereIn(
335
            $qualifiedKeyName, $searchResults
336
        ); // sub query for left join
337
338
        $discardIds = $builder->model->newQuery()
339
            ->select($qualifiedKeyName)
340
            ->leftJoin(DB::raw('('.$sub->getQuery()->toSql().') as '. $builder->model->getConnection()->getTablePrefix() .'sub'), $subQualifiedKeyName, '=', $qualifiedKeyName)
341
            ->addBinding($sub->getQuery()->getBindings(), 'join')
342
            ->whereIn($qualifiedKeyName, $searchResults)
343
            ->whereNull($subQualifiedKeyName)
344
            ->pluck($builder->model->getKeyName());
345
346
        // returns values of $results['ids'] that are not part of $discardIds
347
        return collect($searchResults)->diff($discardIds);
348
    }
349
350
    /**
351
     * Determine if the given model uses soft deletes.
352
     *
353
     * @param  \Illuminate\Database\Eloquent\Model  $model
354
     * @return bool
355
     */
356
    protected function usesSoftDelete($model)
357
    {
358
        return in_array(SoftDeletes::class, class_uses_recursive($model));
359
    }
360
361
    /**
362
     * Determine if soft delete is active and depending on state return the
363
     * appropriate builder.
364
     *
365
     * @param  Builder  $builder
366
     * @param  \Illuminate\Database\Eloquent\Model  $model
367
     * @return Builder
368
     */
369
    private function handleSoftDeletes($builder, $model)
370
    {
371
        // remove where statement for __soft_deleted when soft delete is not active
372
        // does not show soft deleted items when trait is attached to model and
373
        // config('scout.soft_delete') is false
374
        if (!$this->usesSoftDelete($model) || !config('scout.soft_delete', true)) {
375
            unset($this->builder->wheres['__soft_deleted']);
376
            return $builder;
377
        }
378
379
        /**
380
         * Use standard behaviour of Laravel Scout builder class to support soft deletes.
381
         *
382
         * When no __soft_deleted statement is given return all entries
383
         */
384
        if (!in_array('__soft_deleted', $this->builder->wheres)) {
385
            return $builder->withTrashed();
386
        }
387
388
        /**
389
         * When __soft_deleted is 1 then return only soft deleted entries
390
         */
391
        if ($this->builder->wheres['__soft_deleted']) {
392
            $builder = $builder->onlyTrashed();
393
        }
394
395
        /**
396
         * Returns all undeleted entries, default behaviour
397
         */
398
        unset($this->builder->wheres['__soft_deleted']);
399
        return $builder;
400
    }
401
402
    /**
403
     * Apply where statements as constraints to the query builder.
404
     *
405
     * @param Builder $builder
406
     * @return \Illuminate\Support\Collection
407
     */
408
    private function applyWheres($builder)
409
    {
410
        // iterate over given where clauses
411
        return collect($this->builder->wheres)->map(function ($value, $key) {
412
            // for reduce function combine key and value into array
413
            return [$key, $value];
414
        })->reduce(function ($builder, $where) {
415
            // separate key, value again
416
            list($key, $value) = $where;
417
            return $builder->where($key, $value);
418
        }, $builder);
419
    }
420
421
    /**
422
     * Apply order by statements as constraints to the query builder.
423
     *
424
     * @param Builder $builder
425
     * @return \Illuminate\Support\Collection
426
     */
427
    private function applyOrders($builder)
428
    {
429
        //iterate over given orderBy clauses - should be only one
430
        return collect($this->builder->orders)->map(function ($value, $key) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
431
            // for reduce function combine key and value into array
432
            return [$value["column"], $value["direction"]];
433
        })->reduce(function ($builder, $orderBy) {
434
            // separate key, value again
435
            list($column, $direction) = $orderBy;
436
            return $builder->orderBy($column, $direction);
437
        }, $builder);
438
    }
439
440
    /**
441
     * Flush all of the model's records from the engine.
442
     *
443
     * @param  \Illuminate\Database\Eloquent\Model  $model
444
     * @return void
445
     */
446
    public function flush($model)
447
    {
448
        $indexName   = $model->searchableAs();
449
        $pathToIndex = $this->tnt->config['storage']."/{$indexName}.index";
450
        if (file_exists($pathToIndex)) {
451
            unlink($pathToIndex);
452
        }
453
454
        $pathToGeoIndex = $this->geotnt->config['storage']."/{$indexName}.geoindex";
455
        if (file_exists($pathToGeoIndex)) {
456
            unlink($pathToGeoIndex);
457
        }
458
    }
459
}
460