|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of Scout Extended. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Algolia Team <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended\Engines; |
|
15
|
|
|
|
|
16
|
|
|
use Laravel\Scout\Builder; |
|
17
|
|
|
use Algolia\AlgoliaSearch\SearchClient; |
|
18
|
|
|
use Algolia\ScoutExtended\Jobs\DeleteJob; |
|
19
|
|
|
use Algolia\ScoutExtended\Jobs\UpdateJob; |
|
20
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
21
|
|
|
use Algolia\ScoutExtended\Searchable\ModelsResolver; |
|
22
|
|
|
use Laravel\Scout\Engines\AlgoliaEngine as BaseAlgoliaEngine; |
|
23
|
|
|
|
|
24
|
|
|
class AlgoliaEngine extends BaseAlgoliaEngine |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* The Algolia client. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Algolia\AlgoliaSearch\SearchClient |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $algolia; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new engine instance. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $algolia |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
36 |
|
public function __construct(SearchClient $algolia) |
|
40
|
|
|
{ |
|
41
|
36 |
|
$this->algolia = $algolia; |
|
42
|
36 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $algolia |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
31 |
|
public function setClient($algolia): void |
|
50
|
|
|
{ |
|
51
|
31 |
|
$this->algolia = $algolia; |
|
52
|
31 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the client. |
|
56
|
|
|
* |
|
57
|
|
|
* @return \Algolia\AlgoliaSearch\SearchClient $algolia |
|
58
|
|
|
*/ |
|
59
|
35 |
|
public function getClient(): SearchClient |
|
60
|
|
|
{ |
|
61
|
35 |
|
return $this->algolia; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
13 |
|
public function update($searchables) |
|
68
|
|
|
{ |
|
69
|
13 |
|
dispatch_now(new UpdateJob($searchables)); |
|
70
|
13 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function delete($searchables) |
|
76
|
|
|
{ |
|
77
|
5 |
|
dispatch_now(new DeleteJob($searchables)); |
|
78
|
5 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
7 |
|
public function map(Builder $builder, $results, $searchable) |
|
84
|
|
|
{ |
|
85
|
7 |
|
if (count($results['hits']) === 0) { |
|
86
|
5 |
|
return Collection::make(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
$ids = collect($results['hits'])->pluck('objectID')->values()->all(); |
|
90
|
|
|
|
|
91
|
2 |
|
return resolve(ModelsResolver::class)->from($builder, $searchable, $ids); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function flush($model) |
|
98
|
|
|
{ |
|
99
|
2 |
|
$index = $this->algolia->initIndex($model->searchableAs()); |
|
100
|
|
|
|
|
101
|
2 |
|
$index->clearObjects(); |
|
102
|
2 |
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* {@inheritdoc} |
|
106
|
|
|
*/ |
|
107
|
8 |
|
protected function filters(Builder $builder): array |
|
108
|
|
|
{ |
|
109
|
8 |
|
$operators = ['<', '<=', '=', '!=', '>=', '>']; |
|
110
|
|
|
|
|
111
|
|
|
return collect($builder->wheres)->map(function ($value, $key) use ($operators) { |
|
112
|
2 |
|
if (ends_with($key, $operators) || starts_with($value, $operators)) { |
|
113
|
1 |
|
return $key.' '.$value; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
1 |
|
return $key.'='.$value; |
|
117
|
8 |
|
})->values()->all(); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|