Completely update documents without creating if not exist

Is there any elasticsearch method for fully (not partially) updating documents and not creating new ones if it does not already exist?

So far, I have found that the _update method when passing the doc attribute inside the json request body to partially update documents, however, I would like to replace the entire document in this case, not only partially.

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

Setting op_type to create will force a document to be created, not an update. I was wondering if there is a way to always apply update and never create new one?

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

+5
source share
1 answer

If I understand correctly, do you want to index the document, but only if it already exists? How is the op_type update option?

You can mainly use this with the update API, given that your mapping remains unchanged. With _update , if the document does not exist, you will return to 404 . If it exists, ES will merge the contents of the doc with any document. If you make sure that you send a new document with all the fields in the mapping, you are actually replacing it directly.

Note, however, that you can do this without merging the document quite effectively in two queries; the first of which checks for the existence of a HEAD request document. If HEAD /idx/type/id successful, execute a PUT . This, in fact, is what happens inside the company anyway with the update API, with a little extra overhead. But HEAD really cheap because it does not shuffle any payload. It just returns HTTP 200/404.

0
source

Source: https://habr.com/ru/post/1201496/


All Articles