Insert if not exist or return the existent document

unfortunatelly I haven't found the answer after some search and tries...

After insert the document below I received the id istiNGUBHVrkxpxfdqML

{
    "person" : {
        "brid" : "12345",
        "name" : "Jonh",
        "birthDate" : "1985-08-27",
        "expirationDate" : "2018-08-13" 
    }
}

I would like to know if is possible to deal with the following situation in only one request.

If the user tries to add a document with the same brid and current date is before expirationDate, return the content of document id istiNGUBHVrkxpxfdqML. If not, insert a new document and return the new document id.

Is this possible?

op_type is probably what you want to prevent an overwrite of the data; but you'd have to manually fetch the doc if it already exists. I would say that most systems expose a get_or_create type functionality would do it this way in behind the scenes.

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-index_.html#operation-type

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