# How to create autoincrement id in ES using java\_api in storm?

**URL:** <https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832>\
**Category:** Elasticsearch\
**Created:** [September 8, 2015, 10:39am UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832 "2015-09-08T10:39:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![SachinBigdata](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@SachinBigdata](https://discuss.elastic.co/u/SachinBigdata)\
**Post date:** [September 8, 2015, 10:39am UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/1 "2015-09-08T10:39:47Z")

</div>

Hi,

I am trying to create autoincrement id in ES in storm using TransportClient client. first i am checking the max id present in ES and then inserting existing max id +1 . But, its replicating same id for 3-4 times. Pelase help me if somebody would have created auto-increment id in ES using java API.

---

<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:** [September 8, 2015, 6:08pm UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/2 "2015-09-08T18:08:30Z")

</div>

There are a couple of problems with this.

- The probable reason for what you're seeing is that new documents only become searchable after a _refresh_. Refreshes take place periodically but can also be explicitly triggered (at a cost).
- Checking the highest value and incrementing it by one requires that there at any time is no more than one thread or process that inserts values, or that you have a global lock preventing more than one of doing it at a time.
- Unless you have a very low insertion rate these queries for each insert operation are going to be prohibitively expensive.

What problem are you trying to solve? Do you _really_ need this kind of auto-incrementing id? How are you going to use this field?

---

<div class="post-metadata">

**Author:** ![SachinBigdata](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@SachinBigdata](https://discuss.elastic.co/u/SachinBigdata)\
**Post date:** [September 9, 2015, 11:37am UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/3 "2015-09-09T11:37:13Z")

</div>

Thank you Mognus, I think i will have to rethink on autoincrement id, actually we need this kind of unique autoincrement id for searching purpose, search by ID functionality.

---

<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:** [September 9, 2015, 5:20pm UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/4 "2015-09-09T17:20:43Z")

</div>

Searching by id makes total sense, but I don't see why it has to be an auto-incrementing id. Is there anything wrong with the ids that Elasticsearch generates automatically?

---

<div class="post-metadata">

**Author:** ![whattheheck](https://avatars.discourse-cdn.com/v4/letter/w/7c8e57/32.png) [@whattheheck](https://discuss.elastic.co/u/whattheheck)\
**Post date:** [September 10, 2015, 8:00am UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/5 "2015-09-10T08:00:13Z")

</div>

To get the auto increment functionality you require  
I would suggest using the scripting mechanism  
Groovy script can do this atomic increment

---

<div class="post-metadata">

**Author:** ![SachinBigdata](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@SachinBigdata](https://discuss.elastic.co/u/SachinBigdata)\
**Post date:** [September 10, 2015, 1:31pm UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/6 "2015-09-10T13:31:21Z")

</div>

I am fine with ids generated by ES but it is not readable , is there a way i can make it readable ?  
autogenerated ids by ES are like ..AU-tUX0H6tLZvOocuO00 , AU-tUX0e6tLZvOocuO01

---

<div class="post-metadata">

**Author:** ![SachinBigdata](https://avatars.discourse-cdn.com/v4/letter/s/c2a13f/32.png) [@SachinBigdata](https://discuss.elastic.co/u/SachinBigdata)\
**Post date:** [September 10, 2015, 1:32pm UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/7 "2015-09-10T13:32:18Z")

</div>

@whattheheck,

Please elaborate and example please, Thank you.

---

<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 5, 2017, 11:51pm UTC](https://discuss.elastic.co/t/how-to-create-autoincrement-id-in-es-using-java-api-in-storm/28832/8 "2017-07-05T23:51:16Z")

</div>


