Skip to content

Commit cad5ec4

Browse files
committed
Fixed Response::isOk() to work better with bulk update api
1 parent c9f41fa commit cad5ec4

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

changes.txt

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
CHANGES
22

3+
2014-10-10
4+
- Fixed Response::isOk() to work better with bulk update api
5+
36
2014-10-05
47
- ResultSet creation moved to static ResultSet::create() method #690
58
- Accept an options array at Type::updateDocument() #686

lib/Elastica/Response.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,17 @@ public function isOk()
140140
}
141141
return false;
142142
}
143+
143144
if (isset($data['items'])) {
145+
if (isset($data['errors']) && true === $data['errors']) {
146+
return false;
147+
}
148+
144149
foreach ($data['items'] as $item) {
145-
if (false == $item['index']['ok']) {
150+
if (isset($item['index']['ok']) && false == $item['index']['ok']) {
151+
return false;
152+
153+
} elseif (isset($item['index']['status']) && ($item['index']['status'] < 200 || $item['index']['status'] >= 300)) {
146154
return false;
147155
}
148156
}

test/lib/Elastica/Test/ResponseTest.php

+81
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Elastica\Query;
77
use Elastica\Query\MatchAll;
88
use Elastica\Request;
9+
use Elastica\Response;
910
use Elastica\Type\Mapping;
1011
use Elastica\Test\Base as BaseTest;
1112

@@ -79,6 +80,86 @@ public function testIsOkMultiple()
7980
$this->assertTrue($response->isOk());
8081
}
8182

83+
public function testIsOkBulkWithErrorsField()
84+
{
85+
$response = new Response(json_encode(array(
86+
'took' => 213,
87+
'errors' => false,
88+
'items' => array(
89+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
90+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
91+
)
92+
)));
93+
94+
$this->assertTrue($response->isOk());
95+
}
96+
97+
public function testIsNotOkBulkWithErrorsField()
98+
{
99+
$response = new Response(json_encode(array(
100+
'took' => 213,
101+
'errors' => true,
102+
'items' => array(
103+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
104+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
105+
)
106+
)));
107+
108+
$this->assertFalse($response->isOk());
109+
}
110+
111+
public function testIsOkBulkItemsWithOkField()
112+
{
113+
$response = new Response(json_encode(array(
114+
'took' => 213,
115+
'items' => array(
116+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
117+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => true)),
118+
)
119+
)));
120+
121+
$this->assertTrue($response->isOk());
122+
}
123+
124+
public function testIsNotOkBulkItemsWithOkField()
125+
{
126+
$response = new Response(json_encode(array(
127+
'took' => 213,
128+
'items' => array(
129+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'ok' => true)),
130+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'ok' => false)),
131+
)
132+
)));
133+
134+
$this->assertFalse($response->isOk());
135+
}
136+
137+
public function testIsOkBulkItemsWithStatusField()
138+
{
139+
$response = new Response(json_encode(array(
140+
'took' => 213,
141+
'items' => array(
142+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
143+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 200)),
144+
)
145+
)));
146+
147+
$this->assertTrue($response->isOk());
148+
}
149+
150+
public function testIsNotOkBulkItemsWithStatusField()
151+
{
152+
$response = new Response(json_encode(array(
153+
'took' => 213,
154+
'items' => array(
155+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707891', '_version' => 4, 'status' => 200)),
156+
array('index' => array('_index' => 'rohlik', '_type' => 'grocery', '_id' => '707893', '_version' => 4, 'status' => 301)),
157+
)
158+
)));
159+
160+
$this->assertFalse($response->isOk());
161+
}
162+
82163
public function testGetDataEmpty()
83164
{
84165
$index = $this->_createIndex();

0 commit comments

Comments
 (0)