Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle underscores in update option names #1621

Merged
merged 1 commit into from
Mar 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ All notable changes to this project will be documented in this file based on the
## [Unreleased](https://github.com/ruflin/Elastica/compare/6.1.1...master)

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

### Bugfixes
* Always set the Guzzle `base_uri` to support connecting to multiple ES hosts. [#1618](https://github.com/ruflin/Elastica/pull/1618)
* 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)

### Added

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ All library issues should go to the [issue tracker from github](https://github.c

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


Contributing
Expand All @@ -36,4 +36,4 @@ This project tries to follow Elasticsearch in terms of [End of Life](https://www
| [5.x](https://github.com/ruflin/Elastica/tree/5.x) | 5.x | ^5.0 | \>=5.6 |
| [3.2.3](https://github.com/ruflin/Elastica/tree/3.2.3) (unmaintained) | 2.4.0 | no | \>=5.4 |
| [2.x](https://github.com/ruflin/Elastica/tree/2.x) (unmaintained) | 1.7.2 | no | \>=5.3.3 |
------------
------------
89 changes: 37 additions & 52 deletions lib/Elastica/AbstractUpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getIndex()
*/
public function setVersion($version)
{
return $this->setParam('_version', (int) $version);
return $this->setParam('version', (int) $version);
}

/**
Expand All @@ -123,15 +123,15 @@ public function setVersion($version)
*/
public function getVersion()
{
return $this->getParam('_version');
return $this->getParam('version');
}

/**
* @return bool
*/
public function hasVersion()
{
return $this->hasParam('_version');
return $this->hasParam('version');
}

/**
Expand All @@ -144,7 +144,7 @@ public function hasVersion()
*/
public function setVersionType($versionType)
{
return $this->setParam('_version_type', $versionType);
return $this->setParam('version_type', $versionType);
}

/**
Expand All @@ -154,15 +154,15 @@ public function setVersionType($versionType)
*/
public function getVersionType()
{
return $this->getParam('_version_type');
return $this->getParam('version_type');
}

/**
* @return bool
*/
public function hasVersionType()
{
return $this->hasParam('_version_type');
return $this->hasParam('version_type');
}

/**
Expand All @@ -176,7 +176,7 @@ public function hasVersionType()
*/
public function setParent($parent)
{
return $this->setParam('_parent', $parent);
return $this->setParam('parent', $parent);
}

/**
Expand All @@ -186,15 +186,15 @@ public function setParent($parent)
*/
public function getParent()
{
return $this->getParam('_parent');
return $this->getParam('parent');
}

/**
* @return bool
*/
public function hasParent()
{
return $this->hasParam('_parent');
return $this->hasParam('parent');
}

/**
Expand All @@ -206,7 +206,7 @@ public function hasParent()
*/
public function setOpType($opType)
{
return $this->setParam('_op_type', $opType);
return $this->setParam('op_type', $opType);
}

/**
Expand All @@ -216,15 +216,15 @@ public function setOpType($opType)
*/
public function getOpType()
{
return $this->getParam('_op_type');
return $this->getParam('op_type');
}

/**
* @return bool
*/
public function hasOpType()
{
return $this->hasParam('_op_type');
return $this->hasParam('op_type');
}

/**
Expand All @@ -236,7 +236,7 @@ public function hasOpType()
*/
public function setRouting($value)
{
return $this->setParam('_routing', $value);
return $this->setParam('routing', $value);
}

/**
Expand All @@ -246,15 +246,15 @@ public function setRouting($value)
*/
public function getRouting()
{
return $this->getParam('_routing');
return $this->getParam('routing');
}

/**
* @return bool
*/
public function hasRouting()
{
return $this->hasParam('_routing');
return $this->hasParam('routing');
}

/**
Expand All @@ -268,7 +268,7 @@ public function setFields($fields)
$fields = \implode(',', $fields);
}

return $this->setParam('_fields', (string) $fields);
return $this->setParam('fields', (string) $fields);
}

/**
Expand All @@ -284,15 +284,15 @@ public function setFieldsSource()
*/
public function getFields()
{
return $this->getParam('_fields');
return $this->getParam('fields');
}

/**
* @return bool
*/
public function hasFields()
{
return $this->hasParam('_fields');
return $this->hasParam('fields');
}

/**
Expand All @@ -302,23 +302,23 @@ public function hasFields()
*/
public function setRetryOnConflict($num)
{
return $this->setParam('_retry_on_conflict', (int) $num);
return $this->setParam('retry_on_conflict', (int) $num);
}

/**
* @return int
*/
public function getRetryOnConflict()
{
return $this->getParam('_retry_on_conflict');
return $this->getParam('retry_on_conflict');
}

/**
* @return bool
*/
public function hasRetryOnConflict()
{
return $this->hasParam('_retry_on_conflict');
return $this->hasParam('retry_on_conflict');
}

/**
Expand All @@ -328,23 +328,23 @@ public function hasRetryOnConflict()
*/
public function setRefresh($refresh = true)
{
return $this->setParam('_refresh', (bool) $refresh ? 'true' : 'false');
return $this->setParam('refresh', (bool) $refresh ? 'true' : 'false');
}

/**
* @return bool
*/
public function getRefresh()
{
return 'true' === $this->getParam('_refresh');
return 'true' === $this->getParam('refresh');
}

/**
* @return bool
*/
public function hasRefresh()
{
return $this->hasParam('_refresh');
return $this->hasParam('refresh');
}

/**
Expand All @@ -354,23 +354,23 @@ public function hasRefresh()
*/
public function setTimeout($timeout)
{
return $this->setParam('_timeout', $timeout);
return $this->setParam('timeout', $timeout);
}

/**
* @return bool
*/
public function getTimeout()
{
return $this->getParam('_timeout');
return $this->getParam('timeout');
}

/**
* @return bool
*/
public function hasTimeout()
{
return $this->hasParam('_timeout');
return $this->hasParam('timeout');
}

/**
Expand All @@ -380,23 +380,23 @@ public function hasTimeout()
*/
public function setConsistency($timeout)
{
return $this->setParam('_consistency', $timeout);
return $this->setParam('consistency', $timeout);
}

/**
* @return string
*/
public function getConsistency()
{
return $this->getParam('_consistency');
return $this->getParam('consistency');
}

/**
* @return bool
*/
public function hasConsistency()
{
return $this->hasParam('_consistency');
return $this->hasParam('consistency');
}

/**
Expand All @@ -406,23 +406,23 @@ public function hasConsistency()
*/
public function setReplication($timeout)
{
return $this->setParam('_replication', $timeout);
return $this->setParam('replication', $timeout);
}

/**
* @return string
*/
public function getReplication()
{
return $this->getParam('_replication');
return $this->getParam('replication');
}

/**
* @return bool
*/
public function hasReplication()
{
return $this->hasParam('_replication');
return $this->hasParam('replication');
}

/**
Expand Down Expand Up @@ -455,31 +455,16 @@ public function hasUpsert()
}

/**
* @param array $fields if empty array all options will be returned, field names can be either with underscored either without, i.e. _percolate, routing
* @param bool $withUnderscore should option keys contain underscore prefix
* @param array $fields if empty array all options will be returned
*
* @return array
*/
public function getOptions(array $fields = [], $withUnderscore = false)
public function getOptions(array $fields = [])
{
if (!empty($fields)) {
$data = [];
foreach ($fields as $field) {
$key = '_'.\ltrim($field, '_');
if ($this->hasParam($key) && '' !== (string) $this->getParam($key)) {
$data[$key] = $this->getParam($key);
}
}
} else {
$data = $this->getParams();
}
if (!$withUnderscore) {
foreach ($data as $key => $value) {
$data[\ltrim($key, '_')] = $value;
unset($data[$key]);
}
return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields)));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup.

}

return $data;
return \array_filter($this->getParams());
}
}
8 changes: 4 additions & 4 deletions lib/Elastica/Bulk/Action/DeleteDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class DeleteDocument extends AbstractDocument
protected function _getMetadata(AbstractUpdateAction $action): array
{
return $action->getOptions([
'index',
'type',
'id',
'_index',
'_type',
'_id',
'version',
'version_type',
'routing',
'parent',
], true);
]);
}
}
8 changes: 4 additions & 4 deletions lib/Elastica/Bulk/Action/IndexDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public function setDocument(Document $document): AbstractDocument
protected function _getMetadata(AbstractUpdateAction $action): array
{
return $action->getOptions([
'index',
'type',
'id',
'_index',
'_type',
'_id',
'version',
'version_type',
'routing',
'parent',
'retry_on_conflict',
], true);
]);
}
}
Loading