Skip to content

Commit

Permalink
Merge pull request #472 from mvar/bulk_update_fix
Browse files Browse the repository at this point in the history
Pass raw update query to bulk body
  • Loading branch information
Mantas Urnieža committed Nov 26, 2015
2 parents 4aef31c + 0195eeb commit 2085cb2
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 3 deletions.
4 changes: 1 addition & 3 deletions Service/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,8 @@ public function bulk($operation, $type, array $query)
switch ($operation) {
case 'index':
case 'create':
$this->bulkQueries['body'][] = $query;
break;
case 'update':
$this->bulkQueries['body'][] = ['doc' => $query];
$this->bulkQueries['body'][] = $query;
break;
case 'delete':
// Body for delete operation is not needed to apply.
Expand Down
111 changes: 111 additions & 0 deletions Tests/Unit/Service/ManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchBundle\Tests\Unit\DSL\Aggregation;

use ONGR\ElasticsearchBundle\Service\Manager;

class ManagerTest extends \PHPUnit_Framework_TestCase
{
/**
* Data provider for testBulk()
*
* @return array[]
*
* @todo Add more test cases to cover all logic
*/
public function getTestBulkData()
{
return [
[
'expected' => [
'body' => [
[
'update' => ['_index' => 'test', '_type' => 'product'],
],
[
'doc' => ['title' => 'Sample'],
],
],
],
'calls' => [
[
'update',
'product',
[
'doc' => [
'title' => 'Sample',
],
],
],
],
],
[
'expected' => [
'body' => [
[
'update' => ['_index' => 'test', '_type' => 'product'],
],
[
'script' => 'ctx._source.counter += count',
'params' => ['count' => '4'],
],
],
],
'calls' => [
[
'update',
'product',
[
'script' => 'ctx._source.counter += count',
'params' => ['count' => '4'],
],
],
],
],
];
}

/**
* Test if manager builds correct bulk structure
*
* @param array $expected
* @param array $calls
*
* @dataProvider getTestBulkData()
*/
public function testBulk($expected, $calls)
{
$indices = $this->getMock('Elasticsearch\Namespaces\IndicesNamespace', [], [], '', false);

$esClient = $this->getMock('Elasticsearch\Client', [], [], '', false);
$esClient->expects($this->once())->method('bulk')->with($expected);
$esClient->expects($this->any())->method('indices')->will($this->returnValue($indices));

$metadataCollector = $this->getMockBuilder('ONGR\ElasticsearchBundle\Mapping\MetadataCollector')
->disableOriginalConstructor()
->getMock();

$converter = $this->getMockBuilder('ONGR\ElasticsearchBundle\Result\Converter')
->disableOriginalConstructor()
->getMock();

$config = ['readonly' => false];

$manager = new Manager('test', $config, $esClient, ['index' => 'test'], $metadataCollector, $converter);

foreach ($calls as list($operation, $type, $query)) {
$manager->bulk($operation, $type, $query);
}

$manager->commit();
}
}

0 comments on commit 2085cb2

Please sign in to comment.