Skip to content
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
40 changes: 24 additions & 16 deletions Client/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,36 @@ 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;
case 'delete':
// Body for delete opertation is not needed to apply.
default:
throw new \InvalidArgumentException('Wrong bulk operation selected');
// Do nothing.
break;
}
}

Expand Down
38 changes: 38 additions & 0 deletions Tests/Functional/Client/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}