# How to assign \_id field to a document field on create Or fetch the highest document id in a index

**URL:** <https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561>\
**Category:** Elasticsearch\
**Created:** [September 14, 2017, 2:35pm UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561 "2017-09-14T14:35:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paddy\_Mahadeva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paddy_mahadeva/32/44225_2.png) [@Paddy\_Mahadeva](https://discuss.elastic.co/u/Paddy_Mahadeva)\
**Post date:** [September 14, 2017, 2:35pm UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561/1 "2017-09-14T14:35:19Z")

</div>

Hi,

Is there a way for me to assign the \_id that is created when I create a document in Elasticsearch directly to a field in my document when its getting created?

I am currently trying to find the max \_id of the documents in an index and incrementing it by 1 before I create the next document.

But currently, I am not getting any way to fetch the max \_id (or \_uid) of the documents in my index. So that idea is not working out.

Can someone please suggest, how I can do either one of these?

Thanks  
Paddy  
_newbie alert_

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 14, 2017, 3:29pm UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561/2 "2017-09-14T15:29:55Z")

</div>

Its not 100% clear from your post if you are using autogenerated IDs (i.e. you are not specifying the id when you create the document) though are suspect you are not using auto generated ids. But if you are, the auto generated ids are not numeric so getting the max id would not make sense.

To find the max value of a field you can generally use the [max aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-aggregations-metrics-max-aggregation.html), however the max aggregation only works on numeric fields and the `_id` in Elasticsearch is interpreted as a String and stored as such (actually this is a bit of a lie as its stored in a more specialised way but it is closest to a String or `keyword` type for the purposes of this explanation) so you can't do a max aggregation on the `_id`.

However, if you are setting the `_id` yourself you can always add your own `my_id` field which is mapped as a long type and use that to find the max version and rely on your client application to always send documents where the `_id` matches the `my_id`.

Why are you wanting to do this incrementing of the id before you index the next document? It is to simulate a counting sequential id? If so there are some things to consider:

1. Making a request to find the max id requires a call to Elasticsearch and for Elasticsearch to scan through all id values for all documents in your index. Doing this on every index request will impact indexing performance, as well as causing a lot of requests to Elasticsearch which may impact the performance of your other queries too. If your application is the only application indexing documents into this index I would suggest that you keep a counter of the last doc id in you client application and increment it from there.
2. If you are wanting to do this because you might have loads of client applications indexing documents independently then as well as the point above you could run into concurrency problems where client A gets the max id and goes to write the next document with the increased id, but before client A writes that new document, client B gets the max Id and will get the old max ID meaning that it will overwrite the document A is about to write. The only way to reliably avoid this is either to use auto-generated IDs (i.e. let Elasticsearch pick the id) or to use a field (or combination of fields) in your document which unique across all the documents you are going to write.

Hope that helps

---

<div class="post-metadata">

**Author:** ![Paddy\_Mahadeva](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paddy_mahadeva/32/44225_2.png) [@Paddy\_Mahadeva](https://discuss.elastic.co/u/Paddy_Mahadeva)\
**Post date:** [September 15, 2017, 5:03am UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561/3 "2017-09-15T05:03:48Z")

</div>

Hi @colings86 ,

That was a really clean answer to my question. Apologies not giving clarity on my question.

I have only a single point to create documents, this is enforced in the application using it, to avoid concurrency issues. The reason I need the max id is that I need the id to be sequential as the order of creation is very critical for the application. I realized that the \_id is not a number for me to do max aggr while I was testing my application, hence I asked how I can assign value to a field on creation time.

So I think I will have to take the first approach you suggested and create the id in my application. So two request have go to ES instance and I have to take care of any concurrency issue because of that.

Subsequently I would like to ask

1. Can I ensure that documents are not created in case there is an existing document with the given id if it is already existing(through create or update calls)?

Thanks  
Paddy

---

<div class="post-metadata">

**Author:** ![colings86](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/colings86/32/44960_2.png) [@colings86](https://discuss.elastic.co/u/colings86)\
**Post date:** [September 15, 2017, 7:54am UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561/4 "2017-09-15T07:54:50Z")

</div>

You can call `PUT {index}/{type}/{id}/_create` which will fail if a document with the same id already exists or create the document iff the document does not already exist. More information can be found here: [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-index\_.html#operation-type](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-index_.html#operation-type)

---

<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:** [October 13, 2017, 8:09am UTC](https://discuss.elastic.co/t/how-to-assign--id-field-to-a-document-field-on-create-or-fetch-the-highest-document-id-in-a-index/100561/5 "2017-10-13T08:09:42Z")

</div>

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