# Inserting a document that already exists. Exception?

**URL:** https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084
**Category:** Elasticsearch
**Created:** [June 15, 2018, 11:04am UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084 "2018-06-15T11:04:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![javidr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javidr/32/136982_2.png) [@javidr](https://discuss.elastic.co/u/javidr)
#### Post date: [June 15, 2018, 11:04am UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/1 "2018-06-15T11:04:54Z")

</div>

Hi

I will need to add documents to an elasticsearch index i already have. I want to validate if the document i an adding already exists in this index, and if thats the case, dont add it

Is elasticsearch controlling that scenario returning an exception? If not, how can i validate this?

I have tried using the GET API, but you need to send the ID as parameter. i want to search inside the whole index, not using a specific ID

Thanks

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 15, 2018, 11:19am UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/2 "2018-06-15T11:19:38Z")

</div>

I guess this is what you want: [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index\_.html#operation-type](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type)

---

<div class="post-metadata">

### Author: ![tdasch](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@tdasch](https://discuss.elastic.co/u/tdasch)
#### Post date: [June 15, 2018, 11:23am UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/3 "2018-06-15T11:23:53Z")

</div>

It looks like `op_type` requires the id?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 15, 2018, 11:36am UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/4 "2018-06-15T11:36:28Z")

</div>

Ha I missed that part. Sorry.

Then there is no other than doing 2 calls.

The first to search if the document exists:

```auto
GET index/_search
{
  "size": 0,
  "query": ...
}

```

The second to effectively index if no document exists.

Problem is that document can be added in the meantime.

Another solution would be to compute a fingerprint on whatever fields are important to you and use that as the `_id`.

---

<div class="post-metadata">

### Author: ![javidr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javidr/32/136982_2.png) [@javidr](https://discuss.elastic.co/u/javidr)
#### Post date: [June 15, 2018, 1:23pm UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/5 "2018-06-15T13:23:34Z")

</div>

Thanks for the answer

My problem is that the id is just a number i am generating at insertion time (1,2,3...)

What i would like to do i search in an index if a document (without using the id) exists in that index, as i dont know what is the id for the existing document

Imagine i have {"name": "javi"} and it has been indexed with id 4847 on the index "test"

How can i check if {"name": "javi"} exists in "test"?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [June 15, 2018, 1:41pm UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/6 "2018-06-15T13:41:23Z")

</div>

Has I said:

- run a search to see if there is a match

Or

- generate a hash based on this payload and use it as an id

---

<div class="post-metadata">

### Author: ![tdasch](https://avatars.discourse-cdn.com/v4/letter/t/58f4c7/32.png) [@tdasch](https://discuss.elastic.co/u/tdasch)
#### Post date: [June 15, 2018, 6:33pm UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/7 "2018-06-15T18:33:44Z")

</div>

Javi,

Like Mr. Pilato said, you can run a query to search. Let's use your example in the query:

```
GET test/_search
{
  "query": {
    "match": {
      "name": "javi"
    }
  }
}

```

If the doc is present, you'll get a hit and the associated id, see example below:

```
{
  "took": 10,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "doc",
        "_id": "gFO3BGQBHKIRaf1Qrs4z",
        "_score": 0.2876821,
        "_source": {
          "name": "javi"
        }
      }
    ]
  }
}

```

The line you are looking for is `"_id": "gFO3BGQBHKIRaf1Qrs4z",` , if it exists.

---

<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 13, 2018, 6:33pm UTC](https://discuss.elastic.co/t/inserting-a-document-that-already-exists-exception/136084/8 "2018-07-13T18:33:44Z")

</div>

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