Getting started With Elastic Search(Part - 2)

In the previous post we had seen the installation of Elastic search and Kibana,Hope all have successfully installed and ready.


  1. Start elastic search by double click on elasticsearch.bat file and just hit url as                             http://localhost:9200/ 
  2. Start Kibana by double click on kibana.bat file and hit url as http://localhost:5601
These are the two things we require to work with ,Opening Kibana you will find Dev Tools option.

.
  • By clicking you will find two panes The left pane has your commands, which should be formatted with the HTTP action (GET, POST, PUT) and the URL .
  • In the case of a GET, that will be all you need. In the case of a POST or PUT with a body, you’ll provide the body as JSON on subsequent lines.
  • To run the command, simply click the green arrow next to it.

Initially We are new here so we don't have data to work with so let's create some data for that we have to get introduced with the document.

 What are Documents in elasticsearch?

              Most entities or objects in most applications can be serialized into a JSON object, with keys                and values.That Json objects we can refer similar as Documents but here in                                          elasticsearch document has a specific meaning. It refers to the top-level, or root object that                  is serialized into JSON and stored in Elasticsearch under a unique ID.

Lets understand with the help of an example.

We will take a simple example to get understand the term document lets have a Student object we will take some of its properties like studentName ,StudentId ,Hobbies, favourite sport.

Additionally,Student will also have an ID to make it easier to look them up by that reference, but the ID won’t be part of the document we create. 


So, let’s create our first Student:

POST /college/student/1
{
  "studentName " : "kushal baldev",
  "studentId " : 1,
  "hobbies" : "play cricket read books and writing blogs",
  "sport" : "cricket"
}

paste it in the left pane of kibana and hit the triangle button.

you will get a response like

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


from the above two we need to understand what is _index and _type

An index is like a ‘database’ in a relational database. It has a mapping which defines multiple types.

MySQL => Databases => Tables => Columns/Rows
Elasticsearch => Indices => Types => Documents with Properties

So here our Database that is index we have created is college and in it we have Tables that is Types as student , So we can say like in our Database named as college we have a table which is named as student and we are inserting Data in it as we have defined in Json format.

Now lets have some more data

POST /college/student/2
{
  "studentName " : "Yogesh Dhedhi",
  "studentId " : 2,
  "hobbies" : "Active discussions and debate",
  "sport" : "pool and snokker"
}

Here we have defined another student by Id 2 and here the respose is like

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


We will create another two records for the variety of data as 


POST /college/student/3
{
  "studentName " : "John",
  "studentId " : 3,
  "hobbies" : "horse riding",
  "sport" : "Long jump"
}


POST /college/student/4
{
  "studentName " : "Darshan",
  "studentId " : 4,
  "hobbies" : "shopping",
  "sport" : "football"
}

So far we have created Documents now we will see how to get the data from documents

1) GET /college/student/_search 
      This will search all your documents.

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 4,
    "max_score": 1,
    "hits": [
      {
        "_index": "college",
        "_type": "student",
        "_id": "2",
        "_score": 1,
        "_source": {
          "studentName ": "Yogesh Dhedhi",
          "studentId ": 2,
          "hobbies": "Active discussions and debate",
          "sport": "pool and snokker"
        }
      },
      {
        "_index": "college",
        "_type": "student",
        "_id": "4",
        "_score": 1,
        "_source": {
          "studentName ": "Darshan",
          "studentId ": 4,
          "hobbies": "shopping",
          "sport": "football"
        }
      },
      {
        "_index": "college",
        "_type": "student",
        "_id": "1",
        "_score": 1,
        "_source": {
          "studentName ": "kushal baldev",
          "studentId ": 1,
          "hobbies": "play cricket read books and writing blogs",
          "sport": "cricket"
        }
      },
      {
        "_index": "college",
        "_type": "student",
        "_id": "3",
        "_score": 1,
        "_source": {
          "studentName ": "John",
          "studentId ": 3,
          "hobbies": "horse riding",
          "sport": "Long jump"
        }
      }
    ]
  }
}


We have 4 Documents in our index that's similar to select * from student from our college database

To get particular record by Id we can refer as 

GET /college/student/1
GET /college/student/2
GET /college/student/3 

Try to hit and observe the result. Now we will go in deep to find specific results.

The REST API for search is accessible from the _search endpoint. We will make use of it 

Here In elastic search we will have a concept of query DSL(Domain Specific Language)

For starting we can assume that Lucene is a powerful Mechanism or a machine helping elastic search to figure out the records we need.

To use Lucene we have elastic search as a point of contact.

To use the Query DSL, pass a query in the query parameter:

GET /_search
{
    "query": YOUR_QUERY_HERE
}

Now hit the query

POST /college/student/_search
{
    "query" :
    {
       "match_all": {}
    }
}

This will return everything its simply means match all documents.

Now lets say we need to find a student who has cricket as hobby.

So you have two words in mind a hobbies as a key and cricket for that we will use a match clause as 

POST /college/student/_search
{
    "query" :
    {
       "match": {
         "FIELD": "TEXT"
       }
    }
}

FIELD is a key and Text is the value which we want to find. Now here our query will form like 

POST /college/student/_search
{
    "query" :
    {
       "match": {
         "hobbies": "cricket"
       }
    }
}

hit the query and observe the result the result will be 

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "college",
        "_type": "student",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "studentName ": "kushal baldev",
          "studentId ": 1,
          "hobbies": "play cricket read books and writing blogs", 
          "sport": "cricket"
        }
      }
    ]
  }
}


Here we only mentioned hobbies as cricket then how it matched the whole string highlighted in our record?

The string field has split into two new types: text, which should be used for full-text search, and keyword, which should be used for keyword search.

elastic search will allow you by providing hints whether we want a text based search or keyword based search.

In above query is for text based search now replace that query with 

POST /college/student/_search
{
    "query" :
    {
       "match": {
         "hobbies.keyword": "cricket"
       }
    }
}

We will get no result there because it went to search for the specific keywords  but incase if we provide as

POST /college/student/_search
{
    "query" :
    {
       "match": {
         "hobbies.keyword": "play cricket read books and writing blogs"
       }
    }
}


Here we provided the full String to match for the records but in text base searching it will search just by getting that word.

In the next post we will explore some more concepts about querying in elastic search..!! 

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