elasticsearch-php
elasticsearch-php copied to clipboard
Throw an exception on unsuccessful bulk operations
Summary of problem or feature request
Bulk API returns 200 status code even in case some operations have failed, but the response body contains a flag (errors=false
), which can be used to identify the overall request result. See more details in the official ES documentation.
This is very inconvenient to parse the response in every project and check if there were errors or not. It would be really nice to get some generic exception when one or more operations did not complete successfully. It can be configurable if this matters.
Code snippet of problem
Here is an example of unsuccessful bulk indexing:
{
"took":1,
"errors":true,
"items":[
{
"index":{
"_index":"books",
"_type":"_doc",
"_id":"2",
"status":400,
"error":{
"type":"mapper_parsing_exception",
"reason":"failed to parse field [published] of type [date] in document with id '2'. Preview of field's value: '2020'",
"caused_by":{
"type":"illegal_argument_exception",
"reason":"failed to parse date field [2020] with format [yyyy-MM-dd]",
"caused_by":{
"type":"date_time_parse_exception",
"reason":"Text '2020' could not be parsed at index 4"
}
}
}
}
}
]
}
@babenkoivan Thanks for your feedback. I think this is a good idea but I need to find a nice way to prevent BC break. Maybe, as you suggested this can be an option, disabled by default.
Hope this is fixed soon as not throwing an error results in a debugging hell. Thanks!
Something like
namespace Elasticsearch\Next;
/**
* @deprecated To be removed in 8.0
*/
class Client extends \Elasticsearch\Client
{
public function bulk($body) {
$response = parent::bulk($body);
if ($response['errors']) {
throw BulkException::fromResponse($response);
}
return $response;
}
}