1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elastica; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base class for things that can be sent to the update api (Document and |
7
|
|
|
* Script). |
8
|
|
|
* |
9
|
|
|
* @author Nik Everett <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class AbstractUpdateAction extends Param |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Document |
15
|
|
|
*/ |
16
|
|
|
protected $_upsert; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Sets the id of the document. |
20
|
|
|
*/ |
21
|
|
|
public function setId(?string $id = null): self |
22
|
|
|
{ |
23
|
|
|
return $this->setParam('_id', $id); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns document id. |
28
|
|
|
* |
29
|
|
|
* @return string|null Document id |
30
|
|
|
*/ |
31
|
|
|
public function getId(): ?string |
32
|
|
|
{ |
33
|
|
|
return $this->hasParam('_id') ? $this->getParam('_id') : null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function hasId(): bool |
37
|
|
|
{ |
38
|
|
|
return null !== $this->getId(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Sets the document index name. |
43
|
|
|
* |
44
|
|
|
* @param Index|string $index Index name |
45
|
|
|
*/ |
46
|
|
|
public function setIndex($index): self |
47
|
|
|
{ |
48
|
|
|
if ($index instanceof Index) { |
49
|
|
|
$index = $index->getName(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->setParam('_index', $index); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the document index name. |
57
|
|
|
* |
58
|
|
|
* @throws \Elastica\Exception\InvalidException |
59
|
|
|
* |
60
|
|
|
* @return string Index name |
61
|
|
|
*/ |
62
|
|
|
public function getIndex() |
63
|
|
|
{ |
64
|
|
|
return $this->getParam('_index'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Sets the version parameters of a document for use with optimistic concurrency control. |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setVersionParams(array $responseData): self |
73
|
|
|
{ |
74
|
|
|
if (isset($responseData['_version'])) { |
75
|
|
|
$this->setVersion($responseData['_version']); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (isset($data['_seq_no'])) { |
|
|
|
|
79
|
|
|
$this->setSequenceNumber($responseData['_seq_no']); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (isset($data['_primary_term'])) { |
83
|
|
|
$this->setPrimaryTerm($responseData['_primary_term']); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the sequence number of a document for use with optimistic concurrency control. |
91
|
|
|
* |
92
|
|
|
* @param int $number Sequence Number |
93
|
|
|
* |
94
|
|
|
* @return $this |
95
|
|
|
* |
96
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html |
97
|
|
|
*/ |
98
|
|
|
public function setSequenceNumber(int $number): self |
99
|
|
|
{ |
100
|
|
|
return $this->setParam('if_seq_no', $number); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns document version. |
105
|
|
|
* |
106
|
|
|
* @return int Document version |
107
|
|
|
*/ |
108
|
|
|
public function getSequenceNumber(): int |
109
|
|
|
{ |
110
|
|
|
return $this->getParam('if_seq_no'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function hasSequenceNumber(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->hasParam('if_seq_no'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Sets the primary term of a document for use with optimistic concurrency control. |
120
|
|
|
* |
121
|
|
|
* @param int $term Primary Term |
122
|
|
|
* |
123
|
|
|
* @return $this |
124
|
|
|
* |
125
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html |
126
|
|
|
*/ |
127
|
|
|
public function setPrimaryTerm(int $term): self |
128
|
|
|
{ |
129
|
|
|
return $this->setParam('if_primary_term', $term); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns document version. |
134
|
|
|
* |
135
|
|
|
* @return int Document version |
136
|
|
|
*/ |
137
|
|
|
public function getPrimaryTerm(): int |
138
|
|
|
{ |
139
|
|
|
return $this->getParam('if_primary_term'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function hasPrimaryTerm(): bool |
143
|
|
|
{ |
144
|
|
|
return $this->hasParam('if_primary_term'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Sets the version of a document. |
149
|
|
|
* |
150
|
|
|
* @param int $version Document version |
151
|
|
|
* |
152
|
|
|
* @return $this |
153
|
|
|
* |
154
|
|
|
* @see https://www.elastic.co/blog/versioning |
155
|
|
|
*/ |
156
|
|
|
public function setVersion($version) |
157
|
|
|
{ |
158
|
|
|
return $this->setParam('version', (int) $version); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns document version. |
163
|
|
|
* |
164
|
|
|
* @return int|string Document version |
165
|
|
|
*/ |
166
|
|
|
public function getVersion() |
167
|
|
|
{ |
168
|
|
|
return $this->getParam('version'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return bool |
173
|
|
|
*/ |
174
|
|
|
public function hasVersion() |
175
|
|
|
{ |
176
|
|
|
return $this->hasParam('version'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Set operation type. |
181
|
|
|
* |
182
|
|
|
* @param string $opType Only accept create |
183
|
|
|
* |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function setOpType($opType) |
187
|
|
|
{ |
188
|
|
|
return $this->setParam('op_type', $opType); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get operation type. |
193
|
|
|
* |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getOpType() |
197
|
|
|
{ |
198
|
|
|
return $this->getParam('op_type'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @return bool |
203
|
|
|
*/ |
204
|
|
|
public function hasOpType() |
205
|
|
|
{ |
206
|
|
|
return $this->hasParam('op_type'); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Set routing query param. |
211
|
|
|
* |
212
|
|
|
* @param string $value routing |
213
|
|
|
* |
214
|
|
|
* @return $this |
215
|
|
|
*/ |
216
|
|
|
public function setRouting($value) |
217
|
|
|
{ |
218
|
|
|
return $this->setParam('routing', $value); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get routing parameter. |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getRouting() |
227
|
|
|
{ |
228
|
|
|
return $this->getParam('_routing'); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function hasRouting() |
235
|
|
|
{ |
236
|
|
|
return $this->hasParam('_routing'); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param array|string $fields |
241
|
|
|
* |
242
|
|
|
* @return $this |
243
|
|
|
*/ |
244
|
|
|
public function setFields($fields) |
245
|
|
|
{ |
246
|
|
|
if (\is_array($fields)) { |
247
|
|
|
$fields = \implode(',', $fields); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $this->setParam('fields', (string) $fields); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return $this |
255
|
|
|
*/ |
256
|
|
|
public function setFieldsSource() |
257
|
|
|
{ |
258
|
|
|
return $this->setFields('_source'); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return string |
263
|
|
|
*/ |
264
|
|
|
public function getFields() |
265
|
|
|
{ |
266
|
|
|
return $this->getParam('fields'); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
|
|
public function hasFields() |
273
|
|
|
{ |
274
|
|
|
return $this->hasParam('fields'); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* @param int $num |
279
|
|
|
* |
280
|
|
|
* @return $this |
281
|
|
|
*/ |
282
|
|
|
public function setRetryOnConflict($num) |
283
|
|
|
{ |
284
|
|
|
return $this->setParam('retry_on_conflict', (int) $num); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return int |
289
|
|
|
*/ |
290
|
|
|
public function getRetryOnConflict() |
291
|
|
|
{ |
292
|
|
|
return $this->getParam('retry_on_conflict'); |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function hasRetryOnConflict() |
299
|
|
|
{ |
300
|
|
|
return $this->hasParam('_retry_on_conflict'); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param bool|string $refresh |
305
|
|
|
* |
306
|
|
|
* @return $this |
307
|
|
|
*/ |
308
|
|
|
public function setRefresh($refresh = true) |
309
|
|
|
{ |
310
|
|
|
\is_bool($refresh) && $refresh = $refresh |
311
|
|
|
? Reindex::REFRESH_TRUE |
312
|
|
|
: Reindex::REFRESH_FALSE; |
313
|
|
|
|
314
|
|
|
return $this->setParam(Reindex::REFRESH, $refresh); |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return bool|string |
319
|
|
|
*/ |
320
|
|
|
public function getRefresh() |
321
|
|
|
{ |
322
|
|
|
$refresh = $this->getParam('refresh'); |
323
|
|
|
|
324
|
|
|
return \in_array($refresh, [Reindex::REFRESH_TRUE, Reindex::REFRESH_FALSE]) |
325
|
|
|
? Reindex::REFRESH_TRUE === $refresh |
326
|
|
|
: $refresh; |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @return bool |
331
|
|
|
*/ |
332
|
|
|
public function hasRefresh() |
333
|
|
|
{ |
334
|
|
|
return $this->hasParam('refresh'); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** |
338
|
|
|
* @param string $timeout |
339
|
|
|
* |
340
|
|
|
* @return $this |
341
|
|
|
*/ |
342
|
|
|
public function setTimeout($timeout) |
343
|
|
|
{ |
344
|
|
|
return $this->setParam('timeout', $timeout); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return bool |
349
|
|
|
*/ |
350
|
|
|
public function getTimeout() |
351
|
|
|
{ |
352
|
|
|
return $this->getParam('timeout'); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @return bool |
357
|
|
|
*/ |
358
|
|
|
public function hasTimeout() |
359
|
|
|
{ |
360
|
|
|
return $this->hasParam('timeout'); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param string $timeout |
365
|
|
|
* |
366
|
|
|
* @return $this |
367
|
|
|
*/ |
368
|
|
|
public function setConsistency($timeout) |
369
|
|
|
{ |
370
|
|
|
return $this->setParam('consistency', $timeout); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* @return string |
375
|
|
|
*/ |
376
|
|
|
public function getConsistency() |
377
|
|
|
{ |
378
|
|
|
return $this->getParam('consistency'); |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @return bool |
383
|
|
|
*/ |
384
|
|
|
public function hasConsistency() |
385
|
|
|
{ |
386
|
|
|
return $this->hasParam('consistency'); |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* @param string $timeout |
391
|
|
|
* |
392
|
|
|
* @return $this |
393
|
|
|
*/ |
394
|
|
|
public function setReplication($timeout) |
395
|
|
|
{ |
396
|
|
|
return $this->setParam('replication', $timeout); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @return string |
401
|
|
|
*/ |
402
|
|
|
public function getReplication() |
403
|
|
|
{ |
404
|
|
|
return $this->getParam('replication'); |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @return bool |
409
|
|
|
*/ |
410
|
|
|
public function hasReplication() |
411
|
|
|
{ |
412
|
|
|
return $this->hasParam('replication'); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param array|Document $data |
417
|
|
|
* |
418
|
|
|
* @return $this |
419
|
|
|
*/ |
420
|
|
|
public function setUpsert($data) |
421
|
|
|
{ |
422
|
|
|
$document = Document::create($data); |
423
|
|
|
$this->_upsert = $document; |
|
|
|
|
424
|
|
|
|
425
|
|
|
return $this; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* @return Document |
430
|
|
|
*/ |
431
|
|
|
public function getUpsert() |
432
|
|
|
{ |
433
|
|
|
return $this->_upsert; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @return bool |
438
|
|
|
*/ |
439
|
|
|
public function hasUpsert() |
440
|
|
|
{ |
441
|
|
|
return null !== $this->_upsert; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* @param array $fields if empty array all options will be returned |
446
|
|
|
* |
447
|
|
|
* @return array |
448
|
|
|
*/ |
449
|
|
|
public function getOptions(array $fields = []) |
450
|
|
|
{ |
451
|
|
|
if (!empty($fields)) { |
452
|
|
|
return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields))); |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
return \array_filter($this->getParams()); |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|
This check looks for calls to
isset(...)
orempty()
on variables that are yet undefined. These calls will always produce the same result and can be removed.This is most likely caused by the renaming of a variable or the removal of a function/method parameter.