# DuplicateKeyError

**URL:** <https://discuss.elastic.co/t/duplicatekeyerror/262107>\
**Category:** Elastic Search\
**Created:** [January 25, 2021, 1:01pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107 "2021-01-25T13:01:05Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pesc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pesc/32/82536_2.png) [@pesc](https://discuss.elastic.co/u/pesc)\
**Post date:** [January 25, 2021, 1:01pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/1 "2021-01-25T13:01:05Z")

</div>

Hi

I'm getting an error while adding a document by POST to `documents`:  
`Exception: Unable to save document: Swiftype::Documents::DuplicateKeyError: `

How's that even possible? If the ID already exists, wouldn't it just update the fields?

Greetings Pascal

---

<div class="post-metadata">

**Author:** ![pesc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pesc/32/82536_2.png) [@pesc](https://discuss.elastic.co/u/pesc)\
**Post date:** [January 25, 2021, 4:41pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/2 "2021-01-25T16:41:42Z")

</div>

So weird. It only happens on a few documents: 22 out of 2.2 millions. But why?

```auto
{
    "id": "taxirothorn.ch",
    "domain": "taxirothorn.ch",
    "timestamp": "2021-01-25T16:45:31.798315+01:00"
}

```

For example on this. Have no idea with what magic AppSearch creates the actual ID stored in ElasticSearch or how it keeps track in `.ent-search-document-backend`

---

<div class="post-metadata">

**Author:** ![david.harsha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david.harsha/32/48003_2.png) [@david.harsha](https://discuss.elastic.co/u/david.harsha)\
**Post date:** [January 25, 2021, 8:03pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/3 "2021-01-25T20:03:16Z")

</div>

Hi @pesc! Good question. Are you able to reproduce the error when trying to index the individual document? I've seen `DuplicateKeyError` issues when the same ID is used in multiple documents in the same batch before.

---

<div class="post-metadata">

**Author:** ![pesc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pesc/32/82536_2.png) [@pesc](https://discuss.elastic.co/u/pesc)\
**Post date:** [January 26, 2021, 6:00am UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/4 "2021-01-26T06:00:16Z")

</div>

Yes, happens on single document indexing too

```auto
curl --location --request POST 'https://api.searchzone.ch/api/as/v1/engines/domains/documents' \
--header 'Authorization: Bearer private-?????' \
--header 'Content-Type: application/json' \
--data-raw '{
    "id": "taxirothorn.ch",
    "domain": "taxirothorn.ch",
    "timestamp": "2021-01-25T16:45:31.798315+01:00"
}'
[{"id":"taxirothorn.ch","errors":["Unable to save document"]}]

```

---

<div class="post-metadata">

**Author:** ![pesc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pesc/32/82536_2.png) [@pesc](https://discuss.elastic.co/u/pesc)\
**Post date:** [January 26, 2021, 10:06am UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/5 "2021-01-26T10:06:01Z")

</div>

Fixed it by manually deleting the entries out of ElasticSearch directly:

```auto
.ent-search-document-backend-[index]-document_type_id-external_id-unique-constraint

```

Don't ask me how it's possible that this happens...

---

<div class="post-metadata">

**Author:** ![david.harsha](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david.harsha/32/48003_2.png) [@david.harsha](https://discuss.elastic.co/u/david.harsha)\
**Post date:** [January 26, 2021, 4:43pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/6 "2021-01-26T16:43:12Z")

</div>

Hm, glad you got it worked out. Was the entry in that index recreated when you indexed the document successfully?

---

<div class="post-metadata">

**Author:** ![pesc](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pesc/32/82536_2.png) [@pesc](https://discuss.elastic.co/u/pesc)\
**Post date:** [January 28, 2021, 2:49pm UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/7 "2021-01-28T14:49:21Z")

</div>

Yes, everything worked properly 👍

---

<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:** [November 4, 2022, 8:22am UTC](https://discuss.elastic.co/t/duplicatekeyerror/262107/8 "2022-11-04T08:22:26Z")

</div>


