diff --git a/changes.txt b/changes.txt index 8c21659ecb..8e50a0c2c2 100755 --- a/changes.txt +++ b/changes.txt @@ -1,8 +1,10 @@ CHANGES +<<<<<<< HEAD 2014-06-13 - Stop ClientTest->testDeleteIdsIdxStringTypeString from failing 1/3 of the time #634 - Stop ScanAndScrollTest->testQuerySizeOverride from failing frequently for no reason #635 +- rework Document and Script so they can share some infrastructure allowing scripts to specify things like _retry_on_conflict and _routing #629 2014-06-11 - Allow bulk API deletes to be routed #631 diff --git a/lib/Elastica/AbstractUpdateAction.php b/lib/Elastica/AbstractUpdateAction.php new file mode 100644 index 0000000000..883284aedb --- /dev/null +++ b/lib/Elastica/AbstractUpdateAction.php @@ -0,0 +1,544 @@ + + */ +class AbstractUpdateAction extends Param +{ + /** + * @var \Elastica\Document + */ + protected $_upsert; + + /** + * Sets the id of the document. + * + * @param string $id + * @return \Elastica\Document + */ + public function setId($id) + { + return $this->setParam('_id', $id); + } + + /** + * Returns document id + * + * @return string|int Document id + */ + public function getId() + { + return ($this->hasParam('_id')) ? $this->getParam('_id') : null; + } + + /** + * @return bool + */ + public function hasId() + { + return '' !== (string) $this->getId(); + } + + /** + * Sets lifetime of document + * + * @param string $ttl + * @return \Elastica\Document + */ + public function setTtl($ttl) + { + return $this->setParam('_ttl', $ttl); + } + + /** + * @return string + */ + public function getTtl() + { + return $this->getParam('_ttl'); + } + + /** + * @return bool + */ + public function hasTtl() + { + return $this->hasParam('_ttl'); + } + + /** + * Sets the document type name + * + * @param string $type Type name + * @return \Elastica\Document Current object + */ + public function setType($type) + { + if ($type instanceof Type) { + $this->setIndex($type->getIndex()); + $type = $type->getName(); + } + return $this->setParam('_type', $type); + } + + /** + * Return document type name + * + * @return string Document type name + * @throws \Elastica\Exception\InvalidException + */ + public function getType() + { + return $this->getParam('_type'); + } + + /** + * Sets the document index name + * + * @param string $index Index name + * @return \Elastica\Document Current object + */ + public function setIndex($index) + { + if ($index instanceof Index) { + $index = $index->getName(); + } + return $this->setParam('_index', $index); + } + + /** + * Get the document index name + * + * @return string Index name + * @throws \Elastica\Exception\InvalidException + */ + public function getIndex() + { + return $this->getParam('_index'); + } + + /** + * Sets the version of a document for use with optimistic concurrency control + * + * @param int $version Document version + * @return \Elastica\Document Current object + * @link http://www.elasticsearch.org/blog/2011/02/08/versioning.html + */ + public function setVersion($version) + { + return $this->setParam('_version', (int) $version); + } + + /** + * Returns document version + * + * @return string|int Document version + */ + public function getVersion() + { + return $this->getParam('_version'); + } + + /** + * @return bool + */ + public function hasVersion() + { + return $this->hasParam('_version'); + } + + /** + * Sets the version_type of a document + * Default in ES is internal, but you can set to external to use custom versioning + * + * @param int $versionType Document version type + * @return \Elastica\Document Current object + * @link http://www.elasticsearch.org/guide/reference/api/index_.html + */ + public function setVersionType($versionType) + { + return $this->setParam('_version_type', $versionType); + } + + /** + * Returns document version type + * + * @return string|int Document version type + */ + public function getVersionType() + { + return $this->getParam('_version_type'); + } + + /** + * @return bool + */ + public function hasVersionType() + { + return $this->hasParam('_version_type'); + } + + /** + * Sets parent document id + * + * @param string|int $parent Parent document id + * @return \Elastica\Document Current object + * @link http://www.elasticsearch.org/guide/reference/mapping/parent-field.html + */ + public function setParent($parent) + { + return $this->setParam('_parent', $parent); + } + + /** + * Returns the parent document id + * + * @return string|int Parent document id + */ + public function getParent() + { + return $this->getParam('_parent'); + } + + /** + * @return bool + */ + public function hasParent() + { + return $this->hasParam('_parent'); + } + + /** + * Set operation type + * + * @param string $opType Only accept create + * @return \Elastica\Document Current object + */ + public function setOpType($opType) + { + return $this->setParam('_op_type', $opType); + } + + /** + * Get operation type + * @return string + */ + public function getOpType() + { + return $this->getParam('_op_type'); + } + + /** + * @return bool + */ + public function hasOpType() + { + return $this->hasParam('_op_type'); + } + + /** + * Set percolate query param + * + * @param string $value percolator filter + * @return \Elastica\Document + */ + public function setPercolate($value = '*') + { + return $this->setParam('_percolate', $value); + } + + /** + * Get percolate parameter + * + * @return string + */ + public function getPercolate() + { + return $this->getParam('_percolate'); + } + + /** + * @return bool + */ + public function hasPercolate() + { + return $this->hasParam('_percolate'); + } + + /** + * Set routing query param + * + * @param string $value routing + * @return \Elastica\Document + */ + public function setRouting($value) + { + return $this->setParam('_routing', $value); + } + + /** + * Get routing parameter + * + * @return string + */ + public function getRouting() + { + return $this->getParam('_routing'); + } + + /** + * @return bool + */ + public function hasRouting() + { + return $this->hasParam('_routing'); + } + + /** + * @param array|string $fields + * @return \Elastica\Document + */ + public function setFields($fields) + { + if (is_array($fields)) { + $fields = implode(',', $fields); + } + return $this->setParam('_fields', (string) $fields); + } + + /** + * @return \Elastica\Document + */ + public function setFieldsSource() + { + return $this->setFields('_source'); + } + + /** + * @return string + */ + public function getFields() + { + return $this->getParam('_fields'); + } + + /** + * @return bool + */ + public function hasFields() + { + return $this->hasParam('_fields'); + } + + /** + * @param int $num + * @return \Elastica\Document + */ + public function setRetryOnConflict($num) + { + return $this->setParam('_retry_on_conflict', (int) $num); + } + + /** + * @return int + */ + public function getRetryOnConflict() + { + return $this->getParam('_retry_on_conflict'); + } + + /** + * @return bool + */ + public function hasRetryOnConflict() + { + return $this->hasParam('_retry_on_conflict'); + } + + /** + * @param string $timestamp + * @return \Elastica\Document + */ + public function setTimestamp($timestamp) + { + return $this->setParam('_timestamp', $timestamp); + } + + /** + * @return int + */ + public function getTimestamp() + { + return $this->getParam('_timestamp'); + } + + /** + * @return bool + */ + public function hasTimestamp() + { + return $this->hasParam('_timestamp'); + } + + /** + * @param bool $refresh + * @return \Elastica\Document + */ + public function setRefresh($refresh = true) + { + return $this->setParam('_refresh', (bool) $refresh); + } + + /** + * @return bool + */ + public function getRefresh() + { + return $this->getParam('_refresh'); + } + + /** + * @return bool + */ + public function hasRefresh() + { + return $this->hasParam('_refresh'); + } + + /** + * @param string $timeout + * @return \Elastica\Document + */ + public function setTimeout($timeout) + { + return $this->setParam('_timeout', $timeout); + } + + /** + * @return bool + */ + public function getTimeout() + { + return $this->getParam('_timeout'); + } + + /** + * @return string + */ + public function hasTimeout() + { + return $this->hasParam('_timeout'); + } + + /** + * @param string $timeout + * @return \Elastica\Document + */ + public function setConsistency($timeout) + { + return $this->setParam('_consistency', $timeout); + } + + /** + * @return string + */ + public function getConsistency() + { + return $this->getParam('_consistency'); + } + + /** + * @return string + */ + public function hasConsistency() + { + return $this->hasParam('_consistency'); + } + + /** + * @param string $timeout + * @return \Elastica\Document + */ + public function setReplication($timeout) + { + return $this->setParam('_replication', $timeout); + } + + /** + * @return string + */ + public function getReplication() + { + return $this->getParam('_replication'); + } + + /** + * @return bool + */ + public function hasReplication() + { + return $this->hasParam('_replication'); + } + + /** + * @param \Elastica\Document|array $data + * @return \Elastica\Document + */ + public function setUpsert($data) + { + $document = Document::create($data); + $this->_upsert = $document; + + return $this; + } + + /** + * @return \Elastica\Document + */ + public function getUpsert() + { + return $this->_upsert; + } + + /** + * @return bool + */ + public function hasUpsert() + { + return null !== $this->_upsert; + } + + /** + * @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 + * @return array + */ + public function getOptions(array $fields = array(), $withUnderscore = false) + { + if (!empty($fields)) { + $data = array(); + 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 $data; + } +} \ No newline at end of file diff --git a/lib/Elastica/Bulk/Action/AbstractDocument.php b/lib/Elastica/Bulk/Action/AbstractDocument.php index a143a567f3..545e695c5a 100755 --- a/lib/Elastica/Bulk/Action/AbstractDocument.php +++ b/lib/Elastica/Bulk/Action/AbstractDocument.php @@ -2,6 +2,7 @@ namespace Elastica\Bulk\Action; +use Elastica\AbstractUpdateAction; use Elastica\Bulk\Action; use Elastica\Document; use Elastica\Script; @@ -29,7 +30,7 @@ public function setDocument(Document $document) { $this->_data = $document; - $metadata = $this->_getMetadataByDocument($document); + $metadata = $this->_getMetadata($document); $this->setMetadata($metadata); @@ -48,7 +49,7 @@ public function setScript(Script $script) $this->_data = $script; - $metadata = $this->_getMetadataByScript($script); + $metadata = $this->_getMetadata($script); $this->setMetadata($metadata); return $this; @@ -111,18 +112,10 @@ public function getData() } /** - * @param \Elastica\Document $document + * @param \Elastica\AbstractUpdateAction $source * @return array */ - abstract protected function _getMetadataByDocument(Document $document); - - /** - * @param \Elastica\Script $script - * @return array - */ - protected function _getMetadataByScript(Script $script) - { - } + abstract protected function _getMetadata(AbstractUpdateAction $source); /** * @param \Elastica\Document|\Elastica\Script $data @@ -135,14 +128,18 @@ public static function create($data, $opType = null) if (!($data instanceof Document) && !($data instanceof Script)) { throw new \InvalidArgumentException("The data needs to be a Document or a Script."); } - + if (null === $opType && $data->hasOpType()) { $opType = $data->getOpType(); } //Check that scripts can only be used for updates - if ($data instanceof Script && isset($opType) && $opType != self::OP_TYPE_UPDATE) { - throw new \InvalidArgumentException("When performing an update action, the data needs to be a Document or a Script."); + if ($data instanceof Script) { + if ($opType === null) { + $opType = self::OP_TYPE_UPDATE; + } else if ($opType != self::OP_TYPE_UPDATE) { + throw new \InvalidArgumentException("Scripts can only be used with the update operation type."); + } } switch ($opType) { diff --git a/lib/Elastica/Bulk/Action/DeleteDocument.php b/lib/Elastica/Bulk/Action/DeleteDocument.php index 5e46d7859f..572f80b667 100644 --- a/lib/Elastica/Bulk/Action/DeleteDocument.php +++ b/lib/Elastica/Bulk/Action/DeleteDocument.php @@ -2,7 +2,7 @@ namespace Elastica\Bulk\Action; -use Elastica\Document; +use Elastica\AbstractUpdateAction; class DeleteDocument extends AbstractDocument { @@ -12,10 +12,10 @@ class DeleteDocument extends AbstractDocument protected $_opType = self::OP_TYPE_DELETE; /** - * @param \Elastica\Document $document + * @param \Elastica\AbstractUpdateAction $action * @return array */ - protected function _getMetadataByDocument(Document $document) + protected function _getMetadata(AbstractUpdateAction $action) { $params = array( 'index', @@ -26,7 +26,7 @@ protected function _getMetadataByDocument(Document $document) 'routing', 'parent' ); - $metadata = $document->getOptions($params, true); + $metadata = $action->getOptions($params, true); return $metadata; } diff --git a/lib/Elastica/Bulk/Action/IndexDocument.php b/lib/Elastica/Bulk/Action/IndexDocument.php index 099b309217..d405563ebd 100644 --- a/lib/Elastica/Bulk/Action/IndexDocument.php +++ b/lib/Elastica/Bulk/Action/IndexDocument.php @@ -2,6 +2,7 @@ namespace Elastica\Bulk\Action; +use Elastica\AbstractUpdateAction; use Elastica\Bulk\Action; use Elastica\Document; @@ -26,10 +27,10 @@ public function setDocument(Document $document) } /** - * @param \Elastica\Document $document + * @param \Elastica\AbstractUpdateAction $source * @return array */ - protected function _getMetadataByDocument(Document $document) + protected function _getMetadata(AbstractUpdateAction $action) { $params = array( 'index', @@ -44,7 +45,7 @@ protected function _getMetadataByDocument(Document $document) 'timestamp', 'retry_on_conflict', ); - $metadata = $document->getOptions($params, true); + $metadata = $action->getOptions($params, true); return $metadata; } diff --git a/lib/Elastica/Bulk/Action/UpdateDocument.php b/lib/Elastica/Bulk/Action/UpdateDocument.php index 1f06d384e5..4c1dbaa449 100755 --- a/lib/Elastica/Bulk/Action/UpdateDocument.php +++ b/lib/Elastica/Bulk/Action/UpdateDocument.php @@ -66,27 +66,4 @@ public function setScript(Script $script) return $this; } - - /** - * @param \Elastica\Script $script - * @return array - */ - protected function _getMetadataByScript(Script $script) - { - $params = array( - 'index', - 'type', - 'id', - 'version', - 'version_type', - 'routing', - 'percolate', - 'parent', - 'ttl', - 'timestamp', - ); - $metadata = $script->getOptions($params, true); - - return $metadata; - } } diff --git a/lib/Elastica/Document.php b/lib/Elastica/Document.php index 2b8e63286e..3f6b88dee6 100644 --- a/lib/Elastica/Document.php +++ b/lib/Elastica/Document.php @@ -14,7 +14,7 @@ * @package Elastica * @author Nicolas Ruflin */ -class Document extends Param +class Document extends AbstractUpdateAction { const OP_TYPE_CREATE = Action::OP_TYPE_CREATE; @@ -25,11 +25,6 @@ class Document extends Param */ protected $_data = array(); - /** - * @var \Elastica\Document - */ - protected $_upsert; - /** * Whether to use this document to upsert if the document does not exist. * @@ -58,35 +53,6 @@ public function __construct($id = '', $data = array(), $type = '', $index = '') $this->setIndex($index); } - /** - * Sets the id of the document. - * - * @param string $id - * @return \Elastica\Document - */ - public function setId($id) - { - return $this->setParam('_id', $id); - } - - /** - * Returns document id - * - * @return string|int Document id - */ - public function getId() - { - return ($this->hasParam('_id')) ? $this->getParam('_id') : null; - } - - /** - * @return bool - */ - public function hasId() - { - return '' !== (string) $this->getId(); - } - /** * @param string $key * @return mixed @@ -262,33 +228,6 @@ public function setData($data) return $this; } - /** - * Sets lifetime of document - * - * @param string $ttl - * @return \Elastica\Document - */ - public function setTtl($ttl) - { - return $this->setParam('_ttl', $ttl); - } - - /** - * @return string - */ - public function getTtl() - { - return $this->getParam('_ttl'); - } - - /** - * @return bool - */ - public function hasTtl() - { - return $this->hasParam('_ttl'); - } - /** * Returns the document data * @@ -299,420 +238,6 @@ public function getData() return $this->_data; } - /** - * Sets the document type name - * - * @param string $type Type name - * @return \Elastica\Document Current object - */ - public function setType($type) - { - if ($type instanceof Type) { - $this->setIndex($type->getIndex()); - $type = $type->getName(); - } - return $this->setParam('_type', $type); - } - - /** - * Return document type name - * - * @return string Document type name - * @throws \Elastica\Exception\InvalidException - */ - public function getType() - { - return $this->getParam('_type'); - } - - /** - * Sets the document index name - * - * @param string $index Index name - * @return \Elastica\Document Current object - */ - public function setIndex($index) - { - if ($index instanceof Index) { - $index = $index->getName(); - } - return $this->setParam('_index', $index); - } - - /** - * Get the document index name - * - * @return string Index name - * @throws \Elastica\Exception\InvalidException - */ - public function getIndex() - { - return $this->getParam('_index'); - } - - /** - * Sets the version of a document for use with optimistic concurrency control - * - * @param int $version Document version - * @return \Elastica\Document Current object - * @link http://www.elasticsearch.org/blog/2011/02/08/versioning.html - */ - public function setVersion($version) - { - return $this->setParam('_version', (int) $version); - } - - /** - * Returns document version - * - * @return string|int Document version - */ - public function getVersion() - { - return $this->getParam('_version'); - } - - /** - * @return bool - */ - public function hasVersion() - { - return $this->hasParam('_version'); - } - - /** - * Sets the version_type of a document - * Default in ES is internal, but you can set to external to use custom versioning - * - * @param int $versionType Document version type - * @return \Elastica\Document Current object - * @link http://www.elasticsearch.org/guide/reference/api/index_.html - */ - public function setVersionType($versionType) - { - return $this->setParam('_version_type', $versionType); - } - - /** - * Returns document version type - * - * @return string|int Document version type - */ - public function getVersionType() - { - return $this->getParam('_version_type'); - } - - /** - * @return bool - */ - public function hasVersionType() - { - return $this->hasParam('_version_type'); - } - - /** - * Sets parent document id - * - * @param string|int $parent Parent document id - * @return \Elastica\Document Current object - * @link http://www.elasticsearch.org/guide/reference/mapping/parent-field.html - */ - public function setParent($parent) - { - return $this->setParam('_parent', $parent); - } - - /** - * Returns the parent document id - * - * @return string|int Parent document id - */ - public function getParent() - { - return $this->getParam('_parent'); - } - - /** - * @return bool - */ - public function hasParent() - { - return $this->hasParam('_parent'); - } - - /** - * Set operation type - * - * @param string $opType Only accept create - * @return \Elastica\Document Current object - */ - public function setOpType($opType) - { - return $this->setParam('_op_type', $opType); - } - - /** - * Get operation type - * @return string - */ - public function getOpType() - { - return $this->getParam('_op_type'); - } - - /** - * @return bool - */ - public function hasOpType() - { - return $this->hasParam('_op_type'); - } - - /** - * Set percolate query param - * - * @param string $value percolator filter - * @return \Elastica\Document - */ - public function setPercolate($value = '*') - { - return $this->setParam('_percolate', $value); - } - - /** - * Get percolate parameter - * - * @return string - */ - public function getPercolate() - { - return $this->getParam('_percolate'); - } - - /** - * @return bool - */ - public function hasPercolate() - { - return $this->hasParam('_percolate'); - } - - /** - * Set routing query param - * - * @param string $value routing - * @return \Elastica\Document - */ - public function setRouting($value) - { - return $this->setParam('_routing', $value); - } - - /** - * Get routing parameter - * - * @return string - */ - public function getRouting() - { - return $this->getParam('_routing'); - } - - /** - * @return bool - */ - public function hasRouting() - { - return $this->hasParam('_routing'); - } - - /** - * @param array|string $fields - * @return \Elastica\Document - */ - public function setFields($fields) - { - if (is_array($fields)) { - $fields = implode(',', $fields); - } - return $this->setParam('_fields', (string) $fields); - } - - /** - * @return \Elastica\Document - */ - public function setFieldsSource() - { - return $this->setFields('_source'); - } - - /** - * @return string - */ - public function getFields() - { - return $this->getParam('_fields'); - } - - /** - * @return bool - */ - public function hasFields() - { - return $this->hasParam('_fields'); - } - - /** - * @param int $num - * @return \Elastica\Document - */ - public function setRetryOnConflict($num) - { - return $this->setParam('_retry_on_conflict', (int) $num); - } - - /** - * @return int - */ - public function getRetryOnConflict() - { - return $this->getParam('_retry_on_conflict'); - } - - /** - * @return bool - */ - public function hasRetryOnConflict() - { - return $this->hasParam('_retry_on_conflict'); - } - - /** - * @param string $timestamp - * @return \Elastica\Document - */ - public function setTimestamp($timestamp) - { - return $this->setParam('_timestamp', $timestamp); - } - - /** - * @return int - */ - public function getTimestamp() - { - return $this->getParam('_timestamp'); - } - - /** - * @return bool - */ - public function hasTimestamp() - { - return $this->hasParam('_timestamp'); - } - - /** - * @param bool $refresh - * @return \Elastica\Document - */ - public function setRefresh($refresh = true) - { - return $this->setParam('_refresh', (bool) $refresh); - } - - /** - * @return bool - */ - public function getRefresh() - { - return $this->getParam('_refresh'); - } - - /** - * @return bool - */ - public function hasRefresh() - { - return $this->hasParam('_refresh'); - } - - /** - * @param string $timeout - * @return \Elastica\Document - */ - public function setTimeout($timeout) - { - return $this->setParam('_timeout', $timeout); - } - - /** - * @return bool - */ - public function getTimeout() - { - return $this->getParam('_timeout'); - } - - /** - * @return string - */ - public function hasTimeout() - { - return $this->hasParam('_timeout'); - } - - /** - * @param string $timeout - * @return \Elastica\Document - */ - public function setConsistency($timeout) - { - return $this->setParam('_consistency', $timeout); - } - - /** - * @return string - */ - public function getConsistency() - { - return $this->getParam('_consistency'); - } - - /** - * @return string - */ - public function hasConsistency() - { - return $this->hasParam('_consistency'); - } - - /** - * @param string $timeout - * @return \Elastica\Document - */ - public function setReplication($timeout) - { - return $this->setParam('_replication', $timeout); - } - - /** - * @return string - */ - public function getReplication() - { - return $this->getParam('_replication'); - } - - /** - * @return bool - */ - public function hasReplication() - { - return $this->hasParam('_replication'); - } - /** * @param \Elastica\Script $data * @throws NotImplementedException @@ -723,18 +248,6 @@ public function setScript($data) throw new NotImplementedException("setScript() is no longer available as of 0.90.2. See http://elastica.io/migration/0.90.2/upsert.html to migrate"); } - /** - * @param \Elastica\Document|array $data - * @return \Elastica\Document - */ - public function setUpsert($data) - { - $document = Document::create($data); - $this->_upsert = $document; - - return $this; - } - /** * @throws NotImplementedException * @deprecated @@ -744,14 +257,6 @@ public function getScript() throw new NotImplementedException("getScript() is no longer available as of 0.90.2. See http://elastica.io/migration/0.90.2/upsert.html to migrate"); } - /** - * @return \Elastica\Document - */ - public function getUpsert() - { - return $this->_upsert; - } - /** * @throws NotImplementedException * @deprecated @@ -761,14 +266,6 @@ public function hasScript() throw new NotImplementedException("hasScript() is no longer available as of 0.90.2. See http://elastica.io/migration/0.90.2/upsert.html to migrate"); } - /** - * @return bool - */ - public function hasUpsert() - { - return null !== $this->_upsert; - } - /** * @param bool $value * @return \Elastica\Document @@ -819,33 +316,6 @@ public function toArray() return $doc; } - /** - * @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 - * @return array - */ - public function getOptions(array $fields = array(), $withUnderscore = false) - { - if (!empty($fields)) { - $data = array(); - 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 $data; - } - /** * @param array|\Elastica\Document $data * @throws \Elastica\Exception\InvalidException diff --git a/lib/Elastica/Script.php b/lib/Elastica/Script.php index f896c90d8a..99d9a6aa88 100644 --- a/lib/Elastica/Script.php +++ b/lib/Elastica/Script.php @@ -12,7 +12,7 @@ * @author avasilenko * @link http://www.elasticsearch.org/guide/reference/modules/scripting.html */ -class Script extends Param +class Script extends AbstractUpdateAction { const LANG_MVEL = 'mvel'; const LANG_JS = 'js'; @@ -30,11 +30,6 @@ class Script extends Param */ private $_lang; - /** - * @var \Elastica\Document - */ - protected $_upsert; - /** * @param string $script * @param array|null $params @@ -107,93 +102,6 @@ public static function create($data) return $script; } - /** - * @param \Elastica\Document|array $data - * @return \Elastica\Document - */ - public function setUpsert($data) - { - $document = Document::create($data); - $this->_upsert = $document; - - return $this; - } - - /** - * @return \Elastica\Document - */ - public function getUpsert() - { - return $this->_upsert; - } - - /** - * @return bool - */ - public function hasUpsert() - { - return null !== $this->_upsert; - } - - /** - * Sets the id of the document the script updates. - * - * @param string $id - * @return \Elastica\Script - */ - public function setId($id) - { - return $this->setParam('_id', $id); - } - - /** - * Returns id of the document the script updates. - * - * @return string|int Document id - */ - public function getId() - { - return ($this->hasParam('_id')) ? $this->getParam('_id') : null; - } - - /** - * @return bool - */ - public function hasId() - { - return (bool)$this->getId(); - } - - /** - * @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 - * @return array - */ - public function getOptions(array $fields = array(), $withUnderscore = false) - { - if (!empty($fields)) { - $data = array(); - - 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 $data; - } - /** * @param array $data * @throws \Elastica\Exception\InvalidException diff --git a/test/lib/Elastica/Test/BulkTest.php b/test/lib/Elastica/Test/BulkTest.php index a289e26d43..3396fe863c 100755 --- a/test/lib/Elastica/Test/BulkTest.php +++ b/test/lib/Elastica/Test/BulkTest.php @@ -633,6 +633,17 @@ public function testRetry() $metadata = $actions[0]->getMetadata(); $this->assertEquals(5, $metadata[ '_retry_on_conflict' ]); + + $script = new \Elastica\Script( '' ); + $script->setRetryOnConflict(5); + + $bulk = new Bulk($client); + $bulk->addScript($script); + + $actions = $bulk->getActions(); + + $metadata = $actions[0]->getMetadata(); + $this->assertEquals(5, $metadata[ '_retry_on_conflict' ]); } public function udpDataProvider()