[Recipes] Replace, Update and Upsert Elasticsearch Documents

Problem: 

Need to replace existing documents. Need to update only part of the document with / without scripting. Need to do upserts.

Solution Summary: 

We can replace elastic search documents using a PUT request with a request body.

We can update only part of a document using a POST request with _update endpoint. ES documents are immutable. So internally _update will get the document, update the fields and PUT again.

We can use scrpting to update based on existing field values. 

Finally, we can use upserts to update only if the document exist.

Solution Steps: 

Initial State

We will create a new document in the employee index and use it for some of the following excercises. 

PUT /employee/_doc/2?pretty
{
  "name": "Sneha",
  "phone": "9999999999"
}

Verify it using: GET /employee/_doc/2?pretty

 

Replacing Documents

We can replace a document using PUT.

PUT /employee/_doc/2?pretty
{
  "name": "Sneha",
  "salary": 5000000
}

Verify it using: GET /employee/_doc/2?pretty

You can see that there is no more a phone field, but a salary field. 

Note:

  1. Using POST will also have same behaviour.

  2. When replacing a document, ES will still still update _version (verify)

Next, we can see how to update some fields alone.

 

Updating Documents

We can use _update endpoint and the "doc" property.

POST /employee/_doc/2/_update
{
  "doc": {
    "salary": 10000000
  }
}

Verify it using: GET /employee/_doc/2?pretty

Note:

  1. Only POST is allowed and PUT is not allowed here.

  2. ES will increment _version

 

Updating Documents Using Script

POST /employee/_doc/2/_update
{
  "script": "ctx._source.salary += 10"
}

Note: ctx variable reprecents current document.

 

Upsert Example

Create document if it does not exist executing the upsert part. 

POST /employee/_doc/4/_update
{
  "script": "ctx._source.salary +=10",
  "upsert": {
    "salary": 5000000
  }
}

Verify by executing GET before first execution, after first execution and after second execution:

GET /employee/_doc/4/?pretty

Note: You cannot use this syntax to create a field for an existing document. 

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook

Contact

Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.

About

CloudMaterials is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Microsoft Azure and Amazon Web Services (AWS).

I like to write and I try to document what I learn to share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream