# Race condition when removing, indexing and counting data by using the JAVA API?

**URL:** <https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190>\
**Category:** Elasticsearch\
**Created:** [June 9, 2015, 7:50am UTC](https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190 "2015-06-09T07:50:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![niba](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@niba](https://discuss.elastic.co/u/niba)\
**Post date:** [June 9, 2015, 7:50am UTC](https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190/1 "2015-06-09T07:50:56Z")

</div>

I'm creating a little wrapper around the elasticsearch java api and found something weird. In my code I have the following sequence of execution:

1. Remove index

2.Create index and index some data with bulk request

```
bulkRequest.add(IndexRequest)
bulkRequest.execute().get();

```

3.And finally get count of records:

```
.getClient().prepareCount(indexes);

```

The problem is that the count always shows 0 records. To make it working I have to add Thread.Sleep between inserting/indexing data and getting count. I thought that get executes when operation is completed. Is it normally behaviour here? Should I wait a little more to give time to indexing data before execute count?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [June 9, 2015, 8:49am UTC](https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190/2 "2015-06-09T08:49:50Z")

</div>

I believe a get operation for a single document by id is always consistent but that counting documents counts as a query and therefore won't be correct until the index has been [refreshed](https://www.elastic.co/guide/en/elasticsearch/guide/current/near-real-time.html).

---

<div class="post-metadata">

**Author:** ![niba](https://avatars.discourse-cdn.com/v4/letter/n/a5b964/32.png) [@niba](https://discuss.elastic.co/u/niba)\
**Post date:** [June 9, 2015, 10:25am UTC](https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190/3 "2015-06-09T10:25:15Z")

</div>

You were right. Thanks!

---

<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:** [July 6, 2017, 12:08am UTC](https://discuss.elastic.co/t/race-condition-when-removing-indexing-and-counting-data-by-using-the-java-api/2190/4 "2017-07-06T00:08:53Z")

</div>


