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 sequence number of a document for use with optimistic concurrency control. |
69
|
|
|
* |
70
|
|
|
* @param int $number Sequence Number |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
* |
74
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html |
75
|
|
|
*/ |
76
|
|
|
public function setSequenceNumber($number) |
77
|
|
|
{ |
78
|
|
|
return $this->setParam('if_seq_no', $number); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Returns document version. |
83
|
|
|
* |
84
|
|
|
* @return int|string Document version |
85
|
|
|
*/ |
86
|
|
|
public function getSequenceNumber() |
87
|
|
|
{ |
88
|
|
|
return $this->getParam('if_seq_no'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function hasSequenceNumber() |
95
|
|
|
{ |
96
|
|
|
return $this->hasParam('if_seq_no'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the prmary term of a document for use with optimistic concurrency control. |
101
|
|
|
* |
102
|
|
|
* @param int $term Primary Term |
103
|
|
|
* |
104
|
|
|
* @return $this |
105
|
|
|
* |
106
|
|
|
* @see https://www.elastic.co/guide/en/elasticsearch/reference/6.8/optimistic-concurrency-control.html |
107
|
|
|
*/ |
108
|
|
|
public function setPrimaryTerm($term) |
109
|
|
|
{ |
110
|
|
|
return $this->setParam('if_primary_term', $term); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns document version. |
115
|
|
|
* |
116
|
|
|
* @return int|string Document version |
117
|
|
|
*/ |
118
|
|
|
public function getPrimaryTerm() |
119
|
|
|
{ |
120
|
|
|
return $this->getParam('if_primary_term'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function hasPrimaryTerm() |
127
|
|
|
{ |
128
|
|
|
return $this->hasParam('if_primary_term'); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @deprecated |
133
|
|
|
* Sets the version of a document for use with optimistic concurrency control. |
134
|
|
|
* |
135
|
|
|
* @param int $version Document version |
136
|
|
|
* |
137
|
|
|
* @return $this |
138
|
|
|
* |
139
|
|
|
* @see https://www.elastic.co/blog/versioning |
140
|
|
|
*/ |
141
|
|
|
public function setVersion($version) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
\trigger_error('Elastica\AbstractUpdateAction::setVersion is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @deprecated |
150
|
|
|
* Returns document version. |
151
|
|
|
* |
152
|
|
|
* @return int|string Document version |
153
|
|
|
*/ |
154
|
|
|
public function getVersion() |
155
|
|
|
{ |
156
|
|
|
return $this->getParam('version'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @deprecated |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function hasVersion() |
164
|
|
|
{ |
165
|
|
|
return $this->hasParam('version'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @deprecated |
170
|
|
|
* Sets the version_type of a document |
171
|
|
|
* Default in ES is internal, but you can set to external to use custom versioning. |
172
|
|
|
* |
173
|
|
|
* @param string $versionType Document version type |
174
|
|
|
* |
175
|
|
|
* @return $this |
176
|
|
|
*/ |
177
|
|
|
public function setVersionType($versionType) |
|
|
|
|
178
|
|
|
{ |
179
|
|
|
\trigger_error('Elastica\AbstractUpdateAction::setVersionType is deprecated. Use Elastica\AbstractUpdateAction::setSequenceNumber and Elastica\AbstractUpdateAction::setPrimaryTerm instead.', E_USER_DEPRECATED); |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @deprecated |
186
|
|
|
* Returns document version type. |
187
|
|
|
* |
188
|
|
|
* @return int|string Document version type |
189
|
|
|
*/ |
190
|
|
|
public function getVersionType() |
191
|
|
|
{ |
192
|
|
|
return $this->getParam('version_type'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @deprecated |
197
|
|
|
* @return bool |
198
|
|
|
*/ |
199
|
|
|
public function hasVersionType() |
200
|
|
|
{ |
201
|
|
|
return $this->hasParam('version_type'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set operation type. |
206
|
|
|
* |
207
|
|
|
* @param string $opType Only accept create |
208
|
|
|
* |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
|
|
public function setOpType($opType) |
212
|
|
|
{ |
213
|
|
|
return $this->setParam('op_type', $opType); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Get operation type. |
218
|
|
|
* |
219
|
|
|
* @return string |
220
|
|
|
*/ |
221
|
|
|
public function getOpType() |
222
|
|
|
{ |
223
|
|
|
return $this->getParam('op_type'); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function hasOpType() |
230
|
|
|
{ |
231
|
|
|
return $this->hasParam('op_type'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Set routing query param. |
236
|
|
|
* |
237
|
|
|
* @param string $value routing |
238
|
|
|
* |
239
|
|
|
* @return $this |
240
|
|
|
*/ |
241
|
|
|
public function setRouting($value) |
242
|
|
|
{ |
243
|
|
|
return $this->setParam('routing', $value); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Get routing parameter. |
248
|
|
|
* |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
public function getRouting() |
252
|
|
|
{ |
253
|
|
|
return $this->getParam('_routing'); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return bool |
258
|
|
|
*/ |
259
|
|
|
public function hasRouting() |
260
|
|
|
{ |
261
|
|
|
return $this->hasParam('_routing'); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param array|string $fields |
266
|
|
|
* |
267
|
|
|
* @return $this |
268
|
|
|
*/ |
269
|
|
|
public function setFields($fields) |
270
|
|
|
{ |
271
|
|
|
if (\is_array($fields)) { |
272
|
|
|
$fields = \implode(',', $fields); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return $this->setParam('fields', (string) $fields); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return $this |
280
|
|
|
*/ |
281
|
|
|
public function setFieldsSource() |
282
|
|
|
{ |
283
|
|
|
return $this->setFields('_source'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* @return string |
288
|
|
|
*/ |
289
|
|
|
public function getFields() |
290
|
|
|
{ |
291
|
|
|
return $this->getParam('fields'); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @return bool |
296
|
|
|
*/ |
297
|
|
|
public function hasFields() |
298
|
|
|
{ |
299
|
|
|
return $this->hasParam('fields'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param int $num |
304
|
|
|
* |
305
|
|
|
* @return $this |
306
|
|
|
*/ |
307
|
|
|
public function setRetryOnConflict($num) |
308
|
|
|
{ |
309
|
|
|
return $this->setParam('retry_on_conflict', (int) $num); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @return int |
314
|
|
|
*/ |
315
|
|
|
public function getRetryOnConflict() |
316
|
|
|
{ |
317
|
|
|
return $this->getParam('retry_on_conflict'); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
|
|
public function hasRetryOnConflict() |
324
|
|
|
{ |
325
|
|
|
return $this->hasParam('_retry_on_conflict'); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param bool|string $refresh |
330
|
|
|
* |
331
|
|
|
* @return $this |
332
|
|
|
*/ |
333
|
|
|
public function setRefresh($refresh = true) |
334
|
|
|
{ |
335
|
|
|
\is_bool($refresh) && $refresh = $refresh |
336
|
|
|
? Reindex::REFRESH_TRUE |
337
|
|
|
: Reindex::REFRESH_FALSE; |
338
|
|
|
|
339
|
|
|
return $this->setParam(Reindex::REFRESH, $refresh); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @return bool|string |
344
|
|
|
*/ |
345
|
|
|
public function getRefresh() |
346
|
|
|
{ |
347
|
|
|
$refresh = $this->getParam('refresh'); |
348
|
|
|
|
349
|
|
|
return \in_array($refresh, [Reindex::REFRESH_TRUE, Reindex::REFRESH_FALSE]) |
350
|
|
|
? Reindex::REFRESH_TRUE === $refresh |
351
|
|
|
: $refresh; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* @return bool |
356
|
|
|
*/ |
357
|
|
|
public function hasRefresh() |
358
|
|
|
{ |
359
|
|
|
return $this->hasParam('refresh'); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* @param string $timeout |
364
|
|
|
* |
365
|
|
|
* @return $this |
366
|
|
|
*/ |
367
|
|
|
public function setTimeout($timeout) |
368
|
|
|
{ |
369
|
|
|
return $this->setParam('timeout', $timeout); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* @return bool |
374
|
|
|
*/ |
375
|
|
|
public function getTimeout() |
376
|
|
|
{ |
377
|
|
|
return $this->getParam('timeout'); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* @return bool |
382
|
|
|
*/ |
383
|
|
|
public function hasTimeout() |
384
|
|
|
{ |
385
|
|
|
return $this->hasParam('timeout'); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param string $timeout |
390
|
|
|
* |
391
|
|
|
* @return $this |
392
|
|
|
*/ |
393
|
|
|
public function setConsistency($timeout) |
394
|
|
|
{ |
395
|
|
|
return $this->setParam('consistency', $timeout); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @return string |
400
|
|
|
*/ |
401
|
|
|
public function getConsistency() |
402
|
|
|
{ |
403
|
|
|
return $this->getParam('consistency'); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @return bool |
408
|
|
|
*/ |
409
|
|
|
public function hasConsistency() |
410
|
|
|
{ |
411
|
|
|
return $this->hasParam('consistency'); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* @param string $timeout |
416
|
|
|
* |
417
|
|
|
* @return $this |
418
|
|
|
*/ |
419
|
|
|
public function setReplication($timeout) |
420
|
|
|
{ |
421
|
|
|
return $this->setParam('replication', $timeout); |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* @return string |
426
|
|
|
*/ |
427
|
|
|
public function getReplication() |
428
|
|
|
{ |
429
|
|
|
return $this->getParam('replication'); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* @return bool |
434
|
|
|
*/ |
435
|
|
|
public function hasReplication() |
436
|
|
|
{ |
437
|
|
|
return $this->hasParam('replication'); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @param array|Document $data |
442
|
|
|
* |
443
|
|
|
* @return $this |
444
|
|
|
*/ |
445
|
|
|
public function setUpsert($data) |
446
|
|
|
{ |
447
|
|
|
$document = Document::create($data); |
448
|
|
|
$this->_upsert = $document; |
|
|
|
|
449
|
|
|
|
450
|
|
|
return $this; |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* @return Document |
455
|
|
|
*/ |
456
|
|
|
public function getUpsert() |
457
|
|
|
{ |
458
|
|
|
return $this->_upsert; |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* @return bool |
463
|
|
|
*/ |
464
|
|
|
public function hasUpsert() |
465
|
|
|
{ |
466
|
|
|
return null !== $this->_upsert; |
467
|
|
|
} |
468
|
|
|
|
469
|
|
|
/** |
470
|
|
|
* @param array $fields if empty array all options will be returned |
471
|
|
|
* |
472
|
|
|
* @return array |
473
|
|
|
*/ |
474
|
|
|
public function getOptions(array $fields = []) |
475
|
|
|
{ |
476
|
|
|
if (!empty($fields)) { |
477
|
|
|
return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields))); |
478
|
|
|
} |
479
|
|
|
|
480
|
|
|
return \array_filter($this->getParams()); |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.