1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SearchIndex\SearchIndexHandlers; |
4
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Spatie\SearchIndex\Searchable; |
8
|
|
|
use Spatie\SearchIndex\SearchIndexHandler; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
class Elasticsearch implements SearchIndexHandler |
12
|
|
|
{ |
13
|
|
|
/** @var Elasticsearch */ |
14
|
|
|
protected $elasticsearch; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $indexName; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Client $elasticsearch |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Client $elasticsearch) |
23
|
|
|
{ |
24
|
|
|
$this->elasticsearch = $elasticsearch; |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Set the name of the index that should be used by default. |
29
|
|
|
* |
30
|
|
|
* @param $indexName |
31
|
|
|
* |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function setIndexName($indexName) |
35
|
|
|
{ |
36
|
|
|
$this->indexName = $indexName; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Add or update the given searchable subject or array of subjects or Traversable object containing subjects. |
43
|
|
|
* |
44
|
|
|
* @param Searchable|array|Traversable $subject |
45
|
|
|
*/ |
46
|
|
|
public function upsertToIndex($subject, $indexName = null) |
47
|
|
|
{ |
48
|
|
|
$indexName = $this->resolveIndexName($indexName); |
49
|
|
|
|
50
|
|
|
if ($subject instanceof Searchable) { |
51
|
|
|
$subject = [$subject]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (is_array($subject) || $subject instanceof Traversable) { |
55
|
|
|
$searchableItems = collect($subject) |
56
|
|
|
->each(function ($item) { |
57
|
|
|
if (!$item instanceof Searchable) { |
58
|
|
|
throw new InvalidArgumentException(); |
59
|
|
|
} |
60
|
|
|
}) |
61
|
|
|
->flatMap(function ($item) use($indexName) { |
62
|
|
|
return |
63
|
|
|
[ |
64
|
|
|
[ |
65
|
|
|
'index' => [ |
66
|
|
|
'_id' => $item->getSearchableId(), |
67
|
|
|
'_index' => $indexName, |
68
|
|
|
'_type' => $item->getSearchableType(), |
69
|
|
|
], |
70
|
|
|
], |
71
|
|
|
|
72
|
|
|
$item->getSearchableBody(), |
73
|
|
|
]; |
74
|
|
|
}) |
75
|
|
|
->toArray(); |
76
|
|
|
|
77
|
|
|
$payload['body'] = $searchableItems; |
|
|
|
|
78
|
|
|
|
79
|
|
|
$this->elasticsearch->bulk($payload); |
|
|
|
|
80
|
|
|
|
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new InvalidArgumentException('Subject must be a searchable or array of searchables'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Remove the given subject from the search index. |
89
|
|
|
* |
90
|
|
|
* @param Searchable $subject |
91
|
|
|
*/ |
92
|
|
|
public function removeFromIndex(Searchable $subject, $indexName = null) |
93
|
|
|
{ |
94
|
|
|
$indexName = $this->resolveIndexName($indexName); |
95
|
|
|
|
96
|
|
|
$this->elasticsearch->delete( |
|
|
|
|
97
|
|
|
[ |
98
|
|
|
'index' => $indexName, |
99
|
|
|
'type' => $subject->getSearchableType(), |
100
|
|
|
'id' => $subject->getSearchableId(), |
101
|
|
|
] |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Remove an item from the search index by type and id. |
107
|
|
|
* |
108
|
|
|
* @param string $type |
109
|
|
|
* @param int $id |
110
|
|
|
*/ |
111
|
|
|
public function removeFromIndexByTypeAndId($type, $id, $indexName = null) |
112
|
|
|
{ |
113
|
|
|
$indexName = $this->resolveIndexName($indexName); |
114
|
|
|
|
115
|
|
|
$this->elasticsearch->delete( |
|
|
|
|
116
|
|
|
[ |
117
|
|
|
'index' => $indexName, |
118
|
|
|
'type' => $type, |
119
|
|
|
'id' => $id, |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Remove everything from the index. |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function clearIndex($indexName = null) |
130
|
|
|
{ |
131
|
|
|
$indexName = $this->resolveIndexName($indexName); |
132
|
|
|
|
133
|
|
|
$this->elasticsearch->indices()->delete(['index' => $indexName]); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the results for the given query. |
138
|
|
|
* |
139
|
|
|
* @param array $query |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function getResults($query) |
144
|
|
|
{ |
145
|
|
|
return $this->elasticsearch->search($query); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get the underlying client. |
150
|
|
|
* |
151
|
|
|
* @return Elasticsearch |
152
|
|
|
*/ |
153
|
|
|
public function getClient() |
154
|
|
|
{ |
155
|
|
|
return $this->elasticsearch; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function resolveIndexName($indexName = null) |
159
|
|
|
{ |
160
|
|
|
if (!$indexName) { |
161
|
|
|
$indexName = $this->indexName; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $indexName; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..