elastic / elasticsearch-php

Official PHP client for Elasticsearch.
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
MIT License
5.26k stars 966 forks source link

The array structure of a batch index is confusing #1261

Closed RoRui closed 1 year ago

RoRui commented 1 year ago

That's what the official document says: Bulk indexing with PHP arrays.

for($i = 0; $i < 100; $i++) {
    $params['body'][] = [
        'index' => [
            '_index' => 'my_index',
        ]
    ];

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

$responses = $client->bulk($params);

The above array structure is confusing, so why not do it:

for($i = 0; $i < 100; $i++) {
    $params[] = [
        'index' => [
            '_index' => 'my_index',
            '_id' => $i
        ],
       'body' => [
        'my_field'     => 'my_value',
        'second_field' => 'some more values'
       ]
    ];
}

$responses = $client->bulk($params);
ezimuel commented 1 year ago

@rory5515 you cannot use your solution since we need to create an array of operations in the body field. Each operations has two rows, the first is the operation type (e.g. index) and the second is the document data. If you see the HTTP Bulk API specification here, you can see why we needed to do that. Let me know if you need more help, thanks.

murilolivorato commented 1 year ago

I think that is confusint to the structure of arrays