|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Index; |
|
4
|
|
|
|
|
5
|
|
|
use Elasticsearch\Client; |
|
6
|
|
|
use Elasticsearch\Common\Exceptions\Missing404Exception; |
|
7
|
|
|
use Nimble\ElasticBundle\Document; |
|
8
|
|
|
use Nimble\ElasticBundle\Exception\TypeNotFoundException; |
|
9
|
|
|
use Nimble\ElasticBundle\SearchResults; |
|
10
|
|
|
use Nimble\ElasticBundle\Type\Type; |
|
11
|
|
|
|
|
12
|
|
|
class Index |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The actual index name used for querying elasticsearch. |
|
16
|
|
|
* |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $name; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Client |
|
23
|
|
|
*/ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
private $settings; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Type[] |
|
33
|
|
|
*/ |
|
34
|
|
|
private $types = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Internal index id. |
|
38
|
|
|
* Used for naming services and internal identification. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $id; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $id Internal index name. |
|
46
|
|
|
* @param string $name |
|
47
|
|
|
* @param Client $client |
|
48
|
|
|
* @param array $settings |
|
49
|
|
|
* @param array $types |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct($id, $name = null, Client $client, array $settings, array $types) |
|
52
|
|
|
{ |
|
53
|
|
|
$this->id = $id; |
|
54
|
|
|
$this->name = null === $name ? $id : $name; |
|
55
|
|
|
$this->client = $client; |
|
56
|
|
|
$this->settings = $settings; |
|
57
|
|
|
|
|
58
|
|
|
$this->buildTypes($types); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $types |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function buildTypes(array $types) |
|
65
|
|
|
{ |
|
66
|
|
|
foreach ($types as $typeName => $typeData) { |
|
67
|
|
|
|
|
68
|
|
|
$mappings = null; |
|
69
|
|
|
|
|
70
|
|
|
if (isset($typeData['mappings'])) { |
|
71
|
|
|
$mappings = ['properties' => $typeData['mappings']]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$this->types[$typeName] = new Type($typeName, $this, $mappings); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return Client |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getClient() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->client; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getName() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->name; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getId() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->id; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return bool |
|
104
|
|
|
*/ |
|
105
|
|
|
public function isAliased() |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->id !== $this->name; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Checks if the index exists in ES. |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function exists() |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->client->indices()->exists(['index' => $this->name]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Deletes the index. |
|
122
|
|
|
*/ |
|
123
|
|
|
public function delete() |
|
124
|
|
|
{ |
|
125
|
|
|
$this->client->indices()->delete(['index' => $this->name]); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Resets the index. |
|
130
|
|
|
*/ |
|
131
|
|
|
public function reset() |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->exists()) { |
|
134
|
|
|
$this->delete(); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$this->create(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Creates the index in ES. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function create() |
|
144
|
|
|
{ |
|
145
|
|
|
$params = [ |
|
146
|
|
|
'index' => $this->name, |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$mappings = $this->getMappings(); |
|
150
|
|
|
|
|
151
|
|
|
if (!empty($mappings)) { |
|
152
|
|
|
$params['body']['mappings'] = $mappings; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
if (!empty($this->settings)) { |
|
156
|
|
|
$params['body']['settings'] = $this->settings; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
$this->client->indices()->create($params); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getMappings() |
|
166
|
|
|
{ |
|
167
|
|
|
$mappings = []; |
|
168
|
|
|
|
|
169
|
|
|
foreach ($this->types as $type) { |
|
170
|
|
|
if (!empty($type->getMappings())) { |
|
171
|
|
|
$mappings[$type->getName()] = $type->getMappings(); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return $mappings; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param string $name |
|
180
|
|
|
* @return bool |
|
181
|
|
|
*/ |
|
182
|
|
|
public function hasType($name) |
|
183
|
|
|
{ |
|
184
|
|
|
return isset($this->types[$name]); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* @param string $name |
|
189
|
|
|
* @return Type |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getType($name) |
|
192
|
|
|
{ |
|
193
|
|
|
if (!$this->hasType($name)) { |
|
194
|
|
|
throw new TypeNotFoundException($name, $this->name); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
return $this->types[$name]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* @return Type[] |
|
202
|
|
|
*/ |
|
203
|
|
|
public function getTypes() |
|
204
|
|
|
{ |
|
205
|
|
|
return $this->types; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @param string $type |
|
210
|
|
|
* @param Document $document |
|
211
|
|
|
*/ |
|
212
|
|
|
public function putDocument($type, Document $document) |
|
213
|
|
|
{ |
|
214
|
|
|
$data = [ |
|
215
|
|
|
'index' => $this->name, |
|
216
|
|
|
'type' => $type, |
|
217
|
|
|
'body' => $document->getData(), |
|
218
|
|
|
]; |
|
219
|
|
|
|
|
220
|
|
|
if (null !== $document->getId()) { |
|
221
|
|
|
$data['id'] = $document->getId(); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
$this->client->index($data); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @param string $type |
|
229
|
|
|
* @param string|int $id |
|
230
|
|
|
* @return bool Returns false if document didn't exist. |
|
231
|
|
|
*/ |
|
232
|
|
|
public function deleteDocument($type, $id) |
|
233
|
|
|
{ |
|
234
|
|
|
try { |
|
235
|
|
|
$this->client->delete([ |
|
236
|
|
|
'index' => $this->name, |
|
237
|
|
|
'type' => $type, |
|
238
|
|
|
'id' => $id, |
|
239
|
|
|
]); |
|
240
|
|
|
} catch (Missing404Exception $exception) { |
|
241
|
|
|
/* Do nothing, just ignore not synched. */ |
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return true; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @param string $type |
|
250
|
|
|
* @param array $documents |
|
251
|
|
|
*/ |
|
252
|
|
|
public function putDocuments($type, array $documents) |
|
253
|
|
|
{ |
|
254
|
|
|
foreach ($documents as $document) { |
|
255
|
|
|
$this->putDocument($type, $document); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* @param string $type |
|
261
|
|
|
* @param array $ids |
|
262
|
|
|
*/ |
|
263
|
|
|
public function deleteDocuments($type, array $ids) |
|
264
|
|
|
{ |
|
265
|
|
|
foreach ($ids as $id) { |
|
266
|
|
|
$this->deleteDocument($type, $id); |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* @param array|string $body |
|
272
|
|
|
* @param array $options |
|
273
|
|
|
* @param string $type |
|
274
|
|
|
* @return array |
|
275
|
|
|
*/ |
|
276
|
|
|
protected function buildParams($body, array $options, $type = null) |
|
277
|
|
|
{ |
|
278
|
|
|
$params = array_merge([ |
|
279
|
|
|
'index' => $this->name, |
|
280
|
|
|
'body' => $body, |
|
281
|
|
|
], $options); |
|
282
|
|
|
|
|
283
|
|
|
if (null !== $type) { |
|
284
|
|
|
$params['type'] = $type; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
return $params; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* @param array|string $query Array that will be serialized or raw JSON. |
|
292
|
|
|
* @param array $options |
|
293
|
|
|
* @param string $type |
|
294
|
|
|
* @return SearchResults |
|
295
|
|
|
*/ |
|
296
|
|
|
public function search($query, array $options = [], $type = null) |
|
297
|
|
|
{ |
|
298
|
|
|
return new SearchResults($this->client->search( |
|
|
|
|
|
|
299
|
|
|
$this->buildParams($query, $options, $type) |
|
300
|
|
|
)); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* @param array|string $query Array that will be serialized or raw JSON. |
|
305
|
|
|
* @param array $options |
|
306
|
|
|
* @param string $type |
|
307
|
|
|
* @return int|null |
|
308
|
|
|
*/ |
|
309
|
|
|
public function count($query, array $options = [], $type = null) |
|
310
|
|
|
{ |
|
311
|
|
|
$data = $this->client->count( |
|
312
|
|
|
$this->buildParams($query, $options, $type) |
|
313
|
|
|
); |
|
314
|
|
|
|
|
315
|
|
|
return isset($data['count']) ? $data['count'] : NULL; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param array|string $query |
|
320
|
|
|
* @param array $options |
|
321
|
|
|
* @param string $type |
|
322
|
|
|
*/ |
|
323
|
|
|
public function deleteByQuery($query, array $options = [], $type) |
|
324
|
|
|
{ |
|
325
|
|
|
$this->client->deleteByQuery( |
|
|
|
|
|
|
326
|
|
|
$this->buildParams($query, $options, $type) |
|
327
|
|
|
); |
|
328
|
|
|
} |
|
329
|
|
|
} |
|
330
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: