Search code examples
elasticsearchupdatesputnosql

Fully update documents without creating if not existent


Is there any method on elasticsearch for fully (not partially) updating documents and not create new ones in case it doesn’t already exists?

Until now, I found that the _update method, while passing a doc attribute inside the json request body to partially updating documents, however, I would like to replace the entire document in this case, not only partially.

I have also found that, the index method, where sending a PUT request works fine, although creating a new document in case the id not yet indexed.

Setting the op_type parameter to create will enforce document creation instead update. I was wondering if there is any way to always enforce update and never create a new one?

Or perhaps is there another method that would allow me to achieve such task?


Solution

  • If I understand correctly, you want to index a doc, but only if it already exists? Like an op_type option of update?

    You can mostly do it with the update API, given that your mapping remains consistent. With an _update, if the document doesn't exist, you'll get back a 404. If it does exist, ES will merge the contents of doc with whatever document exists there. If you make sure you're sending over a new doc with all the fields in the mapping, then you're effectively replacing it outright.

    Note, however, that you can do it without the document merge rather efficiently in two requests; the first one checking for doc existence with a HEAD request. If HEAD /idx/type/id is successful, then do a PUT. This is essentially what's happening internally anyway with the update API, with a little extra overhead. But HEAD is really cheap because it's not shuffling any payload around. It simply returns an HTTP 200/404.