# Document ID not found error while updating a document, even though we have that document ID present in the ES

**URL:** <https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239>\
**Category:** Elasticsearch\
**Created:** [July 12, 2019, 12:42pm UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239 "2019-07-12T12:42:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![viveksinghggits](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viveksinghggits/32/49997_2.png) [@viveksinghggits](https://discuss.elastic.co/u/viveksinghggits)\
**Post date:** [July 12, 2019, 12:42pm UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239/1 "2019-07-12T12:42:47Z")

</div>

So, what I am trying to do is get a document from ES cluster, make some changes into the document and then update this document back into the elastic search cluster.  
I am using elasticsearch's python client and `.update` to update the document back to es cluster. Now the problem is sometimes this `.update` gives us `DocumentID not found` error, but that document id is present there for sure, because we are getting the document with the same document ID and then trying to update it.  
I tried to figure out the issue and somehow I think this related to [refresh](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-refresh.html) API.  
Can someone provide some suggestions or how to go about this.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [July 17, 2019, 10:31pm UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239/2 "2019-07-17T22:31:54Z")

</div>

Hi ,  
Can you provide the code you use.

---

<div class="post-metadata">

**Author:** ![viveksinghggits](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/viveksinghggits/32/49997_2.png) [@viveksinghggits](https://discuss.elastic.co/u/viveksinghggits)\
**Post date:** [July 23, 2019, 7:22am UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239/3 "2019-07-23T07:22:22Z")

</div>

Below is the code that we used to add or update the document in the elastic search.

```auto
def add_document(i_index, i_doc_type, i_id, i_body):
    es = es_connect()
    try:
        res = es.index(index=i_index, doc_type=i_doc_type,
                       id=i_id, body=i_body)
        count_success = res.get("_shards", {}).get("successful")
        return count_success
    except Exception as ex:
        raise Exception(str(ex))

```

and

```auto
def update_document(i_index, i_doc_type, i_id, i_body):
    es = es_connect()
    try:
        res = es.update(index=i_index, doc_type=i_doc_type,
                        id=i_id, body={"doc": i_body})
        count_success = res.get("_shards", {}).get("successful")
        return count_success
    except Exception as ex:
        raise Exception(str(ex))

```

adding `refresh='true'` seems to have resolved the issue.

---

<div class="post-metadata">

**Author:** ![gabriel\_tessier](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gabriel_tessier/32/27911_2.png) [@gabriel\_tessier](https://discuss.elastic.co/u/gabriel_tessier)\
**Post date:** [July 24, 2019, 2:32am UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239/4 "2019-07-24T02:32:43Z")

</div>

I get it you add a document and you update it in a very short time (less than one second, default refresh rate).  
Adding refresh on all update can have some bad side effect check here about what happen when you refresh. Here some resource:

> **[Guide to Refresh and Flush Operations in Elasticsearch](https://qbox.io/blog/refresh-flush-operations-elasticsearch-guide)**
>
> In this guide, we explore Refresh and Flush operations in Elasticsearch. This guide will bring resolution to the differences between the two in an effective manner. We also cover the underlying basics of Lucene...

  

> <https://stackoverflow.com/questions/19963406/refresh-vs-flush>

What is your use case to update a document after insert it, there's maybe a better way to solve your problem.

---

<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:** [August 21, 2019, 2:34am UTC](https://discuss.elastic.co/t/document-id-not-found-error-while-updating-a-document-even-though-we-have-that-document-id-present-in-the-es/190239/5 "2019-08-21T02:34:28Z")

</div>

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