[Recipes] Creating Elasticsearch Index and Adding Documents

Problem: 

Need to create an elasticsearch index and add documents to that index through Kibana. 

Solution Summary: 

We can create an Elasticsearch index and add documents easily after from Kibana Devtools. You may also do the same using any HTTP client.

Solution Steps: 

​Creating an index using PUT

PUT /employee?pretty

Will get a success response.

 

Verifying the index with _cat/indices

GET /_cat/indices?v

 

Creating a Document Specifying ID

PUT /employee/_doc/1?pretty
{
  "name": "Heartin",
  "salary": 10000000
}

Examine the response. It should be similar to:

{
  "_index": "employee",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

You may also verify using the id returned:

GET /employee/_doc/1?pretty

 

Creating a Document Without Specifying ID

You may also create a document without specifying an ID using POST

POST /employee/_doc
{
  "name": "Sneha"
}

 

Error Cases

  1. Try using PUT to create doc without specifying id.

    1. Request:
      PUT /employee/_doc
      {
        "name": "Deny"
      }

    2. Response:
      {
        "error": "Incorrect HTTP method for uri [/employee/_doc] and method [PUT], allowed: [POST]",
        "status": 405
      }

  2. Try doing POST to /employee/

    1. Request:
      POST /employee/
      {
        "name": "Deny"
      }

    2. Response:
      {
        "error": "Incorrect HTTP method for uri [/employee/] and method [POST], allowed: [PUT, GET, DELETE, HEAD]",
        "status": 405
      }

    3. Try to do a POST with a different type field

      1. Request:
        POST /employee/another
        {
          "name": "Deny"
        }

      2. Response:
        ...
        "illegal_argument_exception",
            "reason": "Rejecting mapping update to [employee] as the final mapping would have more than 1 type: [_doc, another]"
          },
          "status": 400

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