Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Response::isOk() to work better with bulk update api #702

Merged
merged 1 commit into from
Oct 13, 2014
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
3 changes: 3 additions & 0 deletions changes.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
CHANGES

2014-10-10
- Fixed Response::isOk() to work better with bulk update api

2014-10-05
- ResultSet creation moved to static ResultSet::create() method #690
- Accept an options array at Type::updateDocument() #686
Expand Down
10 changes: 9 additions & 1 deletion lib/Elastica/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,17 @@ public function isOk()
}
return false;
}

if (isset($data['items'])) {
if (isset($data['errors']) && true === $data['errors']) {
return false;
}

foreach ($data['items'] as $item) {
if (false == $item['index']['ok']) {
if (isset($item['index']['ok']) && false == $item['index']['ok']) {
return false;

} elseif (isset($item['index']['status']) && ($item['index']['status'] < 200 || $item['index']['status'] >= 300)) {
return false;
}
}
Expand Down
81 changes: 81 additions & 0 deletions test/lib/Elastica/Test/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elastica\Query;
use Elastica\Query\MatchAll;
use Elastica\Request;
use Elastica\Response;
use Elastica\Type\Mapping;
use Elastica\Test\Base as BaseTest;

Expand Down Expand Up @@ -79,6 +80,86 @@ public function testIsOkMultiple()
$this->assertTrue($response->isOk());
}

public function testIsOkBulkWithErrorsField()
{
$response = new Response(json_encode(array(
'took' => 213,
'errors' => false,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkWithErrorsField()
{
$response = new Response(json_encode(array(
'took' => 213,
'errors' => true,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertFalse($response->isOk());
}

public function testIsOkBulkItemsWithOkField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => true)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkItemsWithOkField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => false)),
)
)));

$this->assertFalse($response->isOk());
}

public function testIsOkBulkItemsWithStatusField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
)
)));

$this->assertTrue($response->isOk());
}

public function testIsNotOkBulkItemsWithStatusField()
{
$response = new Response(json_encode(array(
'took' => 213,
'items' => array(
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 301)),
)
)));

$this->assertFalse($response->isOk());
}

public function testGetDataEmpty()
{
$index = $this->_createIndex();
Expand Down