# Auto generated id

**URL:** <https://discuss.elastic.co/t/auto-generated-id/4062>\
**Category:** Elasticsearch\
**Created:** [March 9, 2011, 8:19am UTC](https://discuss.elastic.co/t/auto-generated-id/4062 "2011-03-09T08:19:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://discuss.elastic.co/u/jason)\
**Post date:** [March 9, 2011, 8:19am UTC](https://discuss.elastic.co/t/auto-generated-id/4062/1 "2011-03-09T08:19:09Z")

</div>

Hi everyone,

I went over the Java code examples, and I saw that whenver  
IndexResponse is created, the id value is always code-generated, i.e.

```
      for (int j = 0; j < BATCH; j++) {
                counter++;

```

request.add(Requests.indexRequest("test").type("type1").id(Integer.toString(counter))  
.source(source(Integer.toString(counter),  
sValues[counter % sValues.length], lValues[counter %  
lValues.length])));  
}

However, I would like to use something other than incrementing  
counter, because in my project I have to index new document within  
listener event, so I want to avoid using global variables. Is there  
any way to create new id on the fly using Java classes from ES  
library?

Thanks,  
Eugene.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [March 9, 2011, 8:24am UTC](https://discuss.elastic.co/t/auto-generated-id/4062/2 "2011-03-09T08:24:38Z")

</div>

Just provide whatever id you want, if you don't provide one, then one will be generated for you.  
On Wednesday, March 9, 2011 at 10:19 AM, eugene wrote:

> Hi everyone,
> 
> I went over the Java code examples, and I saw that whenver  
> IndexResponse is created, the id value is always code-generated, i.e.
> 
> for (int j = 0; j \< BATCH; j++) {  
> counter++;
> 
> request.add(Requests.indexRequest("test").type("type1").id(Integer.toString(counter))  
> .source(source(Integer.toString(counter),  
> sValues[counter % sValues.length], lValues[counter %  
> lValues.length])));  
> }
> 
> However, I would like to use something other than incrementing  
> counter, because in my project I have to index new document within  
> listener event, so I want to avoid using global variables. Is there  
> any way to create new id on the fly using Java classes from ES  
> library?
> 
> Thanks,  
> Eugene.

---

<div class="post-metadata">

**Author:** ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://discuss.elastic.co/u/jason)\
**Post date:** [March 9, 2011, 8:44am UTC](https://discuss.elastic.co/t/auto-generated-id/4062/3 "2011-03-09T08:44:52Z")

</div>

Thanks Shay, that worked! From your experience, is it better to put  
muliple documents under one index, or create more indices for each  
document (which I doubt is a good idea because you may run out of  
space), or different types per single index for each new json entry?

On Mar 9, 12:24 am, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> Just provide whatever id you want, if you don't provide one, then one will be generated for you.
> 
> On Wednesday, March 9, 2011 at 10:19 AM, eugene wrote:
> 
> > Hi everyone,
> 
> > I went over the Java code examples, and I saw that whenver  
> > IndexResponse is created, the id value is always code-generated, i.e.
> 
> > for (int j = 0; j \< BATCH; j++) {  
> > counter++;
> 
> > request.add(Requests.indexRequest("test").type("type1").id(Integer.toString­(counter))  
> > .source(source(Integer.toString(counter),  
> > sValues[counter % sValues.length], lValues[counter %  
> > lValues.length])));  
> > }
> 
> > However, I would like to use something other than incrementing  
> > counter, because in my project I have to index new document within  
> > listener event, so I want to avoid using global variables. Is there  
> > any way to create new id on the fly using Java classes from ES  
> > library?
> 
> > Thanks,  
> > Eugene.- Hide quoted text -
> 
> - Show quoted text -

---

<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, 4:10am UTC](https://discuss.elastic.co/t/auto-generated-id/4062/4 "2017-07-06T04:10:29Z")

</div>


