Skip to content

Commit 28b9555

Browse files
committed
Properly handle underscores in options
Since 6.1.0 all metadata except _index, _type and _id are accepted without the underscore. In 6.5 it produces a deprecation warning and in 7 they are no longer accepted.
1 parent d0433d6 commit 28b9555

File tree

9 files changed

+75
-91
lines changed

9 files changed

+75
-91
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ All notable changes to this project will be documented in this file based on the
44
## [Unreleased](https://github.com/ruflin/Elastica/compare/6.1.1...master)
55

66
### Backward Compatibility Breaks
7+
* \Elastica\AbstractUpdateAction::getOptions( $fields ) no longer supports the $underscore parameter, option names must match what elasticsearch expects.
78

89
### Bugfixes
910
* Always set the Guzzle `base_uri` to support connecting to multiple ES hosts. [#1618](https://github.com/ruflin/Elastica/pull/1618)
11+
* Properly handle underscore prefixes in options and bulk request metadata ([cf upstream](https://github.com/elastic/elasticsearch/issues/26886). [#1621](https://github.com/ruflin/Elastica/pull/1621)
1012

1113
### Added
1214

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ All library issues should go to the [issue tracker from github](https://github.c
1717

1818
Compatibility
1919
-------------
20-
This release is compatible with all Elasticsearch 6.x releases. It was tested with version 6.6.1.
20+
This release is compatible with all Elasticsearch 6.1 releases and onwards. It was tested with version 6.6.1.
2121

2222

2323
Contributing
@@ -36,4 +36,4 @@ This project tries to follow Elasticsearch in terms of [End of Life](https://www
3636
| [5.x](https://github.com/ruflin/Elastica/tree/5.x) | 5.x | ^5.0 | \>=5.6 |
3737
| [3.2.3](https://github.com/ruflin/Elastica/tree/3.2.3) (unmaintained) | 2.4.0 | no | \>=5.4 |
3838
| [2.x](https://github.com/ruflin/Elastica/tree/2.x) (unmaintained) | 1.7.2 | no | \>=5.3.3 |
39-
------------
39+
------------

lib/Elastica/AbstractUpdateAction.php

+37-52
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function getIndex()
113113
*/
114114
public function setVersion($version)
115115
{
116-
return $this->setParam('_version', (int) $version);
116+
return $this->setParam('version', (int) $version);
117117
}
118118

119119
/**
@@ -123,15 +123,15 @@ public function setVersion($version)
123123
*/
124124
public function getVersion()
125125
{
126-
return $this->getParam('_version');
126+
return $this->getParam('version');
127127
}
128128

129129
/**
130130
* @return bool
131131
*/
132132
public function hasVersion()
133133
{
134-
return $this->hasParam('_version');
134+
return $this->hasParam('version');
135135
}
136136

137137
/**
@@ -144,7 +144,7 @@ public function hasVersion()
144144
*/
145145
public function setVersionType($versionType)
146146
{
147-
return $this->setParam('_version_type', $versionType);
147+
return $this->setParam('version_type', $versionType);
148148
}
149149

150150
/**
@@ -154,15 +154,15 @@ public function setVersionType($versionType)
154154
*/
155155
public function getVersionType()
156156
{
157-
return $this->getParam('_version_type');
157+
return $this->getParam('version_type');
158158
}
159159

160160
/**
161161
* @return bool
162162
*/
163163
public function hasVersionType()
164164
{
165-
return $this->hasParam('_version_type');
165+
return $this->hasParam('version_type');
166166
}
167167

168168
/**
@@ -176,7 +176,7 @@ public function hasVersionType()
176176
*/
177177
public function setParent($parent)
178178
{
179-
return $this->setParam('_parent', $parent);
179+
return $this->setParam('parent', $parent);
180180
}
181181

182182
/**
@@ -186,15 +186,15 @@ public function setParent($parent)
186186
*/
187187
public function getParent()
188188
{
189-
return $this->getParam('_parent');
189+
return $this->getParam('parent');
190190
}
191191

192192
/**
193193
* @return bool
194194
*/
195195
public function hasParent()
196196
{
197-
return $this->hasParam('_parent');
197+
return $this->hasParam('parent');
198198
}
199199

200200
/**
@@ -206,7 +206,7 @@ public function hasParent()
206206
*/
207207
public function setOpType($opType)
208208
{
209-
return $this->setParam('_op_type', $opType);
209+
return $this->setParam('op_type', $opType);
210210
}
211211

212212
/**
@@ -216,15 +216,15 @@ public function setOpType($opType)
216216
*/
217217
public function getOpType()
218218
{
219-
return $this->getParam('_op_type');
219+
return $this->getParam('op_type');
220220
}
221221

222222
/**
223223
* @return bool
224224
*/
225225
public function hasOpType()
226226
{
227-
return $this->hasParam('_op_type');
227+
return $this->hasParam('op_type');
228228
}
229229

230230
/**
@@ -236,7 +236,7 @@ public function hasOpType()
236236
*/
237237
public function setRouting($value)
238238
{
239-
return $this->setParam('_routing', $value);
239+
return $this->setParam('routing', $value);
240240
}
241241

242242
/**
@@ -246,15 +246,15 @@ public function setRouting($value)
246246
*/
247247
public function getRouting()
248248
{
249-
return $this->getParam('_routing');
249+
return $this->getParam('routing');
250250
}
251251

252252
/**
253253
* @return bool
254254
*/
255255
public function hasRouting()
256256
{
257-
return $this->hasParam('_routing');
257+
return $this->hasParam('routing');
258258
}
259259

260260
/**
@@ -268,7 +268,7 @@ public function setFields($fields)
268268
$fields = \implode(',', $fields);
269269
}
270270

271-
return $this->setParam('_fields', (string) $fields);
271+
return $this->setParam('fields', (string) $fields);
272272
}
273273

274274
/**
@@ -284,15 +284,15 @@ public function setFieldsSource()
284284
*/
285285
public function getFields()
286286
{
287-
return $this->getParam('_fields');
287+
return $this->getParam('fields');
288288
}
289289

290290
/**
291291
* @return bool
292292
*/
293293
public function hasFields()
294294
{
295-
return $this->hasParam('_fields');
295+
return $this->hasParam('fields');
296296
}
297297

298298
/**
@@ -302,23 +302,23 @@ public function hasFields()
302302
*/
303303
public function setRetryOnConflict($num)
304304
{
305-
return $this->setParam('_retry_on_conflict', (int) $num);
305+
return $this->setParam('retry_on_conflict', (int) $num);
306306
}
307307

308308
/**
309309
* @return int
310310
*/
311311
public function getRetryOnConflict()
312312
{
313-
return $this->getParam('_retry_on_conflict');
313+
return $this->getParam('retry_on_conflict');
314314
}
315315

316316
/**
317317
* @return bool
318318
*/
319319
public function hasRetryOnConflict()
320320
{
321-
return $this->hasParam('_retry_on_conflict');
321+
return $this->hasParam('retry_on_conflict');
322322
}
323323

324324
/**
@@ -328,23 +328,23 @@ public function hasRetryOnConflict()
328328
*/
329329
public function setRefresh($refresh = true)
330330
{
331-
return $this->setParam('_refresh', (bool) $refresh ? 'true' : 'false');
331+
return $this->setParam('refresh', (bool) $refresh ? 'true' : 'false');
332332
}
333333

334334
/**
335335
* @return bool
336336
*/
337337
public function getRefresh()
338338
{
339-
return 'true' === $this->getParam('_refresh');
339+
return 'true' === $this->getParam('refresh');
340340
}
341341

342342
/**
343343
* @return bool
344344
*/
345345
public function hasRefresh()
346346
{
347-
return $this->hasParam('_refresh');
347+
return $this->hasParam('refresh');
348348
}
349349

350350
/**
@@ -354,23 +354,23 @@ public function hasRefresh()
354354
*/
355355
public function setTimeout($timeout)
356356
{
357-
return $this->setParam('_timeout', $timeout);
357+
return $this->setParam('timeout', $timeout);
358358
}
359359

360360
/**
361361
* @return bool
362362
*/
363363
public function getTimeout()
364364
{
365-
return $this->getParam('_timeout');
365+
return $this->getParam('timeout');
366366
}
367367

368368
/**
369369
* @return bool
370370
*/
371371
public function hasTimeout()
372372
{
373-
return $this->hasParam('_timeout');
373+
return $this->hasParam('timeout');
374374
}
375375

376376
/**
@@ -380,23 +380,23 @@ public function hasTimeout()
380380
*/
381381
public function setConsistency($timeout)
382382
{
383-
return $this->setParam('_consistency', $timeout);
383+
return $this->setParam('consistency', $timeout);
384384
}
385385

386386
/**
387387
* @return string
388388
*/
389389
public function getConsistency()
390390
{
391-
return $this->getParam('_consistency');
391+
return $this->getParam('consistency');
392392
}
393393

394394
/**
395395
* @return bool
396396
*/
397397
public function hasConsistency()
398398
{
399-
return $this->hasParam('_consistency');
399+
return $this->hasParam('consistency');
400400
}
401401

402402
/**
@@ -406,23 +406,23 @@ public function hasConsistency()
406406
*/
407407
public function setReplication($timeout)
408408
{
409-
return $this->setParam('_replication', $timeout);
409+
return $this->setParam('replication', $timeout);
410410
}
411411

412412
/**
413413
* @return string
414414
*/
415415
public function getReplication()
416416
{
417-
return $this->getParam('_replication');
417+
return $this->getParam('replication');
418418
}
419419

420420
/**
421421
* @return bool
422422
*/
423423
public function hasReplication()
424424
{
425-
return $this->hasParam('_replication');
425+
return $this->hasParam('replication');
426426
}
427427

428428
/**
@@ -455,31 +455,16 @@ public function hasUpsert()
455455
}
456456

457457
/**
458-
* @param array $fields if empty array all options will be returned, field names can be either with underscored either without, i.e. _percolate, routing
459-
* @param bool $withUnderscore should option keys contain underscore prefix
458+
* @param array $fields if empty array all options will be returned
460459
*
461460
* @return array
462461
*/
463-
public function getOptions(array $fields = [], $withUnderscore = false)
462+
public function getOptions(array $fields = [])
464463
{
465464
if (!empty($fields)) {
466-
$data = [];
467-
foreach ($fields as $field) {
468-
$key = '_'.\ltrim($field, '_');
469-
if ($this->hasParam($key) && '' !== (string) $this->getParam($key)) {
470-
$data[$key] = $this->getParam($key);
471-
}
472-
}
473-
} else {
474-
$data = $this->getParams();
475-
}
476-
if (!$withUnderscore) {
477-
foreach ($data as $key => $value) {
478-
$data[\ltrim($key, '_')] = $value;
479-
unset($data[$key]);
480-
}
465+
return array_filter(\array_intersect_key($this->getParams(), \array_flip($fields)));
481466
}
482467

483-
return $data;
468+
return array_filter($this->getParams());
484469
}
485470
}

lib/Elastica/Bulk/Action/DeleteDocument.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class DeleteDocument extends AbstractDocument
1717
protected function _getMetadata(AbstractUpdateAction $action): array
1818
{
1919
return $action->getOptions([
20-
'index',
21-
'type',
22-
'id',
20+
'_index',
21+
'_type',
22+
'_id',
2323
'version',
2424
'version_type',
2525
'routing',
2626
'parent',
27-
], true);
27+
]);
2828
}
2929
}

lib/Elastica/Bulk/Action/IndexDocument.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public function setDocument(Document $document): AbstractDocument
3030
protected function _getMetadata(AbstractUpdateAction $action): array
3131
{
3232
return $action->getOptions([
33-
'index',
34-
'type',
35-
'id',
33+
'_index',
34+
'_type',
35+
'_id',
3636
'version',
3737
'version_type',
3838
'routing',
3939
'parent',
4040
'retry_on_conflict',
41-
], true);
41+
]);
4242
}
4343
}

0 commit comments

Comments
 (0)