Bulk indexing document without providing id using php api


(Rabeet waqar) #1

Bulk indexing document without providing id using php api?


(Colin Goodheart-Smithe) #2

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.


(Rabeet waqar) #3

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}


(Colin Goodheart-Smithe) #4

Then I don't know, sorry.

Hopefully someone else can help you.


(Rabeet waqar) #5

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


(Zachary Tong) #6

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


(Rabeet waqar) #7

Thanks it worked :slight_smile:


(Zachary Tong) #8

Great, happy to help :slight_smile:


(system) #9