Elastic search PHP: wait for bulk indexation to finish

Hello!

My question is very simple yet I can't find an answer for it:

Situation:

I am creating search feature for a small online store that uses elastic search PHP library.

I am using client->bulk() operation to index all the products from the database, however I need to display how many items have been indexed out of the total products available.

When I use the client->count() operation to get the number of items, it is still 0 until after a few seconds, then it gives me the correct count, so I image the elastic search indexation doesn't complete after I run the bulk() operation.

Question:
How can I tell elastic search to "wait" for the bulk indexation to actually finish when I call bulk() so when I call the count() method it actually returns the number of items that have been indexed?

In the context of an integration test, you can call the refresh API

Hello David, thanks for the reply.

I have read about the refresh API and I have tried the following:

$params = array();
        
foreach ($productsFromDb as $product) {
    $params['body'][] = array(
        'index' => array(
            '_index' => 'products',
            '_type' => 'product',
        ),
        'refresh' => true,
    );

    $params['body'][] = $product;
}

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

and right after I run the following:

$params2 = array(
    'index' => 'products',
    'type' => 'product',
    'body' => array(
        'query' => array(
            'match_all' => new stdClass(),
        ),
    ),
);

$count = $client->count($params2);

print_r($count['count']);

but the count is still 0 :frowning:

Only after a few seconds, the count gives the correct number of products.

Any thoughts ? Thanks.

I'd call the refresh API after the bulk call.

Note that I don't know the php client and if it uses an asynchronous call or not.

1 Like

Thank you, problem solved!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.