Skip to content

Commit 8d8c0f2

Browse files
committed
Append newline to bulk string requests, reject invalid types
If the user provides a string as the complete bulk request, it must end with a newline for Elasticsearch to be happy. This is something we can check and fix for the user. We should also throw an exception if the user provides anything other than an array, traversable object or string.
1 parent 562ec31 commit 8d8c0f2

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

src/Elasticsearch/Endpoints/Bulk.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Elasticsearch\Endpoints;
66

7+
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
78
use Elasticsearch\Serializers\SerializerInterface;
8-
use Elasticsearch\Transport;
99

1010
/**
1111
* Class Bulk
@@ -41,8 +41,13 @@ public function setBody($body)
4141
foreach ($body as $item) {
4242
$this->body .= $this->serializer->serialize($item) . "\n";
4343
}
44-
} else {
44+
} elseif (is_string($body)) {
4545
$this->body = $body;
46+
if (substr($body, -1) != "\n") {
47+
$this->body .= "\n";
48+
}
49+
} else {
50+
throw new InvalidArgumentException("Bulk body must be an array, traversable object or string");
4651
}
4752

4853
return $this;

util/elasticsearch

0 commit comments

Comments
 (0)