# Enforce Primary Key in ElasticSearch

**URL:** <https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370>\
**Category:** Elasticsearch\
**Created:** [January 31, 2017, 1:17pm UTC](https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370 "2017-01-31T13:17:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![yoavb](https://avatars.discourse-cdn.com/v4/letter/y/839c29/32.png) [@yoavb](https://discuss.elastic.co/u/yoavb)\
**Post date:** [January 31, 2017, 1:17pm UTC](https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370/1 "2017-01-31T13:17:16Z")

</div>

Hi,

Please consider the following senarion :

I have a RDBMS database which load every 5 minuts 100K rows from a CSV file into database table.  
Once the the file data has been insert into the database , end user can only run select statment (no insert-update-delete are allowed).

Currently i have 5 tera of data.

I would like to move from that RDBMS to elastic search , but have one problem.  
I must enforce Primary key constraint, which currently built on 3 fields.  
This mean that i need to prevent old records/documents to be uploaded once again into elasticsearch.

1. Please advise if it possible to enforce a Primary key as describe above
2. I am wondering if the P.K limitation cause ElasticSearch to be inappropriate for my case and mybe i need to stick with the RDBMS or maybe try Mongodb .....

Regards  
Yoav

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [January 31, 2017, 2:07pm UTC](https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370/2 "2017-01-31T14:07:51Z")

</div>

Hi,

while there is no direct notion of a Primary Key like in RDBMS in Elasticsearch, you can probably use the document id for this. You need to find a way to create a unique "id" out of your three current pk fields. You can then either `op_type=create` or the `_create` endpoint of the [index api](https://www.elastic.co/guide/en/elasticsearch/reference/5.1/docs-index_.html) to prevent overwrites of existing documents like so:

```auto
PUT index/type/id?op_type=create
{
    "foo" : "bar"
}

```

or

```auto
PUT index/type/id/_create
{
    "foo" : "bar"
}

```

All future attempts to index a document with the same "id" will result in an error. Hope this solves your use case.

---

<div class="post-metadata">

**Author:** ![amar-tari](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amar-tari/32/14243_2.png) [@amar-tari](https://discuss.elastic.co/u/amar-tari)\
**Post date:** [February 1, 2017, 11:51am UTC](https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370/3 "2017-02-01T11:51:24Z")

</div>

Hi You may try two approaches here :

- Create separate field say "id" and while inserting use delete by query to ensure this gets deleted.

- Second approach is use "PUT" and while inserting record along with actual record id.

So your \_id field in ES will take your db record id and will prevent duplicating.

---

<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:** [March 1, 2017, 11:51am UTC](https://discuss.elastic.co/t/enforce-primary-key-in-elasticsearch/73370/4 "2017-03-01T11:51:26Z")

</div>

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