Skip to content

Commit 8cc651d

Browse files
committed
Add bulk method
1 parent a733288 commit 8cc651d

File tree

2 files changed

+47
-7
lines changed

2 files changed

+47
-7
lines changed

src/Client.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public function indexDocument(
8181
if ($options) {
8282
$uri .= '?' . http_build_query($options);
8383
}
84-
return $this->doRequest($this->createJsonRequest($method, $uri, $body));
84+
return $this->doRequest($this->createJsonRequest($method, $uri, json_encode($body)));
8585
}
8686

8787
public function existsDocument(string $index, string $id, string $type = '_doc'): Promise
@@ -177,7 +177,7 @@ public function search(array $query, string $indexOrIndices = null, array $optio
177177
if ($options) {
178178
$uri .= '?' . http_build_query($options);
179179
}
180-
return $this->doRequest($this->createJsonRequest($method, $uri, ['query' => $query]));
180+
return $this->doRequest($this->createJsonRequest($method, $uri, json_encode(['query' => $query])));
181181
}
182182

183183
public function count(string $index, array $options = [], array $query = null): Promise
@@ -189,20 +189,36 @@ public function count(string $index, array $options = [], array $query = null):
189189
if ($options) {
190190
$uri .= '?' . http_build_query($options);
191191
}
192-
$body = null;
193192
if (null !== $query) {
194-
$body = ['query' => $query];
193+
return $this->doRequest($this->createJsonRequest($method, $uri, json_encode(['query' => $query])));
195194
}
196-
return $this->doRequest($this->createJsonRequest($method, $uri, $body));
195+
return $this->doRequest($this->createJsonRequest($method, $uri));
196+
}
197+
198+
public function bulk(array $body, string $index = null, array $options = []): Promise
199+
{
200+
$method = 'POST';
201+
$uri = [$this->baseUri];
202+
if ($index) {
203+
$uri[] = urlencode($index);
204+
}
205+
$uri[] = '_bulk';
206+
$uri = implode('/', $uri);
207+
if ($options) {
208+
$uri .= '?' . http_build_query($options);
209+
}
210+
return $this->doRequest(
211+
$this->createJsonRequest($method, $uri, implode(PHP_EOL, array_map('json_encode', $body)) . PHP_EOL)
212+
);
197213
}
198214

199-
private function createJsonRequest(string $method, string $uri, array $body = null): Request
215+
private function createJsonRequest(string $method, string $uri, string $body = null): Request
200216
{
201217
$request = (new Request($uri, $method))
202218
->withHeader('Content-Type', 'application/json')
203219
->withHeader('Accept', 'application/json');
204220
if ($body) {
205-
$request = $request->withBody(json_encode($body));
221+
$request = $request->withBody($body);
206222
}
207223
return $request;
208224
}

tests/Integration/ClientTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,28 @@ public function testCountWithQuery(): void
272272
$this->assertIsArray($response);
273273
$this->assertEquals(1, $response['count']);
274274
}
275+
276+
public function testBulkIndex(): void
277+
{
278+
Promise\wait($this->client->createIndex(self::TEST_INDEX));
279+
$body = [];
280+
$responses = [];
281+
for ($i = 1; $i <= 1234; $i++) {
282+
$body[] = ['index' => ['_id' => '']];
283+
$body[] = ['test' => 'bulk', 'my_field' => 'my_value_' . $i];
284+
285+
// Every 100 documents stop and send the bulk request
286+
if ($i % 100 === 0) {
287+
$responses = Promise\wait($this->client->bulk($body, self::TEST_INDEX));
288+
$body = [];
289+
unset($responses);
290+
}
291+
}
292+
if (!empty($body)) {
293+
$responses = Promise\wait($this->client->bulk($body, self::TEST_INDEX));
294+
}
295+
296+
$this->assertIsArray($responses);
297+
$this->assertCount(34, $responses['items']);
298+
}
275299
}

0 commit comments

Comments
 (0)