From 336f5a5782eea692e2c59878f60394461fcd2540 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Fri, 21 Nov 2014 16:18:08 +0200 Subject: [PATCH 1/2] bulk method now executes all operations --- Client/Connection.php | 38 +++++++++++++--------- Tests/Functional/Client/ConnectionTest.php | 38 ++++++++++++++++++++++ 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/Client/Connection.php b/Client/Connection.php index 4372f08b..ac637cbc 100644 --- a/Client/Connection.php +++ b/Client/Connection.php @@ -89,28 +89,34 @@ public function getClient() */ public function bulk($operation, $type, array $query) { + if (!in_array($operation, ['index', 'create', 'update', 'delete'])) { + throw new \InvalidArgumentException('Wrong bulk operation selected'); + } + + $this->bulkQueries['body'][] = [ + $operation => array_filter( + [ + '_index' => $this->getIndexName(), + '_type' => $type, + '_id' => isset($query['_id']) ? $query['_id'] : null, + '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null, + '_parent' => isset($query['_parent']) ? $query['_parent'] : null, + ] + ), + ]; + unset($query['_id'], $query['_ttl'], $query['_parent']); + switch ($operation) { case 'index': case 'create': - case 'update': - - $this->bulkQueries['body'][] = [ - $operation => [ - '_index' => $this->getIndexName(), - '_type' => $type, - '_id' => isset($query['_id']) ? $query['_id'] : null, - '_ttl' => isset($query['_ttl']) ? $query['_ttl'] : null, - '_parent' => isset($query['_parent']) ? $query['_parent'] : null - ] - ]; - - // Unset reserved keys. - unset($query['_id'], $query['_ttl'], $query['_parent']); - $this->bulkQueries['body'][] = $query; break; + case 'update': + $this->bulkQueries['body'][] = ['doc' => $query]; + break; default: - throw new \InvalidArgumentException('Wrong bulk operation selected'); + // Do nothing. + break; } } diff --git a/Tests/Functional/Client/ConnectionTest.php b/Tests/Functional/Client/ConnectionTest.php index 064c9bba..a91be086 100644 --- a/Tests/Functional/Client/ConnectionTest.php +++ b/Tests/Functional/Client/ConnectionTest.php @@ -63,4 +63,42 @@ public function testUpdateMapping() $status = $connection->updateMapping(); $this->assertTrue($status, 'Mapping should be updated'); } + + /** + * Tests bulk operations. + */ + public function testBulk() + { + $manager = $this->getManager(); + $connection = $manager->getConnection(); + $repository = $manager->getRepository('ONGRTestingBundle:Product'); + + // CREATE. + $connection->bulk('create', 'product', ['_id' => 'baz', 'title' => 'Awesomo']); + $connection->commit(); + + $product = $repository->find('baz'); + $this->assertEquals('Awesomo', $product->title, 'Document should be created.'); + + // UPDATE. + $connection->bulk('update', 'product', ['_id' => 'baz', 'title' => 'Improved awesomo']); + $connection->commit(); + + $product = $repository->find('baz'); + $this->assertEquals('Improved awesomo', $product->title, 'Document should be updated.'); + + // INDEX. + $connection->bulk('index', 'product', ['_id' => 'baz', 'title' => 'Indexed awesomo']); + $connection->commit(); + + $product = $repository->find('baz'); + $this->assertEquals('Indexed awesomo', $product->title, 'Document should be indexed.'); + + // DELETE. + $connection->bulk('delete', 'product', ['_id' => 'baz']); + $connection->commit(); + + $this->setExpectedException('Elasticsearch\Common\Exceptions\Missing404Exception'); + $product = $repository->find('baz'); + } } From 59e34354d817b7ebda4b96c1395bd856e1a4dcc8 Mon Sep 17 00:00:00 2001 From: Martynas Sudintas Date: Thu, 27 Nov 2014 09:50:19 +0200 Subject: [PATCH 2/2] Added delete case in switch in bulk method --- Client/Connection.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Client/Connection.php b/Client/Connection.php index ac637cbc..46e40521 100644 --- a/Client/Connection.php +++ b/Client/Connection.php @@ -114,6 +114,8 @@ public function bulk($operation, $type, array $query) case 'update': $this->bulkQueries['body'][] = ['doc' => $query]; break; + case 'delete': + // Body for delete opertation is not needed to apply. default: // Do nothing. break;