Elastic search basics..!!(Add an index and Define Mapping) Part - 4

On the first three post I just gave the overview of How we can do hands on on elastic search  but know we will go somewhat deeper by the actual way to learn elastic search.

In the previous post we saw how to create the documents in elastic search , But did anyone wonder how the fields gets attached on the fly and Index as well Type gets created?

In the previous post we took example of student But now lets take an example that will be useful to us in future tasks.

Lets take an example of company where the employees are working.

So what we need first is to prepare a document of a company,by generating

  • Index
  • Type
  • fields(as per requirements)
  • Data Type Or Type of the respective fields.
These are the four basic things we need to deal with in order to deal a document in elastic search.

Lets get started we will define an index and a type so first of all in your Kibana we can write like....

PUT company

So we are making a PUT request and asking to create an index named company.

Note : Index Name will always start with a lowercase letter.

Now we need to create a mapping of fields , By defining a name to mapping ultimately the name we gave to mapping will be our type that we actually write after an index for querying.. Lets see how 


PUT company
{

  "mappings": {
}

We defined a Mapping by saying to index that we will provide the fields of documents, But we have to define a name where these documents rested so in future all operations will be on them.


PUT company
{
  "mappings": {
    "employee":{
}
}


Here we provided the Name to the mapping where our documents will reside.

Now we will define the properties of the documents of the type employee ,Actually we need to know that we have an index called company that is the database in that we have a table called employee here employee refers to the type than what all properties employee type /Table consists of ?

Defining the fields and there types...


PUT company
{
  "mappings": {
    "employee":{
      "properties": {
       }
}
}

We have a keyword called properties In which we will define all the fields now defining the fields of the taste of what the employee must have.

PUT company
{
  "mappings": {
    "employee":{
      "properties": {
        "employee_name":{
          "type": "text"
        },
        "employee_designation":{
          "type": "text"
        },
        "employee_experience":{
          "type": "integer"
        },
        "employee_salary":{
          "type": "float"
        },
        "employee_technology":{
          "type": "text"
        },
        "employee_dateofjoining":{
          "type": "date"
        }
      }
    }
  }
}


All in bold are the fields which we defined and we have the keyword type to specify the type of the respective fields.

Copy paste it to your Kibana and hit the triangle Button and observe the result.

Now here your document overview is prepared a person can know at least what the document will consists.

Again know think we want to view mapping of  what we did, How to see the fields and its data type that any document consists of..

GET company/_mapping

We can make a get request for it by index name and _mapping to see the mapping or we can also write like this 

GET company/employee/_mapping

And the result is 

{
  "company": {
    "mappings": {
      "employee": {
        "properties": {
          "employee_dateofjoining": {
            "type": "date"
          },
          "employee_designation": {
            "type": "text"
          },
          "employee_experience": {
            "type": "integer"
          },
          "employee_name": {
            "type": "text"
          },
          "employee_salary": {
            "type": "float"
          },
          "employee_technology": {
            "type": "text"
          }
        }
      }

The mappings are done now suddenly we remember that we want to add a field say Company Name But we already defined our mapping but yes , we can update our mappings by adding an extra feild as employee_company_name lets do it ..

PUT company/employee/_mapping
{
  "properties": {
    "employee_company_name":{
      "type": "text"
    }
  }

Here there is slight difference in the syntax because we have an index here so just we need to update that index and type.

Now again hit GET company/_mapping and observe the result

Result: 
{
  "company": {
    "mappings": {
      "employee": {
        "properties": {
          "employee_company_name": {
            "type": "text"
          },
          "employee_dateofjoining": {
            "type": "date"
          },
          "employee_designation": {
            "type": "text"
          },
          "employee_experience": {
            "type": "integer"
          },
          "employee_name": {
            "type": "text"
          },
          "employee_salary": {
            "type": "float"
          },
          "employee_technology": {
            "type": "text"
          }
        }
      }
    }
  }
}

So We clearly see that our mapping is altered and we can see employee_company_name added as a field and mapped with our index.

 This was all about creating an index and mapping ,updating our mapping and retrieving the same practice it well in the next post we will add some documents and will understand aggregations from basic level.

Comments

Popular posts from this blog

State space search / blocks world problem by heuristic approach /TIC TAC TOE

Navigation in Vaadin.

Drag and drop items from one Grid to another