# Uniqueness constraint

**URL:** <https://discuss.elastic.co/t/uniqueness-constraint/8615>\
**Category:** Elasticsearch\
**Created:** [August 2, 2012, 9:15pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615 "2012-08-02T21:15:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Greg\_3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Greg\_3](https://discuss.elastic.co/u/Greg_3)\
**Post date:** [August 2, 2012, 9:15pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/1 "2012-08-02T21:15:41Z")

</div>

Hi all

Is it possible to enforce a uniqueness constraint for a particular field in  
Elasticsearch? I would like to prevent multiple documents from being  
indexed with the same email address.

Thanks  
Greg

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 2, 2012, 9:33pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/2 "2012-08-02T21:33:15Z")

</div>

Elasticsearch doesn't enforce any uniqueness constraints unless the field  
is used as ID. If your documents already have IDs that you cannot be  
changed, you could index email addresses as child records. Just make sure  
that you are using op\_type=create to ensure that new records doesn't  
overwrite an existing record.

On Thursday, August 2, 2012 5:15:41 PM UTC-4, Greg wrote:

> Hi all
> 
> Is it possible to enforce a uniqueness constraint for a particular field  
> in Elasticsearch? I would like to prevent multiple documents from being  
> indexed with the same email address.
> 
> Thanks  
> Greg

---

<div class="post-metadata">

**Author:** ![Greg\_3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Greg\_3](https://discuss.elastic.co/u/Greg_3)\
**Post date:** [August 2, 2012, 10:20pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/3 "2012-08-02T22:20:25Z")

</div>

Thanks Igor, that's really useful to know.

Are there any restrictions to the length or the characters that can be used  
for the ID field?

Greg

On Thursday, 2 August 2012 22:33:15 UTC+1, Igor Motov wrote:

> Elasticsearch doesn't enforce any uniqueness constraints unless the field  
> is used as ID. If your documents already have IDs that you cannot be  
> changed, you could index email addresses as child records. Just make sure  
> that you are using op\_type=create to ensure that new records doesn't  
> overwrite an existing record.
> 
> On Thursday, August 2, 2012 5:15:41 PM UTC-4, Greg wrote:
> 
> > Hi all
> > 
> > Is it possible to enforce a uniqueness constraint for a particular field  
> > in Elasticsearch? I would like to prevent multiple documents from being  
> > indexed with the same email address.
> > 
> > Thanks  
> > Greg

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 3, 2012, 1:36am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/4 "2012-08-03T01:36:37Z")

</div>

I cannot think of any length restrictions besides URL size that your HTTP  
client can handle and available memory. However, considering that ID is  
used in almost all record-related operations, it would be beneficial to  
keep it as small as possible.

