Create, Update, and Delete Documents in Elasticsearch

Hands-On Lab

 

Photo of Myles Young

Myles Young

BigData Training Architect II in Content

Length

01:00:00

Difficulty

Intermediate

Whether you're using Elasticsearch for log aggregation, site search, or as a NoSQL database, it is important to understand basic CRUD operations when working with documents. In this hands-on lab, you are given the opportunity to create, update, and delete documents from a pre-loaded dataset in order to familiarize yourself with these basic operations.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Create, Update, and Delete Documents in Elasticsearch

Introduction

Whether you're using Elasticsearch for log aggregation, site search, or as a NoSQL database, it is important to understand basic CRUD operations when working with documents. In this hands-on lab, you are given the opportunity to create, update, and delete documents from a pre-loaded dataset in order to familiarize yourself with these basic operations.

Solution

  1. Begin by forwarding port 5601 to the lab server using the credentials provided on the hands-on lab page:

    ssh cloud_user@PUBLIC_IP_ADDRESS -L 5601:localhost:5601
  2. In your browser, open a new tab to http://localhost:5601.

  3. Navigate to Dev Tools in the menu on the left.

Create the new account for Kenny with document ID 1000

  1. In Kibana's Console tool, send the following request:

    PUT /bank/account/1000
    {
      "account_number": 1000,
      "balance": 65536,
      "firstname": "Kenny",
      "lastname": "Armstrong",
      "age": 67,
      "gender": "M",
      "address": "125 Bear Creek Pkwy",
      "employer": "Linux Academy",
      "email": "kenfrd@linuxacademy.com",
      "city": "Keller",
      "state": "TX"
    }

    You should see something like this:

    {
      "_index": "bank",
      "_type": "account",
      "_id": "1000",
      "_version": 1,
      "result": "created",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 1008,
      "_primary_term": 1
    }
  2. You can check your work with:

    GET /bank/account/1000

Update Madden's account (document ID 100) with her new address

  1. In Kibana's Console tool, send the following request:

    POST /bank/account/100/_update
    {
      "doc": {
        "address": "1600 Pennsylvania Ave NW",
        "city": "Washington",
        "state": "DC"
      }
    }

    You should see something like:

    {
      "_index": "bank",
      "_type": "account",
      "_id": "100",
      "_version": 2,
      "result": "noop",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      }
    }
  2. You can check your work with:

    GET /bank/account/100

Delete the closed accounts for Amber (document ID 1) and Dominique (document ID 10)

  1. In Kibana's Console tool, send the following requests:

    DELETE /bank/account/1
    DELETE /bank/account/10

    You should see something like:

    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 2,
      "result": "deleted",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 103,
      "_primary_term": 1
    }

    And:

    {
      "_index": "bank",
      "_type": "account",
      "_id": "10",
      "_version": 2,
      "result": "deleted",
      "_shards": {
        "total": 1,
        "successful": 1,
        "failed": 0
      },
      "_seq_no": 104,
      "_primary_term": 1
    }
  2. You can check your work with:

    GET /bank/account/1
    GET /bank/account/10

Conclusion

Congratulations — you've completed this hands-on lab!