# Using php client bulk() - ERROR: 400 parse\_exception: Failed to derive xcontent

**URL:** https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557
**Category:** Elasticsearch
**Created:** [April 16, 2016, 7:06am UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557 "2016-04-16T07:06:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)
#### Post date: [April 16, 2016, 7:06am UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557/1 "2016-04-16T07:06:09Z")

</div>

Using the 2.0 php client bulk() I'm getting "ERROR: 400 parse\_exception: Failed to derive xcontent" on some documents. The linked document below consistently hits this exception. My ES instance is running 2.2.0. Before the bulk() is called I can see the document structure in php, it's like the other documents before it in the bulk queue, but this hits the exception. What's interesting though, is if I limit my bulk() batch to just 1 document in each bulk(), the document array exists, then after the exception the document array passed to bulk() is empty. The php client is emptying the array for some reason.

The document, however, is being created. Seems like I can simply ignore the error?

In Sense I can create the document no problem, no error.

I see a lot of posts on this Failed to derive xcontent, but nothing seems to apply to my case that I've found so far.

The forum wouldn't allow me to insert the document in question into my post here, which is pretty large, so here's a link: [https://www.ezrackbuilder.com/large-document.txt](https://www.ezrackbuilder.com/large-document.txt)

---

<div class="post-metadata">

### Author: ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)
#### Post date: [April 16, 2016, 4:44pm UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557/2 "2016-04-16T16:44:46Z")

</div>

Wish I could delete a post. There was an error in our php script that was causing this issue. No problem with php client or ES in my case.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [April 17, 2016, 3:00am UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557/3 "2016-04-17T03:00:26Z")

</div>

What was the error? It might help someone in the future 🙂

---

<div class="post-metadata">

### Author: ![Zelfapp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zelfapp/32/108888_2.png) [@Zelfapp](https://discuss.elastic.co/u/Zelfapp)
#### Post date: [April 17, 2016, 5:08am UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557/4 "2016-04-17T05:08:16Z")

</div>

Sure. So my issue was this:

We were following the concept on the [https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/\_indexing\_documents.html](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_indexing_documents.html) bulk indexing in batches, which is below.

However, we stupidly had the `// Send the last batch if it exists` code inside of our foreach loop. So, when our foreach loop hit it's max documents before calling bulk, the documents had just been emptied out by the other call. If you attempt to bulk empty docs, you get the error we were seeing.

```
$params = ['body' => []];

for ($i = 1; $i <= 1234567; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
            '_type' => 'my_type',
            '_id' => $i
        ]
    ];

    $params['body'][] = [
        'my_field' => 'my_value',
        'second_field' => 'some more values'
    ];

    // Every 1000 documents stop and send the bulk request
    if ($i % 1000 == 0) {
        $responses = $client->bulk($params);

        // erase the old bulk request
        $params = ['body' => []];

        // unset the bulk response when you are done to save memory
        unset($responses);
    }
}

// Send the last batch if it exists
if (!empty($params['body'])) {
    $responses = $client->bulk($params);
}
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 5, 2017, 10:58pm UTC](https://discuss.elastic.co/t/using-php-client-bulk-error-400-parse-exception-failed-to-derive-xcontent/47557/5 "2017-07-05T22:58:45Z")

</div>


