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

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 API.
Can someone provide some suggestions or how to go about this.

Hi ,
Can you provide the code you use.

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

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

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.

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:


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

1 Like

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