# Bulk indexing document without providing id using php api

**URL:** <https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521>\
**Category:** Elasticsearch\
**Created:** [July 14, 2015, 1:16pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521 "2015-07-14T13:16:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![rabeetwaqar](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rabeetwaqar](https://discuss.elastic.co/u/rabeetwaqar)\
**Post date:** [July 14, 2015, 1:16pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/1 "2015-07-14T13:16:32Z")

</div>

Bulk indexing document without providing id using php api?

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 1:22pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/2 "2015-07-14T13:22:45Z")

</div>

Disclaimer: I am not a php developer

From looking at the [php documentation](https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_indexing_operations.html#_bulk_indexing) I would try following the example given in the first code block but remove the line which sets the `_id` and Elasticsearch should generate the id for you.

---

<div class="post-metadata">

**Author:** ![rabeetwaqar](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rabeetwaqar](https://discuss.elastic.co/u/rabeetwaqar)\
**Post date:** [July 14, 2015, 1:25pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/3 "2015-07-14T13:25:15Z")

</div>

if i remove the \_id key it gives me this error,

{"error":"IllegalArgumentException[Malformed action/metadata line [1], expected START\_OBJECT or END\_OBJECT but found [START\_ARRAY]]","status":500}

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [July 14, 2015, 1:26pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/4 "2015-07-14T13:26:08Z")

</div>

Then I don't know, sorry.

Hopefully someone else can help you.

---

<div class="post-metadata">

**Author:** ![rabeetwaqar](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rabeetwaqar](https://discuss.elastic.co/u/rabeetwaqar)\
**Post date:** [July 14, 2015, 1:30pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/5 "2015-07-14T13:30:58Z")

</div>

@magnusbaeck Can you please help me out with this?  
Thanks

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 14, 2015, 1:43pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/6 "2015-07-14T13:43:00Z")

</div>

This is my standard template for bulk indexing via the PHP api:

```php
$params = [];

for ($i = 0; $i <= 100000; $i++) {
    $params['body'][] = array(
        'index' => array(
            '_index' => 'my_index',
            '_type' => 'my_type'
            //'_id' => $i // <-- commented out for your use-case
        )
    );

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

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

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

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

```

If you paste your code we could have a look at that too. As Colin said, you basically just need to remove the `_id` field, so if you are getting a parse exception something else is wrong with the syntax...

---

<div class="post-metadata">

**Author:** ![rabeetwaqar](https://avatars.discourse-cdn.com/v4/letter/r/b2d939/32.png) [@rabeetwaqar](https://discuss.elastic.co/u/rabeetwaqar)\
**Post date:** [July 15, 2015, 6:54am UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/7 "2015-07-15T06:54:34Z")

</div>

Thanks it worked 🙂

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [July 20, 2015, 2:04pm UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/8 "2015-07-20T14:04:18Z")

</div>

Great, happy to help 🙂

---

<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 6, 2017, 12:00am UTC](https://discuss.elastic.co/t/bulk-indexing-document-without-providing-id-using-php-api/25521/9 "2017-07-06T00:00:21Z")

</div>


