-
Notifications
You must be signed in to change notification settings - Fork 342
/
Copy pathAlgoliaEngine.php
219 lines (192 loc) · 5.53 KB
/
AlgoliaEngine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace Laravel\Scout\Engines;
use Laravel\Scout\Builder;
use AlgoliaSearch\Client as Algolia;
use Illuminate\Database\Eloquent\SoftDeletes;
class AlgoliaEngine extends Engine
{
/**
* The Algolia client.
*
* @var \AlgoliaSearch\Client
*/
protected $algolia;
/**
* Create a new engine instance.
*
* @param \AlgoliaSearch\Client $algolia
* @return void
*/
public function __construct(Algolia $algolia)
{
$this->algolia = $algolia;
}
/**
* Update the given model in the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @throws \AlgoliaSearch\AlgoliaException
* @return void
*/
public function update($models)
{
if ($models->isEmpty()) {
return;
}
$index = $this->algolia->initIndex($models->first()->searchableAs());
if ($this->usesSoftDelete($models->first()) && config('scout.soft_delete', false)) {
$models->each->pushSoftDeleteMetadata();
}
$objects = $models->map(function ($model) {
$array = array_merge(
$model->toSearchableArray(), $model->scoutMetadata()
);
if (empty($array)) {
return;
}
return array_merge(['objectID' => $model->getScoutKey()], $array);
})->filter()->values()->all();
if (! empty($objects)) {
$index->addObjects($objects);
}
}
/**
* Remove the given model from the index.
*
* @param \Illuminate\Database\Eloquent\Collection $models
* @return void
*/
public function delete($models)
{
$index = $this->algolia->initIndex($models->first()->searchableAs());
$index->deleteObjects(
$models->map(function ($model) {
return $model->getScoutKey();
})->values()->all()
);
}
/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @return mixed
*/
public function search(Builder $builder)
{
return $this->performSearch($builder, array_filter([
'numericFilters' => $this->filters($builder),
'hitsPerPage' => $builder->limit,
]));
}
/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param int $perPage
* @param int $page
* @return mixed
*/
public function paginate(Builder $builder, $perPage, $page)
{
return $this->performSearch($builder, [
'numericFilters' => $this->filters($builder),
'hitsPerPage' => $perPage,
'page' => $page - 1,
]);
}
/**
* Perform the given search on the engine.
*
* @param \Laravel\Scout\Builder $builder
* @param array $options
* @return mixed
*/
protected function performSearch(Builder $builder, array $options = [])
{
$algolia = $this->algolia->initIndex(
$builder->index ?: $builder->model->searchableAs()
);
if ($builder->callback) {
return call_user_func(
$builder->callback,
$algolia,
$builder->query,
$options
);
}
return $algolia->search($builder->query, $options);
}
/**
* Get the filter array for the query.
*
* @param \Laravel\Scout\Builder $builder
* @return array
*/
protected function filters(Builder $builder)
{
return collect($builder->wheres)->map(function ($value, $key) {
return $key.'='.$value;
})->values()->all();
}
/**
* Pluck and return the primary keys of the given results.
*
* @param mixed $results
* @return \Illuminate\Support\Collection
*/
public function mapIds($results)
{
return collect($results['hits'])->pluck('objectID')->values();
}
/**
* Map the given results to instances of the given model.
*
* @param \Laravel\Scout\Builder $builder
* @param mixed $results
* @param \Illuminate\Database\Eloquent\Model $model
* @return \Illuminate\Database\Eloquent\Collection
*/
public function map(Builder $builder, $results, $model)
{
if (count($results['hits']) === 0) {
return $model->newCollection();
}
$objectIds = collect($results['hits'])->pluck('objectID')->values()->all();
return $model->getScoutModelsByIds(
$builder, $objectIds
)->filter(function ($model) use ($objectIds) {
return in_array($model->getScoutKey(), $objectIds);
});
}
/**
* Get the total count from a raw result returned by the engine.
*
* @param mixed $results
* @return int
*/
public function getTotalCount($results)
{
return $results['nbHits'];
}
/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function flush($model)
{
$index = $this->algolia->initIndex($model->searchableAs());
$index->clearIndex();
}
/**
* Determine if the given model uses soft deletes.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
protected function usesSoftDelete($model)
{
return in_array(SoftDeletes::class, class_uses_recursive($model));
}
}