# Elastic search PHP: wait for bulk indexation to finish

**URL:** <https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394>\
**Category:** Elasticsearch\
**Created:** [April 2, 2018, 8:25am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394 "2018-04-02T08:25:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mike.active](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mike.active](https://discuss.elastic.co/u/mike.active)\
**Post date:** [April 2, 2018, 8:25am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/1 "2018-04-02T08:25:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2018, 8:44am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/2 "2018-04-02T08:44:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mike.active](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mike.active](https://discuss.elastic.co/u/mike.active)\
**Post date:** [April 2, 2018, 9:00am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/3 "2018-04-02T09:00:59Z")

</div>

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 ☹

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

Any thoughts ? Thanks.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 2, 2018, 9:38am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/4 "2018-04-02T09:38:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mike.active](https://avatars.discourse-cdn.com/v4/letter/m/bb73d2/32.png) [@mike.active](https://discuss.elastic.co/u/mike.active)\
**Post date:** [April 2, 2018, 9:49am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/5 "2018-04-02T09:49:08Z")

</div>

Thank you, problem solved!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 30, 2018, 9:49am UTC](https://discuss.elastic.co/t/elastic-search-php-wait-for-bulk-indexation-to-finish/126394/6 "2018-04-30T09:49:53Z")

</div>

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