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 function is_array; |
17
|
|
|
use Laravel\Scout\Builder; |
18
|
|
|
use Illuminate\Support\Str; |
19
|
|
|
use Illuminate\Support\Arr; |
20
|
|
|
use Algolia\AlgoliaSearch\SearchClient; |
21
|
|
|
use Algolia\ScoutExtended\Jobs\DeleteJob; |
22
|
|
|
use Algolia\ScoutExtended\Jobs\UpdateJob; |
23
|
|
|
use Algolia\ScoutExtended\Searchable\ModelsResolver; |
24
|
|
|
use Algolia\ScoutExtended\Searchable\ObjectIdEncrypter; |
25
|
|
|
use Laravel\Scout\Engines\AlgoliaEngine as BaseAlgoliaEngine; |
26
|
|
|
|
27
|
|
|
class AlgoliaEngine extends BaseAlgoliaEngine |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* The Algolia client. |
31
|
|
|
* |
32
|
|
|
* @var \Algolia\AlgoliaSearch\SearchClient |
33
|
|
|
*/ |
34
|
|
|
protected $algolia; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Create a new engine instance. |
38
|
|
|
* |
39
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $algolia |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
50 |
|
public function __construct(SearchClient $algolia) |
43
|
|
|
{ |
44
|
50 |
|
$this->algolia = $algolia; |
45
|
50 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param \Algolia\AlgoliaSearch\SearchClient $algolia |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
*/ |
52
|
44 |
|
public function setClient($algolia): void |
53
|
|
|
{ |
54
|
44 |
|
$this->algolia = $algolia; |
55
|
44 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get the client. |
59
|
|
|
* |
60
|
|
|
* @return \Algolia\AlgoliaSearch\SearchClient $algolia |
61
|
|
|
*/ |
62
|
49 |
|
public function getClient(): SearchClient |
63
|
|
|
{ |
64
|
49 |
|
return $this->algolia; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
15 |
|
public function update($searchables) |
71
|
|
|
{ |
72
|
15 |
|
dispatch_now(new UpdateJob($searchables)); |
73
|
15 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
6 |
|
public function delete($searchables) |
79
|
|
|
{ |
80
|
6 |
|
dispatch_now(new DeleteJob($searchables)); |
81
|
6 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
15 |
|
public function map(Builder $builder, $results, $searchable) |
87
|
|
|
{ |
88
|
15 |
|
if (count($results['hits']) === 0) { |
89
|
11 |
|
return $searchable->newCollection(); |
90
|
|
|
} |
91
|
|
|
|
92
|
4 |
|
$hits = collect($results['hits'])->keyBy('objectID'); |
93
|
|
|
|
94
|
4 |
|
$models = resolve(ModelsResolver::class)->from($builder, $searchable, $hits->keys()->all()); |
95
|
|
|
|
96
|
|
|
return $models->map(function ($model) use ($hits) { |
97
|
3 |
|
if ($hit = $hits->get(ObjectIdEncrypter::encrypt($model))) { |
98
|
2 |
|
foreach (Arr::only($hit, ['_highlightResult', '_rankingInfo']) as $key => $value) { |
99
|
|
|
$model->setAttribute($key, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return $model; |
104
|
3 |
|
}); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* {@inheritdoc} |
109
|
|
|
*/ |
110
|
2 |
|
public function flush($model) |
111
|
|
|
{ |
112
|
2 |
|
$index = $this->algolia->initIndex($model->searchableAs()); |
113
|
|
|
|
114
|
2 |
|
$index->clearObjects(); |
115
|
2 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
16 |
|
protected function filters(Builder $builder): array |
121
|
|
|
{ |
122
|
16 |
|
$operators = ['<', '<=', '=', '!=', '>=', '>', ':']; |
123
|
|
|
|
124
|
|
|
return collect($builder->wheres)->map(function ($value, $key) use ($operators) { |
125
|
8 |
|
if (! is_array($value)) { |
126
|
7 |
|
if (Str::endsWith($key, $operators) || Str::startsWith($value, $operators)) { |
127
|
5 |
|
return $key.' '.$value; |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return $key.'='.$value; |
131
|
|
|
} |
132
|
|
|
|
133
|
2 |
|
return $value; |
134
|
16 |
|
})->values()->all(); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|