From cad5ec49c4d75ed6815dafbc3e50e5f2f17c20d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Proch=C3=A1zka?= Date: Fri, 10 Oct 2014 11:08:35 +0200 Subject: [PATCH] Fixed Response::isOk() to work better with bulk update api --- changes.txt | 3 + lib/Elastica/Response.php | 10 ++- test/lib/Elastica/Test/ResponseTest.php | 81 +++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) diff --git a/changes.txt b/changes.txt index d33dc4a3b8..ccc26c5591 100644 --- a/changes.txt +++ b/changes.txt @@ -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 diff --git a/lib/Elastica/Response.php b/lib/Elastica/Response.php index 5c0ef24d85..cf5e6a1a29 100644 --- a/lib/Elastica/Response.php +++ b/lib/Elastica/Response.php @@ -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; } } diff --git a/test/lib/Elastica/Test/ResponseTest.php b/test/lib/Elastica/Test/ResponseTest.php index 1e026eae79..af8043be9a 100644 --- a/test/lib/Elastica/Test/ResponseTest.php +++ b/test/lib/Elastica/Test/ResponseTest.php @@ -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; @@ -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();