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