Bulk indexing document without providing id using php api

Bulk indexing document without providing id using php api?

Disclaimer: I am not a php developer

From looking at the php documentation 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.

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}

Then I don't know, sorry.

Hopefully someone else can help you.

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

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

$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...

1 Like

Thanks it worked :slight_smile:

Great, happy to help :slight_smile: