elasticsearch 6.0 updating example using curl

Elasticsearch updating example using curl with simple steps.

In our example we will learn how can we update a document using curl in Elasticsearch.

cURL is a command line tool that can be used to transfer data over HTTP.In our example we will execute cURL command from cygwin.

  • To learn for updating document.First we will index a document and after that we will update it.
  • To index a document execute below command.
  • curl -XPUT 'localhost:9200/preparationforinterview2/elasticsearch-update/1?pretty' -H 'Content-Type: application/json' -d '{ "name" : "sam", "profession" : "engineer"}'

  • Explain

    syntax of cURL command is


    As we know in Elasticsearch every index have type and every type have document.In our example preparationforinterview2 is index name and elasticsearch-update is a type name while 1 is document name. For REST API Elasticsearch uses 9200 port as default port.

    Elasticsearch automatically adds index , type and mapping in that type.

    Elasticsearch automatically detects that name and profession is text and if new documents is added with more fields then Elasticsearch finds their type and adds it into mapping

  • After executing above command , we will execute update API.In our example we will change profession of above document from engineer to Doctor.
  • curl -XPOST 'localhost:9200/preparationforinterview2/elasticsearch-update/1/_update?pretty' -H 'Content-Type: application/json' -d '{ "doc": { "profession": "Doctor" } }'

  • Explain

    When we update a document then it follows below steps

    • First it finds existing document
    • Then update that document.
    • Delete old document.
    • Index new document.
  • To verify the update result we can see by search that document.We will get updated document.

  • curl 'localhost:9200/preparationforinterview2/elasticsearch-update/_search?pretty'

Note :

In above example we updated existing document but if we try to update a document that does not exist.Then we get exception


To check this scenario, we try to update document id 2 which does not exist.Execute below command

curl -XPOST 'localhost:9200/preparationforinterview2/elasticsearch-update/2/_update?pretty' -H 'Content-Type: application/json' -d '{ "doc": { "profession": "Doctor" } }'

We get following output

Note :

If we try to update a document that exist but field which we want to update does not exist, then update command will add that field.


If we update field hobby in document id 1 where document exist but field does not exist.Then that field is added by Elasticsearch.

curl -XPOST 'localhost:9200/preparationforinterview2/elasticsearch-update/1/_update?pretty' -H 'Content-Type: application/json' -d '{ "doc": { "hobby": "Singing" } }'

To check that field is added or not.Execute search query

curl 'localhost:9200/preparationforinterview2/elasticsearch-update/_search?pretty'

Visit Others Links

Elasticsearch Introduction
Elasticsearch 6.0 Setup
Elasticsearch Indexing And Searching
Elasticsearch 6.0 indexing example using curl
Elasticsearch 6.0 searching example using curl
Elasticsearch 6.0 updating example using curl
Elasticsearch 6.0 updating example using curl when document does not exist
Elasticsearch 6.0 deleting example using curl
Elasticsearch 6.0 request body based searching
Elasticsearch 6.0 query component example
Elasticsearch 6.0 Analyze API example
Top Elasticsearch Interview Question
© 2019 PreparationForInterview.com