# Maintain a unique field while indexing - equivalent to a UNIQUE INDEX in a relational database

**URL:** <https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883>\
**Category:** Elasticsearch\
**Created:** [December 23, 2015, 7:08pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883 "2015-12-23T19:08:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [December 23, 2015, 7:08pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/1 "2015-12-23T19:08:44Z")

</div>

Hey,

Is there a way to use one of my document fields as a uniq id? making sure there will not be 2 document with the same value in that field?

Here are 2 documents for example:

```
   {
       "name": "name1",
       "email":"firstEmail@gmail.com"
   },
   {
       "name": "name2",
       "email":"seconddEmail@gmail.com"
   }

```

I want to "define" the email field as a uniq identifier.  
To get closer to this goal, I search elasticsearch before indexing for a document with the same email, and when I can't find that email I index the document, and refresh the index.  
This is of course not bullet proof, but it cover 99.999% of the requests

I want to make it more accurate , and make sure I will never index 2 documents with the same email.  
Is there a better way to do it?

Thanks!

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 23, 2015, 7:29pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/2 "2015-12-23T19:29:33Z")

</div>

Making the document's id the email would do it.

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [December 23, 2015, 8:51pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/3 "2015-12-23T20:51:42Z")

</div>

Can you please explain why?  
If I send 100 index requests in one second, with the same email address as their id, it will work?  
This is not an atomic operation as I understand.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [December 23, 2015, 9:55pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/4 "2015-12-23T21:55:30Z")

</div>

Elasticsearch tries not to let you make two documents with the same type:id. Updating a single document is atomic on each copy of the shard on which it lives. Each copy may apply the update at a different time though. And they become visible to search (refresh) at different times as well. Elasticsearch does some tricks where it reads the document out of the index if its been refreshed or out of the translog if it hasn't, but they amount to optimistic concurrency control and are exposed through a `version` parameter on index commands.

You can cheat the document uniqueness using routing or parent/child. That is known and documented. Otherwise if you get convince it to let you make two documents with the same type:id then its a bug.

Its certainly possible that there are bugs related to network partitions where Elasticsearch can get confused. Those are bugs that are actively being worked though.

---

<div class="post-metadata">

**Author:** ![tpraizler](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tpraizler/32/46271_2.png) [@tpraizler](https://discuss.elastic.co/u/tpraizler)\
**Post date:** [December 23, 2015, 11:02pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/5 "2015-12-23T23:02:25Z")

</div>

Cool!

I understand your suggestion, but is there a way to do it, without setting the id to be the email?  
I want to keep the auto generated id, and use a separate field for email.

Is my suggestion(original question) is valid? is there a better way to do it?

---

<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:28pm UTC](https://discuss.elastic.co/t/maintain-a-unique-field-while-indexing-equivalent-to-a-unique-index-in-a-relational-database/37883/6 "2017-07-05T23:28:59Z")

</div>


