elasticsearch 6.0 updating example using upsert and curl

Elasticsearch updating example using upsert and curl with simple steps.


In our example we will learn how can we update a document using upsert 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 start work on upsert first we see how many documents are already in Elasticsearch. To check this execute search command.
  • curl 'localhost:9200/preparationforinterview2/elasticsearch-update/_search?pretty'



  • If we try to update a document that does not exist.Then we get exception
  • document_missing_exception

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

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

    We get following output



    Explain

    syntax of cURL command is

    http://localhost:9200/index-name/type-name/document-name

    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 4 is document name. For REST API Elasticsearch uses 9200 port as default port.

    Upsert

  • If we want to update a document if it exists otherwise create that document as a new document. To do this we use upsert.upsert is made of two command update and insert.

  • curl -XPOST 'localhost:9200/preparationforinterview2/elasticsearch-update/4/_update?pretty' -H 'Content-Type: application/json' -d '{ "doc": { "profession": "Doctor" }, "upsert": { "name" : "Elasticsearch updated", "profession": "Doctor" } }'

  • To check that our document id 4 is indexed or not , we will search again with id 4.To check that execute below command.





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