Home > Net >  What happens when I insert 2 documents with same id in ElasticSearch?
What happens when I insert 2 documents with same id in ElasticSearch?

Time:09-24

I would like to know what happens when I add a document with an existing id in the same index.

I know that it won't be repeated, what I don't know is if Elasticsearch will ignore the latter or if it will update the first one.

Thank you very much.

CodePudding user response:

The latest document will simply override the previous one having the same ID and the version count will be bumped by 1.

It's easy to test:

# 1. create the document
PUT test/_doc/1
{
  "test": "me"
}

# 2. response from the creation
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,               <---- check this
  "result" : "created",         <---- check this
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

# 3. update the document
PUT test/_doc/1
{
  "test": "me2"
}

# 4. response from the update
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,               <---- check this
  "result" : "updated",         <---- check this
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
  • Related