From 1b62adc345dddde0e89049bd58343d1909d36cdb Mon Sep 17 00:00:00 2001 From: Diogo Date: Thu, 19 Aug 2021 16:42:37 +0200 Subject: [PATCH 01/14] fix getDocument to return the version params --- src/Index.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Index.php b/src/Index.php index 249232fe08..f4fe6862d3 100644 --- a/src/Index.php +++ b/src/Index.php @@ -280,7 +280,7 @@ public function getDocument($id, array $options = []): Document } $doc = new Document($id, $data, $this->getName()); - $doc->setVersionParams($data); + $doc->setVersionParams($result); return $doc; } From b9bd935fbaa6650b7bc44c3b57808ec93e4853b7 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 23 Aug 2021 16:43:10 +0200 Subject: [PATCH 02/14] adds test for getting document with version --- tests/ClientFunctionalTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/ClientFunctionalTest.php b/tests/ClientFunctionalTest.php index 88af58ecd2..e7fd0d071b 100644 --- a/tests/ClientFunctionalTest.php +++ b/tests/ClientFunctionalTest.php @@ -219,6 +219,28 @@ public function testUpdateDocuments(): void $this->assertEquals($modifiedValue, $docData2['age']); } + public function testGetDocumentWithVersion(): void + { + $indexName = 'test'; + + $client = $this->_getClient(); + $index = $client->getIndex($indexName); + + $initialValue = 28; + + $doc1 = new Document( + 1, + ['name' => 'hans', 'age' => $initialValue], + $indexName + ); + $data = [$doc1]; + $client->addDocuments($data); + + $doc1 = $index->getDocument(1); + + $this->assertGreaterThan(0, $doc1->getVersion()); + } + /** * Test deleteIds method using string parameter for $index * and object parameter for $type. From d1b3ddc601d58735c72a143ef0ebdb39a88710d6 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 23 Aug 2021 16:43:19 +0200 Subject: [PATCH 03/14] fix failing tests --- src/Query/MoreLikeThis.php | 2 ++ tests/Aggregation/TermsTest.php | 3 +++ 2 files changed, 5 insertions(+) diff --git a/src/Query/MoreLikeThis.php b/src/Query/MoreLikeThis.php index 572de612eb..6da0bf88d3 100644 --- a/src/Query/MoreLikeThis.php +++ b/src/Query/MoreLikeThis.php @@ -170,6 +170,8 @@ public function toArray(): array $doc = $array['more_like_this']['like']; $doc['doc'] = $array['more_like_this']['like']['_source']; unset($doc['_id'], $doc['_source']); + unset($doc['if_seq_no']); + unset($doc['if_primary_term']); $array['more_like_this']['like'] = $doc; } diff --git a/tests/Aggregation/TermsTest.php b/tests/Aggregation/TermsTest.php index aa7e2f0219..0628cd5cf1 100644 --- a/tests/Aggregation/TermsTest.php +++ b/tests/Aggregation/TermsTest.php @@ -103,6 +103,9 @@ public function testTermsSetOrder(): void $this->assertEquals('blue', $results['buckets'][2]['key']); } + /** + * @group functional + */ public function testTermsWithMissingAggregation(): void { $agg = new Terms('terms'); From 9e870683c7480831813f8141745d4f0f83c543da Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 23 Aug 2021 16:46:31 +0200 Subject: [PATCH 04/14] Updates changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a6e273254..bb5e905c55 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated ### Removed ### Fixed +* Fixed the assignment of `version`, `seq_no` and `primary_term` on `getDocument` [#1973](https://github.com/ruflin/Elastica/pull/1973) ### Security ## [7.1.1](https://github.com/ruflin/Elastica/compare/7.1.0...7.1.1) From 97b4e796dfaee187c376407020876e89a971ab8d Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Tue, 24 Aug 2021 09:46:43 +0200 Subject: [PATCH 05/14] run fix-phpcs --- src/Query/MoreLikeThis.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Query/MoreLikeThis.php b/src/Query/MoreLikeThis.php index 6da0bf88d3..d028d8fc1b 100644 --- a/src/Query/MoreLikeThis.php +++ b/src/Query/MoreLikeThis.php @@ -169,9 +169,7 @@ public function toArray(): array } elseif (!empty($array['more_like_this']['like']['_source'])) { $doc = $array['more_like_this']['like']; $doc['doc'] = $array['more_like_this']['like']['_source']; - unset($doc['_id'], $doc['_source']); - unset($doc['if_seq_no']); - unset($doc['if_primary_term']); + unset($doc['_id'], $doc['_source'], $doc['if_seq_no'], $doc['if_primary_term']); $array['more_like_this']['like'] = $doc; } From 54487fd69d14f3035053faf868989a1391f684be Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Fri, 27 Aug 2021 10:17:51 +0200 Subject: [PATCH 06/14] add failing test for scriptUpsert on bulk --- tests/BulkTest.php | 63 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/BulkTest.php b/tests/BulkTest.php index e27d9c578e..d9747e64db 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -583,6 +583,69 @@ public function testUpsert(): void $this->assertEquals('Maradona', $docData['name']); } + /** + * @group functional + */ + public function testScriptUpsert(): void + { + $index = $this->_createIndex(); + $client = $index->getClient(); + + $bulk = new Bulk($client); + $bulk->setIndex($index); + + $id = 1; + $field = 123456789; + $subField = ['field' => 'a']; + $subField2 = ['field_2' => 'b']; + + $defaultData = [ + 'field' => null, + 'sub_field' => [], + 'sub_field_2' => [] + ]; + + //insert doc and update field + $script = new Script("ctx._source.field = params.field", ['field' => $field]); + $script->setUpsert($defaultData); + $script->setId($id); + + $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); + $bulk->addAction($action); + + //update sub_field + $script = new Script("if ( !ctx._source.sub_field.contains(params) ) ctx._source.sub_field.add(params)", $subField); + $script->setUpsert($defaultData); + $script->setId($id); + + $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); + $bulk->addAction($action); + + //update sub_field_2 + $script = new Script("if ( !ctx._source.sub_field_2.contains(params) ) ctx._source.sub_field_2.add(params)", $subField2); + $script->setUpsert($defaultData); + $script->setId($id); + + $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); + $bulk->addAction($action); + + $response = $bulk->send(); + + $this->assertTrue($response->isOk()); + + $index->refresh(); + + $doc = $index->getDocument($id); + + $this->assertEquals($field, $doc->getData()['field']); + + $this->assertCount(1, $doc->getData()['sub_field']); + $this->assertCount(1, $doc->getData()['sub_field_2']); + + $this->assertEquals($subField['field'], $doc->getData()['sub_field'][0]['field']); + $this->assertEquals($subField2['field_2'], $doc->getData()['sub_field_2'][0]['field_2']); + } + /** * @group unit */ From 2e01b39456470a3f1817fdce61e200acef8e3a4c Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Fri, 27 Aug 2021 13:57:12 +0200 Subject: [PATCH 07/14] adds upsert_script flag --- src/Bulk/Action/UpdateDocument.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Bulk/Action/UpdateDocument.php b/src/Bulk/Action/UpdateDocument.php index b14a346f46..317551f616 100644 --- a/src/Bulk/Action/UpdateDocument.php +++ b/src/Bulk/Action/UpdateDocument.php @@ -51,6 +51,7 @@ public function setScript(AbstractScript $script): AbstractDocument if (!empty($upsert)) { $source['upsert'] = $upsert; + $source['scripted_upsert'] = true; } } From 0918ba57610a375b139d3f60bac0e4fc5686d512 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Fri, 27 Aug 2021 13:57:31 +0200 Subject: [PATCH 08/14] fix failing test --- tests/BulkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BulkTest.php b/tests/BulkTest.php index d9747e64db..852a5cb584 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -462,7 +462,7 @@ public function testUpdate(): void $this->assertEquals('The Walrus was Paul', $doc2->name); //test upsert - $script = new Script('', [], null, 5); + $script = new Script('return;', [], null, 5); $doc = new Document('', ['counter' => 1]); $script->setUpsert($doc); $updateAction = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); From 049f8f0fc3ba333743d0461353ce2c9456ec3d02 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Fri, 27 Aug 2021 15:05:48 +0200 Subject: [PATCH 09/14] run fix phpcs --- tests/BulkTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 852a5cb584..a5a5da283f 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -602,11 +602,11 @@ public function testScriptUpsert(): void $defaultData = [ 'field' => null, 'sub_field' => [], - 'sub_field_2' => [] + 'sub_field_2' => [], ]; //insert doc and update field - $script = new Script("ctx._source.field = params.field", ['field' => $field]); + $script = new Script('ctx._source.field = params.field', ['field' => $field]); $script->setUpsert($defaultData); $script->setId($id); @@ -614,7 +614,7 @@ public function testScriptUpsert(): void $bulk->addAction($action); //update sub_field - $script = new Script("if ( !ctx._source.sub_field.contains(params) ) ctx._source.sub_field.add(params)", $subField); + $script = new Script('if ( !ctx._source.sub_field.contains(params) ) ctx._source.sub_field.add(params)', $subField); $script->setUpsert($defaultData); $script->setId($id); @@ -622,7 +622,7 @@ public function testScriptUpsert(): void $bulk->addAction($action); //update sub_field_2 - $script = new Script("if ( !ctx._source.sub_field_2.contains(params) ) ctx._source.sub_field_2.add(params)", $subField2); + $script = new Script('if ( !ctx._source.sub_field_2.contains(params) ) ctx._source.sub_field_2.add(params)', $subField2); $script->setUpsert($defaultData); $script->setId($id); From 45e11c65f3926322ef5537176beb88065e418e2e Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Fri, 27 Aug 2021 16:10:49 +0200 Subject: [PATCH 10/14] updates changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb5e905c55..3ab8005c60 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed ### Fixed * Fixed the assignment of `version`, `seq_no` and `primary_term` on `getDocument` [#1973](https://github.com/ruflin/Elastica/pull/1973) +* Fixed the upsert scripts on bulk requests [#1974](https://github.com/ruflin/Elastica/pull/1974) ### Security ## [7.1.1](https://github.com/ruflin/Elastica/compare/7.1.0...7.1.1) From 2a86f4897a6388482331bc8986724448f80e8071 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 4 Oct 2021 10:24:56 +0200 Subject: [PATCH 11/14] add upsert script flag --- src/Bulk/Action/UpdateDocument.php | 5 ++++- src/Script/AbstractScript.php | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Bulk/Action/UpdateDocument.php b/src/Bulk/Action/UpdateDocument.php index 317551f616..f0f234844f 100644 --- a/src/Bulk/Action/UpdateDocument.php +++ b/src/Bulk/Action/UpdateDocument.php @@ -51,7 +51,10 @@ public function setScript(AbstractScript $script): AbstractDocument if (!empty($upsert)) { $source['upsert'] = $upsert; - $source['scripted_upsert'] = true; + + if ($script->getScriptedUpsert()) { + $source['scripted_upsert'] = true; + } } } diff --git a/src/Script/AbstractScript.php b/src/Script/AbstractScript.php index 70af09968c..4bafeba949 100644 --- a/src/Script/AbstractScript.php +++ b/src/Script/AbstractScript.php @@ -27,6 +27,11 @@ abstract class AbstractScript extends AbstractUpdateAction */ private $_lang; + /** + * @var bool|null + */ + private $scriptedUpsert; + /** * @param string|null $lang Script language, see constants * @param string|null $documentId Document ID the script action should be performed on (only relevant in update context) @@ -136,4 +141,14 @@ private static function _createFromArray(array $data) throw new InvalidException('Failed to create script. Invalid data passed.'); } + + public function setScriptedUpsert(bool $scriptedUpsert): void + { + $this->scriptedUpsert = $scriptedUpsert; + } + + public function getScriptedUpsert(): bool + { + return $this->scriptedUpsert; + } } From d467d445134d814331c8f309dfdaaa5eb3080357 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 4 Oct 2021 10:28:03 +0200 Subject: [PATCH 12/14] revert test to allow empty script with upsert --- tests/BulkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/BulkTest.php b/tests/BulkTest.php index a5a5da283f..5f17cbc573 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -462,7 +462,7 @@ public function testUpdate(): void $this->assertEquals('The Walrus was Paul', $doc2->name); //test upsert - $script = new Script('return;', [], null, 5); + $script = new Script('', [], null, 5); $doc = new Document('', ['counter' => 1]); $script->setUpsert($doc); $updateAction = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); From ab150dcbf53e75cc7c57bddefed5ad6bf1ff4d2f Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 4 Oct 2021 10:34:17 +0200 Subject: [PATCH 13/14] fix failing test --- src/Script/AbstractScript.php | 2 +- tests/BulkTest.php | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Script/AbstractScript.php b/src/Script/AbstractScript.php index 4bafeba949..7516644c5a 100644 --- a/src/Script/AbstractScript.php +++ b/src/Script/AbstractScript.php @@ -147,7 +147,7 @@ public function setScriptedUpsert(bool $scriptedUpsert): void $this->scriptedUpsert = $scriptedUpsert; } - public function getScriptedUpsert(): bool + public function getScriptedUpsert(): ?bool { return $this->scriptedUpsert; } diff --git a/tests/BulkTest.php b/tests/BulkTest.php index 5f17cbc573..3dd1992991 100644 --- a/tests/BulkTest.php +++ b/tests/BulkTest.php @@ -609,6 +609,7 @@ public function testScriptUpsert(): void $script = new Script('ctx._source.field = params.field', ['field' => $field]); $script->setUpsert($defaultData); $script->setId($id); + $script->setScriptedUpsert(true); $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); $bulk->addAction($action); @@ -617,6 +618,7 @@ public function testScriptUpsert(): void $script = new Script('if ( !ctx._source.sub_field.contains(params) ) ctx._source.sub_field.add(params)', $subField); $script->setUpsert($defaultData); $script->setId($id); + $script->setScriptedUpsert(true); $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); $bulk->addAction($action); @@ -625,6 +627,7 @@ public function testScriptUpsert(): void $script = new Script('if ( !ctx._source.sub_field_2.contains(params) ) ctx._source.sub_field_2.add(params)', $subField2); $script->setUpsert($defaultData); $script->setId($id); + $script->setScriptedUpsert(true); $action = AbstractDocument::create($script, Action::OP_TYPE_UPDATE); $bulk->addAction($action); From d123d4b3dee3e31cde5f1151bb13c850a10f6653 Mon Sep 17 00:00:00 2001 From: Diogo Sgrillo Date: Mon, 4 Oct 2021 10:34:30 +0200 Subject: [PATCH 14/14] update changelog accordingly --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ab8005c60..1b2b847745 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * Added `Elastica\Aggregation\NormalizeAggregation` [#1956](https://github.com/ruflin/Elastica/pull/1956) * Added `Elastica\Suggest\Phrase::addDirectGenerator` to align with ES specification [#1964](https://github.com/ruflin/Elastica/pull/1964) * Added support for `psr/log` 2.0 and 3.0 [#1971](https://github.com/ruflin/Elastica/pull/1971) +* Added support for scripted upsert on bulk requests [#1974](https://github.com/ruflin/Elastica/pull/1974) ### Changed * Updated `php-cs-fixer` to `2.18.6` [#1955](https://github.com/ruflin/Elastica/pull/1955) * Updated `php-cs-fixer` to `3.0.0` [#1959](https://github.com/ruflin/Elastica/pull/1959) @@ -19,7 +20,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Removed ### Fixed * Fixed the assignment of `version`, `seq_no` and `primary_term` on `getDocument` [#1973](https://github.com/ruflin/Elastica/pull/1973) -* Fixed the upsert scripts on bulk requests [#1974](https://github.com/ruflin/Elastica/pull/1974) ### Security ## [7.1.1](https://github.com/ruflin/Elastica/compare/7.1.0...7.1.1)