diff --git a/CHANGELOG.md b/CHANGELOG.md index a4216e5f7..113d740fa 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Backward Compatibility Breaks ### Added +* If not expicitly set, use `retry_on_conflict` from Client configuration in Bulk updates [#2184](https://github.com/ruflin/Elastica/pull/2184) ### Changed ### Deprecated ### Removed diff --git a/src/Bulk.php b/src/Bulk.php index 5cff33a07..cd4e883f0 100644 --- a/src/Bulk.php +++ b/src/Bulk.php @@ -131,6 +131,10 @@ public function getActions(): array */ public function addDocument(Document $document, ?string $opType = null): self { + if (!$document->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { + $document->setRetryOnConflict($retry); + } + $action = AbstractDocumentAction::create($document, $opType); return $this->addAction($action); @@ -155,6 +159,10 @@ public function addDocuments(array $documents, ?string $opType = null): self */ public function addScript(AbstractScript $script, ?string $opType = null): self { + if (!$script->hasRetryOnConflict() && $this->_client->hasConnection() && $this->_client->getConnection()->hasParam('retryOnConflict') && ($retry = $this->_client->getConnection()->getParam('retryOnConflict')) > 0) { + $script->setRetryOnConflict($retry); + } + $action = AbstractDocumentAction::create($script, $opType); return $this->addAction($action); diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 07dcdf52d..34d9c55d5 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -702,6 +702,29 @@ public function testRetry(): void $metadata = $actions[0]->getMetadata(); $this->assertEquals(5, $metadata['retry_on_conflict']); + + // Test retry via client + $client->getConnection()->setParam('retryOnConflict', 5); + $doc2 = new Document('2', ['name' => 'Invisible Woman']); + $doc2->setOpType(Action::OP_TYPE_UPDATE); + + $bulk = new Bulk($client); + $bulk->addDocument($doc2); + + $actions = $bulk->getActions(); + + $metadata = $actions[0]->getMetadata(); + $this->assertEquals(5, $metadata['retry_on_conflict']); + + $script = new Script(''); + + $bulk = new Bulk($client); + $bulk->addScript($script); + + $actions = $bulk->getActions(); + + $metadata = $actions[0]->getMetadata(); + $this->assertEquals(5, $metadata['retry_on_conflict']); } /**