For character set restrictions  
see [https://groups.google.com/d/topic/elasticsearch/zLpG\_a2cPz4/discussion](https://groups.google.com/d/topic/elasticsearch/zLpG_a2cPz4/discussion)

On Thursday, August 2, 2012 6:20:25 PM UTC-4, Greg wrote:

> Thanks Igor, that's really useful to know.
> 
> Are there any restrictions to the length or the characters that can be  
> used for the ID field?
> 
> Greg
> 
> On Thursday, 2 August 2012 22:33:15 UTC+1, Igor Motov wrote:
> 
> > Elasticsearch doesn't enforce any uniqueness constraints unless the field  
> > is used as ID. If your documents already have IDs that you cannot be  
> > changed, you could index email addresses as child records. Just make sure  
> > that you are using op\_type=create to ensure that new records doesn't  
> > overwrite an existing record.
> > 
> > On Thursday, August 2, 2012 5:15:41 PM UTC-4, Greg wrote:
> > 
> > > Hi all
> > > 
> > > Is it possible to enforce a uniqueness constraint for a particular field  
> > > in Elasticsearch? I would like to prevent multiple documents from being  
> > > indexed with the same email address.
> > > 
> > > Thanks  
> > > Greg

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [August 3, 2012, 9:35am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/5 "2012-08-03T09:35:45Z")

</div>

On Thu, 2012-08-02 at 14:33 -0700, Igor Motov wrote:

> Elasticsearch doesn't enforce any uniqueness constraints unless the  
> field is used as ID. If your documents already have IDs that you  
> cannot be changed, you could index email addresses as child records.  
> Just make sure that you are using op\_type=create to ensure that new  
> records doesn't overwrite an existing record.

I wrote a Perl module to handle just this situation:  
[https://metacpan.org/module/ElasticSearchX::UniqueKey](https://metacpan.org/module/ElasticSearchX::UniqueKey)

The downside of the module is that you have an extra index dedicated to  
unique keys. The upside is that you can ensure that (eg) the email  
address is unique, but still allow the user to update their email  
address without having to reindex any docs that refer to the user's ID.

The concept used in the module is pretty simple, so it should be easy to  
reimplement in the language of your choice

clint

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [August 3, 2012, 9:37am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/6 "2012-08-03T09:37:58Z")

</div>

> I wrote a Perl module to handle just this situation:  
> [https://metacpan.org/module/ElasticSearchX::UniqueKey](https://metacpan.org/module/ElasticSearchX::UniqueKey)

For Perl users, UniqueKey is nicely integrated into Elastic::Model:

> **[Elastic::Manual::Attributes::Unique - Making attributes unique - metacpan.org](https://metacpan.org/pod/Elastic::Manual::Attributes::Unique)**
>
> Making attributes unique

clint

---

<div class="post-metadata">

**Author:** ![Greg\_3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Greg\_3](https://discuss.elastic.co/u/Greg_3)\
**Post date:** [August 3, 2012, 10:17am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/7 "2012-08-03T10:17:28Z")

</div>

Thanks Clint, but I am not using Perl.

On Friday, 3 August 2012 10:37:58 UTC+1, Clinton Gormley wrote:

> > I wrote a Perl module to handle just this situation:  
> > [https://metacpan.org/module/ElasticSearchX::UniqueKey](https://metacpan.org/module/ElasticSearchX::UniqueKey)
> 
> For Perl users, UniqueKey is nicely integrated into Elastic::Model:
> 
> [Elastic::Manual::Attributes::Unique - Making attributes unique - metacpan.org](https://metacpan.org/module/Elastic::Manual::Attributes::Unique)
> 
> clint

---

<div class="post-metadata">

**Author:** ![Greg\_3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Greg\_3](https://discuss.elastic.co/u/Greg_3)\
**Post date:** [August 3, 2012, 10:18am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/8 "2012-08-03T10:18:57Z")

</div>

Should I raise this is a feature request, or is there a technical reason  
why a field in Elasticsearch can't be made to be unique?

Greg

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [August 3, 2012, 10:34am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/9 "2012-08-03T10:34:43Z")

</div>

On Fri, 2012-08-03 at 03:17 -0700, Greg wrote:

> Thanks Clint, but I am not using Perl.

As I said:

> The concept used in the module is pretty simple, so it should be easy  
> to  
> reimplement in the language of your choice

Even if you don't know Perl, the code with the docs should be fairly  
easy to understand and reimplement

[https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS](https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS)  
[https://metacpan.org/source/DRTECH/ElasticSearchX-UniqueKey-0.03/lib/ElasticSearchX/UniqueKey.pm](https://metacpan.org/source/DRTECH/ElasticSearchX-UniqueKey-0.03/lib/ElasticSearchX/UniqueKey.pm)

---

<div class="post-metadata">

**Author:** ![Greg\_3](https://avatars.discourse-cdn.com/v4/letter/g/dfb087/32.png) [@Greg\_3](https://discuss.elastic.co/u/Greg_3)\
**Post date:** [August 3, 2012, 10:48am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/10 "2012-08-03T10:48:20Z")

</div>

Sorry, I missed the last part of your message. I will look at doing this,  
but I'm really wondering whether this is something that would be useful (or  
even possible) to have built in to Elasticsearch.

On Friday, 3 August 2012 11:34:43 UTC+1, Clinton Gormley wrote:

> On Fri, 2012-08-03 at 03:17 -0700, Greg wrote:
> 
> > Thanks Clint, but I am not using Perl.
> 
> As I said:
> 
> > The concept used in the module is pretty simple, so it should be easy  
> > to  
> > reimplement in the language of your choice
> 
> Even if you don't know Perl, the code with the docs should be fairly  
> easy to understand and reimplement
> 
> [https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS](https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS)
> 
> [lib/ElasticSearchX/UniqueKey.pm - metacpan.org](https://metacpan.org/source/DRTECH/ElasticSearchX-UniqueKey-0.03/lib/ElasticSearchX/UniqueKey.pm)

---

<div class="post-metadata">

**Author:** ![Richard\_Louapre](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Richard\_Louapre](https://discuss.elastic.co/u/Richard_Louapre)\
**Post date:** [October 9, 2013, 11:54am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/11 "2013-10-09T11:54:47Z")

</div>

Is Igor's suggestion still the best way to handle uniqueness constraints in  
Elasticsearch?

Is there any plans to have a built-in feature?

On Friday, August 3, 2012 6:48:20 AM UTC-4, Greg wrote:

> Sorry, I missed the last part of your message. I will look at doing this,  
> but I'm really wondering whether this is something that would be useful (or  
> even possible) to have built in to Elasticsearch.
> 
> On Friday, 3 August 2012 11:34:43 UTC+1, Clinton Gormley wrote:
> 
> > On Fri, 2012-08-03 at 03:17 -0700, Greg wrote:
> > 
> > > Thanks Clint, but I am not using Perl.
> > 
> > As I said:
> > 
> > > The concept used in the module is pretty simple, so it should be easy  
> > > to  
> > > reimplement in the language of your choice
> > 
> > Even if you don't know Perl, the code with the docs should be fairly  
> > easy to understand and reimplement
> > 
> > [https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS](https://metacpan.org/module/ElasticSearchX::UniqueKey#SYNOPSIS)
> > 
> > [lib/ElasticSearchX/UniqueKey.pm - metacpan.org](https://metacpan.org/source/DRTECH/ElasticSearchX-UniqueKey-0.03/lib/ElasticSearchX/UniqueKey.pm)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [October 9, 2013, 12:07pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/12 "2013-10-09T12:07:23Z")

</div>

Can you please elaborate what kind of uniqueness you are interested in?

Do you think of same value in same fields? Do you want to ignore the whole  
doc while indexing or search? Or only the field? Do you want to modify  
\_source or leave \_source intact? Or do you want to version documents also  
like with the ID uniqueness?

Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Richard\_Louapre](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@Richard\_Louapre](https://discuss.elastic.co/u/Richard_Louapre)\
**Post date:** [October 9, 2013, 12:41pm UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/13 "2013-10-09T12:41:01Z")

</div>

I need to reject at indexing time if same value in the same field is found  
in a different document.

On Wednesday, October 9, 2013 8:07:23 AM UTC-4, Jörg Prante wrote:

> Can you please elaborate what kind of uniqueness you are interested in?
> 
> Do you think of same value in same fields? Do you want to ignore the whole  
> doc while indexing or search? Or only the field? Do you want to modify  
> \_source or leave \_source intact? Or do you want to version documents also  
> like with the ID uniqueness?
> 
> Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:13am UTC](https://discuss.elastic.co/t/uniqueness-constraint/8615/14 "2017-07-06T02:13:13Z")

</div>


