{"id":5407,"date":"2018-09-28T03:32:57","date_gmt":"2018-09-28T03:32:57","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=5407"},"modified":"2025-05-04T06:08:22","modified_gmt":"2025-05-04T06:08:22","slug":"understanding-the-update-api-in-elasticsearch","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/understanding-the-update-api-in-elasticsearch\/","title":{"rendered":"Understanding the Update API in Elasticsearch"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5417\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/elastic-search-2.png\" alt=\"\" width=\"600\" height=\"315\" srcset=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/elastic-search-2.png 600w, https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2018\/09\/elastic-search-2-300x158.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p>The update API allows to update(also allows to delete, or ignore the operation). a document based on a script provided.<\/p>\n<ul>\n<li>The operation gets the document from the index, runs the script (with optional script language and parameters), and index back the result.<\/li>\n<li>The _source field needs to be enabled for this feature to work.<\/li>\n<\/ul>\n<p><strong>For example, let\u2019s index a simple doc:<\/strong><\/p>\n<pre>curl -X PUT \"localhost:9200\/test\/_doc\/1\" -H 'Content-Type: application\/json' -d'\n{\n    \"counter\" : 1,\n    \"tags\" : [\"red\"]\n}\n'\n<\/pre>\n<p><strong>Now, we can execute a script that would increment the counter:<\/strong><\/p>\n<pre>curl -X POST \"localhost:9200\/test\/_doc\/1\/_update\" -H 'Content-Type: application\/json' -d'\n{\n    \"script\" : {\n        \"source\": \"ctx._source.counter += params.count\",\n        \"lang\": \"painless\",\n        \"params\" : {\n            \"count\" : 4\n        }\n    }\n}\n'\n<\/pre>\n<p><strong>We can add a tag to the list of tags (note, if the tag exists, it will still add it, since its a list):<\/strong><\/p>\n<pre>curl -X POST \"localhost:9200\/test\/_doc\/1\/_update\" -H 'Content-Type: application\/json' -d'\n{\n    \"script\" : {\n        \"source\": \"ctx._source.tags.add(params.tag)\",\n        \"lang\": \"painless\",\n        \"params\" : {\n            \"tag\" : \"blue\"\n        }\n    }\n}\n'\n<\/pre>\n<p>ng&gt;We can also add a new field to the document:<\/p>\n<pre>curl -X POST \"localhost:9200\/test\/_doc\/1\/_update\" -H 'Content-Type: application\/json' -d'\n{\n    \"script\" : \"ctx._source.new_field = \\u0027value_of_new_field\\u0027\"\n}\n'\n<\/pre>\n<p>We can also add a new field to the document:<\/p>\n<pre>curl -X POST \"localhost:9200\/test\/_doc\/1\/_update\" -H 'Content-Type: application\/json' -d'\n{\n    \"script\" : \"ctx._source.new_field = \\u0027value_of_new_field\\u0027\"\n}\n'\n<\/pre>\n<p><strong>Or remove a field from the document:<\/strong><\/p>\n<pre>curl -X POST \"localhost:9200\/test\/_doc\/1\/_update\" -H 'Content-Type: application\/json' -d'\n{\n    \"script\" : \"ctx._source.remove(\\u0027new_field\\u0027)\"\n}\n'\n<\/pre>\n<p><strong>Reference<\/strong><br \/>https:\/\/www.elastic.co\/guide\/en\/elasticsearch\/reference\/current\/docs-update.html<\/p>\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>ctx is a special variable that allows you to access the source of the object that you want to update. The ctx._source is a writable version of the source.<br> NOTE: You can modify this document in the script and the modified source will be persisted as the new version of the document.<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>The update API allows to update(also allows to delete, or ignore the operation). a document based on a script provided. The operation gets the document from the index, runs the script (with optional script language and parameters), and index back the result. The _source field needs to be enabled for this feature to work. For&#8230;<\/p>\n","protected":false},"author":1,"featured_media":5417,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5936],"tags":[1056,5057,4666,5065,284,5070],"class_list":["post-5407","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-elastic","tag-api","tag-elastic","tag-elasticsearch","tag-search","tag-update","tag-update-api"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=5407"}],"version-history":[{"count":4,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5407\/revisions"}],"predecessor-version":[{"id":6152,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/5407\/revisions\/6152"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/5417"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=5407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=5407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=5407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}