1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SearchIndex\SearchIndexHandlers; |
4
|
|
|
|
5
|
|
|
use AlgoliaSearch\Client; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Spatie\SearchIndex\Query\Algolia\SearchQuery; |
9
|
|
|
use Spatie\SearchIndex\Searchable; |
10
|
|
|
use Spatie\SearchIndex\SearchIndexHandler; |
11
|
|
|
use Traversable; |
12
|
|
|
|
13
|
|
|
class Algolia implements SearchIndexHandler |
14
|
|
|
{ |
15
|
|
|
/** @var \AlgoliaSearch\Client */ |
16
|
|
|
protected $algolia; |
17
|
|
|
|
18
|
|
|
/** @var \AlgoliaSearch\Index */ |
19
|
|
|
public $index; |
20
|
|
|
|
21
|
|
|
public function __construct(Client $algolia) |
22
|
|
|
{ |
23
|
|
|
$this->algolia = $algolia; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Set the name of the index that should be used by default. |
28
|
|
|
* |
29
|
|
|
* @param $indexName |
30
|
|
|
* |
31
|
|
|
* @return $this |
32
|
|
|
*/ |
33
|
|
|
public function setIndexName($indexName) |
34
|
|
|
{ |
35
|
|
|
$this->index = $this->algolia->initIndex($indexName); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Add or update the given searchable subject or array of subjects or Traversable object containing subjects. |
42
|
|
|
* |
43
|
|
|
* @param Searchable|array|Traversable $subject |
44
|
|
|
*/ |
45
|
|
|
public function upsertToIndex($subject) |
46
|
|
|
{ |
47
|
|
|
if ($subject instanceof Searchable) { |
48
|
|
|
$subject = [$subject]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (is_array($subject) || $subject instanceof Traversable) { |
52
|
|
|
$searchIndexPayload = collect($subject) |
53
|
|
|
->each(function ($item) { |
54
|
|
|
if (!$item instanceof Searchable) { |
55
|
|
|
throw new InvalidArgumentException(); |
56
|
|
|
} |
57
|
|
|
}) |
58
|
|
|
->map(function ($item) { |
59
|
|
|
return array_merge( |
60
|
|
|
$item->getSearchableBody(), |
61
|
|
|
['objectID' => $this->getAlgoliaId($item)] |
62
|
|
|
); |
63
|
|
|
}) |
64
|
|
|
->toArray(); |
65
|
|
|
|
66
|
|
|
$this->index->saveObjects($searchIndexPayload); |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
throw new InvalidArgumentException('Subject must be a searchable or array of searchables'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Remove the given subject from the search index. |
76
|
|
|
* |
77
|
|
|
* @param Searchable $subject |
78
|
|
|
*/ |
79
|
|
|
public function removeFromIndex(Searchable $subject) |
80
|
|
|
{ |
81
|
|
|
$this->index->deleteObject($this->getAlgoliaId($subject)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Remove an item from the search index by type and id. |
86
|
|
|
* |
87
|
|
|
* @param string $type |
88
|
|
|
* @param int $id |
89
|
|
|
*/ |
90
|
|
|
public function removeFromIndexByTypeAndId($type, $id) |
91
|
|
|
{ |
92
|
|
|
$this->index->deleteObject($type.'-'.$id); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove everything from the index. |
97
|
|
|
* |
98
|
|
|
* @return mixed |
99
|
|
|
*/ |
100
|
|
|
public function clearIndex() |
101
|
|
|
{ |
102
|
|
|
$this->index->clearIndex(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the results for the given query. |
107
|
|
|
* |
108
|
|
|
* @param string|array|\Spatie\SearchIndex\Query\Algolia\SearchQuery $query |
109
|
|
|
* |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
|
|
public function getResults($query) |
113
|
|
|
{ |
114
|
|
|
$parameters = []; |
115
|
|
|
|
116
|
|
|
if (is_object($query) && $query instanceof SearchQuery) { |
117
|
|
|
$query = $query->toArray(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if (is_array($query)) { |
121
|
|
|
$collection = new Collection($query); |
122
|
|
|
|
123
|
|
|
$query = $collection->pull('query', ''); |
124
|
|
|
|
125
|
|
|
$parameters = $collection->toArray(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->index->search($query, $parameters); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the id parameter that is used by Algolia as an array. |
133
|
|
|
* |
134
|
|
|
* @param Searchable $subject |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
protected function getAlgoliaId($subject) |
139
|
|
|
{ |
140
|
|
|
return $subject->getSearchableType().'-'.$subject->getSearchableId(); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Get the underlying client. |
145
|
|
|
* |
146
|
|
|
* @return \AlgoliaSearch\Client |
147
|
|
|
*/ |
148
|
|
|
public function getClient() |
149
|
|
|
{ |
150
|
|
|
return $this->algolia; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|