# Define custom unique key

**URL:** <https://discuss.elastic.co/t/define-custom-unique-key/301642>\
**Category:** Elasticsearch\
**Created:** [April 5, 2022, 1:29pm UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642 "2022-04-05T13:29:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nn14](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@nn14](https://discuss.elastic.co/u/nn14)\
**Post date:** [April 5, 2022, 1:29pm UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642/1 "2022-04-05T13:29:24Z")

</div>

Hi team,

Is there a way to define our own custom unique key for the document which is similar to [uniqueKey](https://solr.apache.org/guide/7_5/other-schema-elements.html#unique-key) provided in Solr.

For e.g I need to define employeeId or a combination of 2 fields as unique id.

Any example would be really helpful

Thanks

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 5, 2022, 9:39pm UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642/2 "2022-04-05T21:39:06Z")

</div>

Welcome to our community! 😃

You can create your own field (key) with a unique ID if you want.

---

<div class="post-metadata">

**Author:** ![nn14](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@nn14](https://discuss.elastic.co/u/nn14)\
**Post date:** [April 6, 2022, 7:55am UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642/3 "2022-04-06T07:55:08Z")

</div>

Hi @warkolm :

Thanks for you reply.

Please help me understand this better.

Say suppose I want to define empid as a uniquekey. This way I want to prevent insertion of another user with the same empid. There should be only one unique empid in ES, duplicates should get rejected.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [April 6, 2022, 10:42pm UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642/4 "2022-04-06T22:42:45Z")

</div>

> [@nn14](#):
>
> Say suppose I want to define empid as a uniquekey. This way I want to prevent insertion of another user with the same empid. There should be only one unique empid in ES, duplicates should get rejected.

You can set this `empid` as the Elasticsearch `_id`, but if you send an update it will overwrite the existing document.

So Elasticsearch won't reject the request, it's something your code needs to check and enforce.

---

<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:** [May 4, 2022, 10:43pm UTC](https://discuss.elastic.co/t/define-custom-unique-key/301642/5 "2022-05-04T22:43:41Z")

</div>

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