ElasticSearch Part 1 [Mappings]


For this tutorial we are using the "Sense - a Json aware interface to ElasticSearch"

1. creating a index in elasticsearch
PUT /ecommerce {}


2. creating a mapping named product
PUT /ecommerce/
{
    "mappings": {
        "product":{
            "properties": {
                "name":{
                    "type": "string"
                },
                "price":{
                    "type": "double"
                },
                "description":{
                    "type": "string"
                },
                "quantity":{
                  "type":"string"
                },
                "status":{
                    "type": "integer"
                },
                "categories":{
                    "type": "nested",
                    "properties": {
                        "name":{
                            "type": "string"
                        }
                    }
                },
                "tags":{
                    "type": "string"
                }
            }
        }
    }
}


3. putting data in the schema with id 1001

PUT /ecommerce/product/1001
{
    "name": "Inception Innovation Center",
    "price": 120.00,
    "description": "Welcome to heaven of code",
    "status": 1,
    "quantity": 2,
    "categories": [
        {"name":"innovation"},
        {"name":"programming"}
        ],
    "tags":["inception", "programming"]
}


4. Getting the item info having ID 1001
GET /ecommerce/product/1001
{
   "_index": "ecommerce",
   "_type": "product",
   "_id": "1001",
   "_version": 1,
   "found": true,
   "_source": {
      "name": "Inception Innovation Center",
      "price": 120,
      "description": "Welcome to heaven of code",
      "status": 1,
      "quantity": 2,
      "categories": [
         {
            "name": "innovation"
         },
         {
            "name": "programming"
         }
      ],
      "tags": [
         "inception",
         "programming"
      ]
   }
}

5. Lets say we want to update the above doc with one more category then what we need to do is 

POST /ecommerce/product/1001/_update
{
  "doc":{
         "categories": [
                {"name":"innovation"},
                {"name":"programming"},
                {"name":"inception"}
            ]
  }
}

here doc represent the the key-value pair of fields that needs to be update. Its a simple way to update and there is also another way to update for that we need to write entire properties like:

PUT /ecommerce/product/1001
{
    "name": "Inception Innovation Center",
    "price": 120.00,
    "description": "Welcome to heaven of code",
    "status": 1,
    "quantity": 2,
    "categories": [
                {"name":"innovation"},
                {"name":"programming"},
                {"name":"inception"}
            ],
    "tags":["inception", "programming"]
}

among this this I prefer the first one. The main advantage of second one is, if there is no product with id 1001 then it will create a new product with the specified id, which means there will be no data loss.
And the following is the output

{
   "_index": "ecommerce",
   "_type": "product",
   "_id": "1001",
   "_version": 2,
   "found": true,
   "_source": {
      "name": "Inception Innovation Center",
      "price": 120,
      "description": "Welcome to heaven of code",
      "status": 1,
      "quantity": 2,
      "categories": [
         {
            "name": "innovation"
         },
         {
            "name": "programming"
         },
         {
            "name": "inception"
         }
      ],
      "tags": [
         "inception",
         "programming"
      ]
   }
}

.Here the version of the doc has been changed.

1 comment: