OdkoPP
OdkoPP

Reputation: 492

ElasticSearch - Bulk Indexing with refresh

I need to index about 1000 documents as fast as possible. I decided to use bulk function that works about 10 times faster than my original solution. I need to make refresh right after indexing ends to make documents searchable. In other situation i would use refresh parameter 'refresh' => true, but i cant make it work with bulk in PHP. I use code from official documentation.

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

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

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

What is the right way to use refresh in PHP bulk function?

Upvotes: 0

Views: 3680

Answers (2)

Yurii
Yurii

Reputation: 11

Just add

$params['refresh'] = true;

after your loop and before bulk insert.

So eventually the code would be

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

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

$params['refresh'] = true;
$responses = $client->bulk($params);

Upvotes: 1

OdkoPP
OdkoPP

Reputation: 492

I used fake update operation with refresh right after bulk

$params = [
    'index' => 'my_index',
    'type' => 'refresh',
    'id' => 'refresh',
    'refresh' => true,             // REFRESH
    'body' => []
];

$client->index($params);

This is not the best way to do it but the only that worked for me.

Upvotes: 3

Related Questions