1
|
|
|
<?php |
2
|
|
|
namespace Elastica; |
3
|
|
|
|
4
|
|
|
use Elastica\Bulk\Action; |
5
|
|
|
use Elastica\Exception\ConnectionException; |
6
|
|
|
use Elastica\Exception\InvalidException; |
7
|
|
|
use Elastica\Script\AbstractScript; |
8
|
|
|
use Elasticsearch\Endpoints\AbstractEndpoint; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Client to connect the the elasticsearch server. |
14
|
|
|
* |
15
|
|
|
* @author Nicolas Ruflin <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Client |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Config with defaults. |
21
|
|
|
* |
22
|
|
|
* log: Set to true, to enable logging, set a string to log to a specific file |
23
|
|
|
* retryOnConflict: Use in \Elastica\Client::updateDocument |
24
|
|
|
* bigintConversion: Set to true to enable the JSON bigint to string conversion option (see issue #717) |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $_config = [ |
29
|
|
|
'host' => null, |
30
|
|
|
'port' => null, |
31
|
|
|
'path' => null, |
32
|
|
|
'url' => null, |
33
|
|
|
'proxy' => null, |
34
|
|
|
'transport' => null, |
35
|
|
|
'persistent' => true, |
36
|
|
|
'timeout' => null, |
37
|
|
|
'connections' => [], // host, port, path, timeout, transport, compression, persistent, timeout, config -> (curl, headers, url) |
38
|
|
|
'roundRobin' => false, |
39
|
|
|
'log' => false, |
40
|
|
|
'retryOnConflict' => 0, |
41
|
|
|
'bigintConversion' => false, |
42
|
|
|
'username' => null, |
43
|
|
|
'password' => null, |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var callback |
48
|
|
|
*/ |
49
|
|
|
protected $_callback; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var Connection\ConnectionPool |
53
|
|
|
*/ |
54
|
|
|
protected $_connectionPool; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var \Elastica\Request|null |
58
|
|
|
*/ |
59
|
|
|
protected $_lastRequest; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var \Elastica\Response|null |
63
|
|
|
*/ |
64
|
|
|
protected $_lastResponse; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var LoggerInterface |
68
|
|
|
*/ |
69
|
|
|
protected $_logger; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
protected $_version; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Creates a new Elastica client. |
78
|
|
|
* |
79
|
|
|
* @param array $config OPTIONAL Additional config options |
80
|
|
|
* @param callback $callback OPTIONAL Callback function which can be used to be notified about errors (for example connection down) |
81
|
|
|
* @param LoggerInterface $logger |
82
|
|
|
*/ |
83
|
|
|
public function __construct(array $config = [], $callback = null, LoggerInterface $logger = null) |
84
|
|
|
{ |
85
|
|
|
$this->_callback = $callback; |
86
|
|
|
|
87
|
|
|
if (!$logger && isset($config['log']) && $config['log']) { |
88
|
|
|
$logger = new Log($config['log']); |
89
|
|
|
} |
90
|
|
|
$this->_logger = $logger ?: new NullLogger(); |
91
|
|
|
|
92
|
|
|
$this->setConfig($config); |
93
|
|
|
$this->_initConnections(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get current version. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getVersion() |
102
|
|
|
{ |
103
|
|
|
if ($this->_version) { |
104
|
|
|
return $this->_version; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$data = $this->request('/')->getData(); |
108
|
|
|
|
109
|
|
|
return $this->_version = $data['version']['number']; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Inits the client connections. |
114
|
|
|
*/ |
115
|
|
|
protected function _initConnections() |
116
|
|
|
{ |
117
|
|
|
$connections = []; |
118
|
|
|
|
119
|
|
|
foreach ($this->getConfig('connections') as $connection) { |
|
|
|
|
120
|
|
|
$connections[] = Connection::create($this->_prepareConnectionParams($connection)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (isset($this->_config['servers'])) { |
124
|
|
|
foreach ($this->getConfig('servers') as $server) { |
|
|
|
|
125
|
|
|
$connections[] = Connection::create($this->_prepareConnectionParams($server)); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// If no connections set, create default connection |
130
|
|
|
if (empty($connections)) { |
131
|
|
|
$connections[] = Connection::create($this->_prepareConnectionParams($this->getConfig())); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (!isset($this->_config['connectionStrategy'])) { |
135
|
|
|
if ($this->getConfig('roundRobin') === true) { |
136
|
|
|
$this->setConfigValue('connectionStrategy', 'RoundRobin'); |
137
|
|
|
} else { |
138
|
|
|
$this->setConfigValue('connectionStrategy', 'Simple'); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$strategy = Connection\Strategy\StrategyFactory::create($this->getConfig('connectionStrategy')); |
143
|
|
|
|
144
|
|
|
$this->_connectionPool = new Connection\ConnectionPool($connections, $strategy, $this->_callback); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Creates a Connection params array from a Client or server config array. |
149
|
|
|
* |
150
|
|
|
* @param array $config |
151
|
|
|
* |
152
|
|
|
* @return array |
153
|
|
|
*/ |
154
|
|
|
protected function _prepareConnectionParams(array $config) |
155
|
|
|
{ |
156
|
|
|
$params = []; |
157
|
|
|
$params['config'] = []; |
158
|
|
|
foreach ($config as $key => $value) { |
159
|
|
|
if (in_array($key, ['bigintConversion', 'curl', 'headers', 'url'])) { |
160
|
|
|
$params['config'][$key] = $value; |
161
|
|
|
} else { |
162
|
|
|
$params[$key] = $value; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $params; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Sets specific config values (updates and keeps default values). |
171
|
|
|
* |
172
|
|
|
* @param array $config Params |
173
|
|
|
* |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function setConfig(array $config) |
177
|
|
|
{ |
178
|
|
|
foreach ($config as $key => $value) { |
179
|
|
|
$this->_config[$key] = $value; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $this; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Returns a specific config key or the whole |
187
|
|
|
* config array if not set. |
188
|
|
|
* |
189
|
|
|
* @param string $key Config key |
190
|
|
|
* |
191
|
|
|
* @throws \Elastica\Exception\InvalidException |
192
|
|
|
* |
193
|
|
|
* @return array|string Config value |
194
|
|
|
*/ |
195
|
|
|
public function getConfig($key = '') |
196
|
|
|
{ |
197
|
|
|
if (empty($key)) { |
198
|
|
|
return $this->_config; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if (!array_key_exists($key, $this->_config)) { |
202
|
|
|
throw new InvalidException('Config key is not set: '.$key); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return $this->_config[$key]; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Sets / overwrites a specific config value. |
210
|
|
|
* |
211
|
|
|
* @param string $key Key to set |
212
|
|
|
* @param mixed $value Value |
213
|
|
|
* |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function setConfigValue($key, $value) |
217
|
|
|
{ |
218
|
|
|
return $this->setConfig([$key => $value]); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param array|string $keys config key or path of config keys |
223
|
|
|
* @param mixed $default default value will be returned if key was not found |
224
|
|
|
* |
225
|
|
|
* @return mixed |
226
|
|
|
*/ |
227
|
|
|
public function getConfigValue($keys, $default = null) |
228
|
|
|
{ |
229
|
|
|
$value = $this->_config; |
230
|
|
|
foreach ((array) $keys as $key) { |
231
|
|
|
if (isset($value[$key])) { |
232
|
|
|
$value = $value[$key]; |
233
|
|
|
} else { |
234
|
|
|
return $default; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $value; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Returns the index for the given connection. |
243
|
|
|
* |
244
|
|
|
* @param string $name Index name to create connection to |
245
|
|
|
* |
246
|
|
|
* @return \Elastica\Index Index for the given name |
247
|
|
|
*/ |
248
|
|
|
public function getIndex($name) |
249
|
|
|
{ |
250
|
|
|
return new Index($this, $name); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Adds a HTTP Header. |
255
|
|
|
* |
256
|
|
|
* @param string $header The HTTP Header |
257
|
|
|
* @param string $headerValue The HTTP Header Value |
258
|
|
|
* |
259
|
|
|
* @throws \Elastica\Exception\InvalidException If $header or $headerValue is not a string |
260
|
|
|
* |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
View Code Duplication |
public function addHeader($header, $headerValue) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if (is_string($header) && is_string($headerValue)) { |
266
|
|
|
$this->_config['headers'][$header] = $headerValue; |
267
|
|
|
} else { |
268
|
|
|
throw new InvalidException('Header must be a string'); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Remove a HTTP Header. |
276
|
|
|
* |
277
|
|
|
* @param string $header The HTTP Header to remove |
278
|
|
|
* |
279
|
|
|
* @throws \Elastica\Exception\InvalidException If $header is not a string |
280
|
|
|
* |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
View Code Duplication |
public function removeHeader($header) |
|
|
|
|
284
|
|
|
{ |
285
|
|
|
if (is_string($header)) { |
286
|
|
|
if (array_key_exists($header, $this->_config['headers'])) { |
287
|
|
|
unset($this->_config['headers'][$header]); |
288
|
|
|
} |
289
|
|
|
} else { |
290
|
|
|
throw new InvalidException('Header must be a string'); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Uses _bulk to send documents to the server. |
298
|
|
|
* |
299
|
|
|
* Array of \Elastica\Document as input. Index and type has to be |
300
|
|
|
* set inside the document, because for bulk settings documents, |
301
|
|
|
* documents can belong to any type and index |
302
|
|
|
* |
303
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
304
|
|
|
* |
305
|
|
|
* @param array|\Elastica\Document[] $docs Array of Elastica\Document |
306
|
|
|
* |
307
|
|
|
* @throws \Elastica\Exception\InvalidException If docs is empty |
308
|
|
|
* |
309
|
|
|
* @return \Elastica\Bulk\ResponseSet Response object |
310
|
|
|
*/ |
311
|
|
View Code Duplication |
public function updateDocuments(array $docs) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
if (empty($docs)) { |
314
|
|
|
throw new InvalidException('Array has to consist of at least one element'); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
$bulk = new Bulk($this); |
318
|
|
|
|
319
|
|
|
$bulk->addDocuments($docs, Action::OP_TYPE_UPDATE); |
320
|
|
|
|
321
|
|
|
return $bulk->send(); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Uses _bulk to send documents to the server. |
326
|
|
|
* |
327
|
|
|
* Array of \Elastica\Document as input. Index and type has to be |
328
|
|
|
* set inside the document, because for bulk settings documents, |
329
|
|
|
* documents can belong to any type and index |
330
|
|
|
* |
331
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
332
|
|
|
* |
333
|
|
|
* @param array|\Elastica\Document[] $docs Array of Elastica\Document |
334
|
|
|
* |
335
|
|
|
* @throws \Elastica\Exception\InvalidException If docs is empty |
336
|
|
|
* |
337
|
|
|
* @return \Elastica\Bulk\ResponseSet Response object |
338
|
|
|
*/ |
339
|
|
View Code Duplication |
public function addDocuments(array $docs) |
|
|
|
|
340
|
|
|
{ |
341
|
|
|
if (empty($docs)) { |
342
|
|
|
throw new InvalidException('Array has to consist of at least one element'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$bulk = new Bulk($this); |
346
|
|
|
|
347
|
|
|
$bulk->addDocuments($docs); |
348
|
|
|
|
349
|
|
|
return $bulk->send(); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Update document, using update script. Requires elasticsearch >= 0.19.0. |
354
|
|
|
* |
355
|
|
|
* @param int|string $id document id |
356
|
|
|
* @param array|\Elastica\Script\AbstractScript|\Elastica\Document $data raw data for request body |
357
|
|
|
* @param string $index index to update |
358
|
|
|
* @param string $type type of index to update |
359
|
|
|
* @param array $options array of query params to use for query. For possible options check es api |
360
|
|
|
* |
361
|
|
|
* @return \Elastica\Response |
362
|
|
|
* |
363
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html |
364
|
|
|
*/ |
365
|
|
|
public function updateDocument($id, $data, $index, $type, array $options = []) |
366
|
|
|
{ |
367
|
|
|
$path = $index.'/'.$type.'/'.$id.'/_update'; |
368
|
|
|
|
369
|
|
|
if ($data instanceof AbstractScript) { |
370
|
|
|
$requestData = $data->toArray(); |
371
|
|
|
} elseif ($data instanceof Document) { |
372
|
|
|
$requestData = ['doc' => $data->getData()]; |
373
|
|
|
|
374
|
|
|
if ($data->getDocAsUpsert()) { |
375
|
|
|
$requestData['doc_as_upsert'] = true; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
$docOptions = $data->getOptions( |
379
|
|
|
[ |
380
|
|
|
'version', |
381
|
|
|
'version_type', |
382
|
|
|
'routing', |
383
|
|
|
'percolate', |
384
|
|
|
'parent', |
385
|
|
|
'fields', |
386
|
|
|
'retry_on_conflict', |
387
|
|
|
'consistency', |
388
|
|
|
'replication', |
389
|
|
|
'refresh', |
390
|
|
|
'timeout', |
391
|
|
|
] |
392
|
|
|
); |
393
|
|
|
$options += $docOptions; |
394
|
|
|
// set fields param to source only if options was not set before |
395
|
|
|
if ($data instanceof Document && ($data->isAutoPopulate() |
396
|
|
|
|| $this->getConfigValue(['document', 'autoPopulate'], false)) |
397
|
|
|
&& !isset($options['fields']) |
398
|
|
|
) { |
399
|
|
|
$options['fields'] = '_source'; |
400
|
|
|
} |
401
|
|
|
} else { |
402
|
|
|
$requestData = $data; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
//If an upsert document exists |
406
|
|
|
if ($data instanceof AbstractScript || $data instanceof Document) { |
407
|
|
|
if ($data->hasUpsert()) { |
408
|
|
|
$requestData['upsert'] = $data->getUpsert()->getData(); |
409
|
|
|
} |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
if (!isset($options['retry_on_conflict'])) { |
413
|
|
|
if ($retryOnConflict = $this->getConfig('retryOnConflict')) { |
414
|
|
|
$options['retry_on_conflict'] = $retryOnConflict; |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
$response = $this->request($path, Request::POST, $requestData, $options); |
419
|
|
|
|
420
|
|
View Code Duplication |
if ($response->isOk() |
|
|
|
|
421
|
|
|
&& $data instanceof Document |
422
|
|
|
&& ($data->isAutoPopulate() || $this->getConfigValue(['document', 'autoPopulate'], false)) |
423
|
|
|
) { |
424
|
|
|
$responseData = $response->getData(); |
425
|
|
|
if (isset($responseData['_version'])) { |
426
|
|
|
$data->setVersion($responseData['_version']); |
427
|
|
|
} |
428
|
|
|
if (isset($options['fields'])) { |
429
|
|
|
$this->_populateDocumentFieldsFromResponse($response, $data, $options['fields']); |
430
|
|
|
} |
431
|
|
|
} |
432
|
|
|
|
433
|
|
|
return $response; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param \Elastica\Response $response |
438
|
|
|
* @param \Elastica\Document $document |
439
|
|
|
* @param string $fields Array of field names to be populated or '_source' if whole document data should be updated |
440
|
|
|
*/ |
441
|
|
|
protected function _populateDocumentFieldsFromResponse(Response $response, Document $document, $fields) |
442
|
|
|
{ |
443
|
|
|
$responseData = $response->getData(); |
444
|
|
|
if ('_source' == $fields) { |
445
|
|
|
if (isset($responseData['get']['_source']) && is_array($responseData['get']['_source'])) { |
446
|
|
|
$document->setData($responseData['get']['_source']); |
447
|
|
|
} |
448
|
|
|
} else { |
449
|
|
|
$keys = explode(',', $fields); |
450
|
|
|
$data = $document->getData(); |
451
|
|
|
foreach ($keys as $key) { |
452
|
|
|
if (isset($responseData['get']['fields'][$key])) { |
453
|
|
|
$data[$key] = $responseData['get']['fields'][$key]; |
454
|
|
|
} elseif (isset($data[$key])) { |
455
|
|
|
unset($data[$key]); |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
$document->setData($data); |
459
|
|
|
} |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* Bulk deletes documents. |
464
|
|
|
* |
465
|
|
|
* @param array|\Elastica\Document[] $docs |
466
|
|
|
* |
467
|
|
|
* @throws \Elastica\Exception\InvalidException |
468
|
|
|
* |
469
|
|
|
* @return \Elastica\Bulk\ResponseSet |
470
|
|
|
*/ |
471
|
|
View Code Duplication |
public function deleteDocuments(array $docs) |
|
|
|
|
472
|
|
|
{ |
473
|
|
|
if (empty($docs)) { |
474
|
|
|
throw new InvalidException('Array has to consist of at least one element'); |
475
|
|
|
} |
476
|
|
|
|
477
|
|
|
$bulk = new Bulk($this); |
478
|
|
|
$bulk->addDocuments($docs, Action::OP_TYPE_DELETE); |
479
|
|
|
|
480
|
|
|
return $bulk->send(); |
481
|
|
|
} |
482
|
|
|
|
483
|
|
|
/** |
484
|
|
|
* Returns the status object for all indices. |
485
|
|
|
* |
486
|
|
|
* @return \Elastica\Status Status object |
487
|
|
|
*/ |
488
|
|
|
public function getStatus() |
489
|
|
|
{ |
490
|
|
|
return new Status($this); |
491
|
|
|
} |
492
|
|
|
|
493
|
|
|
/** |
494
|
|
|
* Returns the current cluster. |
495
|
|
|
* |
496
|
|
|
* @return \Elastica\Cluster Cluster object |
497
|
|
|
*/ |
498
|
|
|
public function getCluster() |
499
|
|
|
{ |
500
|
|
|
return new Cluster($this); |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* Establishes the client connections. |
505
|
|
|
*/ |
506
|
|
|
public function connect() |
507
|
|
|
{ |
508
|
|
|
return $this->_initConnections(); |
509
|
|
|
} |
510
|
|
|
|
511
|
|
|
/** |
512
|
|
|
* @param \Elastica\Connection $connection |
513
|
|
|
* |
514
|
|
|
* @return $this |
515
|
|
|
*/ |
516
|
|
|
public function addConnection(Connection $connection) |
517
|
|
|
{ |
518
|
|
|
$this->_connectionPool->addConnection($connection); |
519
|
|
|
|
520
|
|
|
return $this; |
521
|
|
|
} |
522
|
|
|
|
523
|
|
|
/** |
524
|
|
|
* Determines whether a valid connection is available for use. |
525
|
|
|
* |
526
|
|
|
* @return bool |
527
|
|
|
*/ |
528
|
|
|
public function hasConnection() |
529
|
|
|
{ |
530
|
|
|
return $this->_connectionPool->hasConnection(); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @throws \Elastica\Exception\ClientException |
535
|
|
|
* |
536
|
|
|
* @return \Elastica\Connection |
537
|
|
|
*/ |
538
|
|
|
public function getConnection() |
539
|
|
|
{ |
540
|
|
|
return $this->_connectionPool->getConnection(); |
541
|
|
|
} |
542
|
|
|
|
543
|
|
|
/** |
544
|
|
|
* @return \Elastica\Connection[] |
545
|
|
|
*/ |
546
|
|
|
public function getConnections() |
547
|
|
|
{ |
548
|
|
|
return $this->_connectionPool->getConnections(); |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
/** |
552
|
|
|
* @return \Elastica\Connection\Strategy\StrategyInterface |
553
|
|
|
*/ |
554
|
|
|
public function getConnectionStrategy() |
555
|
|
|
{ |
556
|
|
|
return $this->_connectionPool->getStrategy(); |
557
|
|
|
} |
558
|
|
|
|
559
|
|
|
/** |
560
|
|
|
* @param array|\Elastica\Connection[] $connections |
561
|
|
|
* |
562
|
|
|
* @return $this |
563
|
|
|
*/ |
564
|
|
|
public function setConnections(array $connections) |
565
|
|
|
{ |
566
|
|
|
$this->_connectionPool->setConnections($connections); |
567
|
|
|
|
568
|
|
|
return $this; |
569
|
|
|
} |
570
|
|
|
|
571
|
|
|
/** |
572
|
|
|
* Deletes documents with the given ids, index, type from the index. |
573
|
|
|
* |
574
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
575
|
|
|
* |
576
|
|
|
* @param array $ids Document ids |
577
|
|
|
* @param string|\Elastica\Index $index Index name |
578
|
|
|
* @param string|\Elastica\Type $type Type of documents |
579
|
|
|
* @param string|bool $routing Optional routing key for all ids |
580
|
|
|
* |
581
|
|
|
* @throws \Elastica\Exception\InvalidException |
582
|
|
|
* |
583
|
|
|
* @return \Elastica\Bulk\ResponseSet Response object |
584
|
|
|
*/ |
585
|
|
|
public function deleteIds(array $ids, $index, $type, $routing = false) |
586
|
|
|
{ |
587
|
|
|
if (empty($ids)) { |
588
|
|
|
throw new InvalidException('Array has to consist of at least one id'); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
$bulk = new Bulk($this); |
592
|
|
|
$bulk->setIndex($index); |
593
|
|
|
$bulk->setType($type); |
594
|
|
|
|
595
|
|
|
foreach ($ids as $id) { |
596
|
|
|
$action = new Action(Action::OP_TYPE_DELETE); |
597
|
|
|
$action->setId($id); |
598
|
|
|
|
599
|
|
|
if (!empty($routing)) { |
600
|
|
|
$action->setRouting($routing); |
|
|
|
|
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
$bulk->addAction($action); |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
return $bulk->send(); |
607
|
|
|
} |
608
|
|
|
|
609
|
|
|
/** |
610
|
|
|
* Bulk operation. |
611
|
|
|
* |
612
|
|
|
* Every entry in the params array has to exactly on array |
613
|
|
|
* of the bulk operation. An example param array would be: |
614
|
|
|
* |
615
|
|
|
* array( |
616
|
|
|
* array('index' => array('_index' => 'test', '_type' => 'user', '_id' => '1')), |
617
|
|
|
* array('user' => array('name' => 'hans')), |
618
|
|
|
* array('delete' => array('_index' => 'test', '_type' => 'user', '_id' => '2')) |
619
|
|
|
* ); |
620
|
|
|
* |
621
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html |
622
|
|
|
* |
623
|
|
|
* @param array $params Parameter array |
624
|
|
|
* |
625
|
|
|
* @throws \Elastica\Exception\ResponseException |
626
|
|
|
* @throws \Elastica\Exception\InvalidException |
627
|
|
|
* |
628
|
|
|
* @return \Elastica\Bulk\ResponseSet Response object |
629
|
|
|
*/ |
630
|
|
|
public function bulk(array $params) |
631
|
|
|
{ |
632
|
|
|
if (empty($params)) { |
633
|
|
|
throw new InvalidException('Array has to consist of at least one param'); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
$bulk = new Bulk($this); |
637
|
|
|
|
638
|
|
|
$bulk->addRawData($params); |
639
|
|
|
|
640
|
|
|
return $bulk->send(); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Makes calls to the elasticsearch server based on this index. |
645
|
|
|
* |
646
|
|
|
* It's possible to make any REST query directly over this method |
647
|
|
|
* |
648
|
|
|
* @param string $path Path to call |
649
|
|
|
* @param string $method Rest method to use (GET, POST, DELETE, PUT) |
650
|
|
|
* @param array|string $data OPTIONAL Arguments as array or pre-encoded string |
651
|
|
|
* @param array $query OPTIONAL Query params |
652
|
|
|
* |
653
|
|
|
* @throws Exception\ConnectionException|\Exception |
654
|
|
|
* |
655
|
|
|
* @return Response Response object |
656
|
|
|
*/ |
657
|
|
|
public function request($path, $method = Request::GET, $data = [], array $query = []) |
658
|
|
|
{ |
659
|
|
|
$connection = $this->getConnection(); |
660
|
|
|
$request = $this->_lastRequest = new Request($path, $method, $data, $query, $connection); |
|
|
|
|
661
|
|
|
$this->_lastResponse = null; |
662
|
|
|
|
663
|
|
|
try { |
664
|
|
|
$response = $this->_lastResponse = $request->send(); |
665
|
|
|
} catch (ConnectionException $e) { |
666
|
|
|
$this->_connectionPool->onFail($connection, $e, $this); |
667
|
|
|
|
668
|
|
|
$this->_log($e); |
|
|
|
|
669
|
|
|
|
670
|
|
|
// In case there is no valid connection left, throw exception which caused the disabling of the connection. |
671
|
|
|
if (!$this->hasConnection()) { |
672
|
|
|
throw $e; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
return $this->request($path, $method, $data, $query); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
$this->_log($request); |
|
|
|
|
679
|
|
|
|
680
|
|
|
return $response; |
681
|
|
|
} |
682
|
|
|
|
683
|
|
|
/** |
684
|
|
|
* Makes calls to the elasticsearch server with usage official client Endpoint |
685
|
|
|
* |
686
|
|
|
* @param AbstractEndpoint $endpoint |
687
|
|
|
* @return Response |
688
|
|
|
*/ |
689
|
|
|
public function requestEndpoint(AbstractEndpoint $endpoint) |
690
|
|
|
{ |
691
|
|
|
return $this->request( |
692
|
|
|
ltrim($endpoint->getURI(), '/'), |
693
|
|
|
$endpoint->getMethod(), |
694
|
|
|
null === $endpoint->getBody() ? [] : $endpoint->getBody(), |
695
|
|
|
$endpoint->getParams() |
696
|
|
|
); |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* logging. |
701
|
|
|
* |
702
|
|
|
* @deprecated Overwriting Client->_log is deprecated. Handle logging functionality by using a custom LoggerInterface. |
703
|
|
|
* |
704
|
|
|
* @param mixed $context |
705
|
|
|
*/ |
706
|
|
|
protected function _log($context) |
707
|
|
|
{ |
708
|
|
|
if ($context instanceof ConnectionException) { |
709
|
|
|
$this->_logger->error('Elastica Request Failure', [ |
710
|
|
|
'exception' => $context, |
711
|
|
|
'request' => $context->getRequest()->toArray(), |
712
|
|
|
'retry' => $this->hasConnection(), |
713
|
|
|
]); |
714
|
|
|
|
715
|
|
|
return; |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
if ($context instanceof Request) { |
719
|
|
|
$this->_logger->debug('Elastica Request', [ |
720
|
|
|
'request' => $context->toArray(), |
721
|
|
|
'response' => $this->_lastResponse ? $this->_lastResponse->getData() : null, |
722
|
|
|
'responseStatus' => $this->_lastResponse ? $this->_lastResponse->getStatus() : null, |
723
|
|
|
]); |
724
|
|
|
|
725
|
|
|
return; |
726
|
|
|
} |
727
|
|
|
|
728
|
|
|
$this->_logger->debug('Elastica Request', [ |
729
|
|
|
'message' => $context, |
730
|
|
|
]); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Optimizes all search indices. |
735
|
|
|
* |
736
|
|
|
* @param array $args OPTIONAL Optional arguments |
737
|
|
|
* |
738
|
|
|
* @return \Elastica\Response Response object |
739
|
|
|
* |
740
|
|
|
* @deprecated Replaced by forcemergeAll |
741
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-optimize.html |
742
|
|
|
*/ |
743
|
|
|
public function optimizeAll($args = []) |
744
|
|
|
{ |
745
|
|
|
trigger_error('Deprecated: Elastica\Client::optimizeAll() is deprecated and will be removed in further Elastica releases. Use Elastica\Client::forcemergeAll() instead.', E_USER_DEPRECATED); |
746
|
|
|
|
747
|
|
|
return $this->forcemergeAll($args); |
748
|
|
|
} |
749
|
|
|
|
750
|
|
|
/** |
751
|
|
|
* Force merges all search indices. |
752
|
|
|
* |
753
|
|
|
* @param array $args OPTIONAL Optional arguments |
754
|
|
|
* |
755
|
|
|
* @return \Elastica\Response Response object |
756
|
|
|
* |
757
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html |
758
|
|
|
*/ |
759
|
|
|
public function forcemergeAll($args = []) |
760
|
|
|
{ |
761
|
|
|
return $this->request('_forcemerge', Request::POST, [], $args); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* Refreshes all search indices. |
766
|
|
|
* |
767
|
|
|
* @return \Elastica\Response Response object |
768
|
|
|
* |
769
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html |
770
|
|
|
*/ |
771
|
|
|
public function refreshAll() |
772
|
|
|
{ |
773
|
|
|
return $this->request('_refresh', Request::POST); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* @return Request|null |
778
|
|
|
*/ |
779
|
|
|
public function getLastRequest() |
780
|
|
|
{ |
781
|
|
|
return $this->_lastRequest; |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
/** |
785
|
|
|
* @return Response|null |
786
|
|
|
*/ |
787
|
|
|
public function getLastResponse() |
788
|
|
|
{ |
789
|
|
|
return $this->_lastResponse; |
790
|
|
|
} |
791
|
|
|
|
792
|
|
|
/** |
793
|
|
|
* Replace the existing logger. |
794
|
|
|
* |
795
|
|
|
* @param LoggerInterface $logger |
796
|
|
|
* |
797
|
|
|
* @return $this |
798
|
|
|
*/ |
799
|
|
|
public function setLogger(LoggerInterface $logger) |
800
|
|
|
{ |
801
|
|
|
$this->_logger = $logger; |
802
|
|
|
|
803
|
|
|
return $this; |
804
|
|
|
} |
805
|
|
|
} |
806
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.