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