chevron_left
70 points
7 3 1

Elastic Search : Technical Fundamentals  For Begineers

 
If are you a new to elastc search please go through with this blogs to clear about what the elastic search is : https://kodlogs.com/blog/11011/elastic-search-fundamental-for-begineers
 

The rest of the blog will be divided into 2 parts:

  1. Getting started with Elasticsearch: In this part we will cover the CRUD operations and perform search queries against the database.
  2. Settings, Mappings, Filters, Analyzers: This section will describe in detail on how elasticsearch analyzes and stores the data.

Part-1: Getting Started with Elasticsearch

Let’s create an index and add few documents in it. Index is equivalent to a ‘database’ in RDBMS while a document resembles a ‘row’. We will create an index named movies. Fire a PUT request from Postman http://localhost:9200/movies or you can use the cURL command curl -X PUT “http://localhost:9200/movies/”

If the index movies gets created successfully, you should see a response like below.

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "movies"
}


To check all the indexes that have been created invoke a GET request http://localhost:9200/_cat/indices? or use the cURL command
curl -X GET “http://localhost:9200/_cat/indices”

To insert records in the database fire a POST request http://localhost/movies/movie/ and pass the record in JSON format in the body. Make sure to set the Content-Type as application/json in the Headers section in the HTTP section. The corresponding cURL command is


curl -X POST "http://localhost:9200/movies/movie/" -H 'content-type: application/json' -d '{"name":"Justice League", "genre":"Action",
"summary":"Fueled by his restored faith in humanity and inspired by Superman's selfless act, Bruce Wayne enlists the help of his newfound ally, Diana Prince, to face an even greater enemy","yearofrelease":201, "metascore":45, "votes":275122, "rating":6.6}’


The above request creates a document in the index movies and the type movie. Type is equivalent to a table in RDBMS. The response for the above request will be the following

 

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

Observe that the result field is set to created. We can see from the response that elasticsearch assigned an_id automatically. We can specify our own _id by invoking a PUT request http://localhost:900/movies/movie/<id> and the response will be the below JSON


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

You can delete a particular record by passing the id and invoking the DELETE request http://localhost:9200/movies/movie/1 and the response will be 
{
    "_index": "movies",
    "_type": "movie",
    "_id": "1",
    "_version": 3,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

Searching

Elasticsearch is a very fast and powerful search engine. The _search API allows you to execute a search query against the index and get back the results that best match the query.

To get all the documents fire a GET request from the postman client http://localhost:9200/movies/_search. The response is:

 

{
    "took": 585,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 1,
        "hits": [
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "wH-YYmUBqmm5JjsAVTYV",
                "_score": 1,
                "_source": {
                    "name": "Christopher Robin",
                    "genre": "Comedy",
                    "summary": "A working-class family man, Christopher Robin, encounters his childhood friend Winnie-the-Pooh, who helps him to rediscover the joys of life",
                    "yearofrelease": 2018,
                    "metascore": 60,
                    "votes": 9648,
                    "rating": 7.9
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "vX-OYmUBqmm5JjsAvjZ6",
                "_score": 1,
                "_source": {
                    "name": "Justice League",
                    "genre": "Action",
                    "summary": "Fueled by his restored faith in humanity and inspired by Superman's selfless act, Bruce Wayne    enlists the help of his newfound ally, Diana Prince, to face an even greater enemy",
                    "yearofrelease": 2017,
                    "metascore": 45,
                    "votes": 275122,
                    "rating": 6.6
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "vn-VYmUBqmm5JjsAzTbZ",
                "_score": 1,
                "_source": {
                    "name": "Thor Ragnarok",
                    "genre": "Action",
                    "summary": "Thor is imprisoned on the planet Sakaar, and must race against time to return to Asgard and stop Ragnarök, the destruction of his world, at the hands of the powerful and ruthless villain Hela",
                    "yearofrelease": 2017,
                    "metascore": 74,
                    "votes": 374270,
                    "rating": 7.9
                }
            },
            {
                "_index": "movies",
                "_type": "movie",
                "_id": "v3-WYmUBqmm5JjsAljZ4",
                "_score": 1,
                "_source": {
                    "name": "Infinity War",
                    "genre": "Sci-Fi",
                    "summary": "The Avengers and their allies must be willing to sacrifice all in an attempt to defeat the powerful Thanos before his blitz of devastation and ruin puts an end to the universe",
                    "yearofrelease": 2018,
                    "metascore": 68,
                    "votes": 450856,
                    "rating": 8.6
                }
            }
        ]
    }
}

 

Elasticsearch is growing very fast and I would recommend you to keep an eye on the latest updates.

 

Thanks and support.

More Posts

Elastic Search : Fundamental for begineers darshan1507 - Jul 14
The fastest method to search for files in the Linux directory tree is to use the ____ command. sakshi - Sep 11
Automatically search for a remote control code for DirecTV amna - Apr 7
Which of the following commands is the fastest method to search a file for fixed strings Farkhanda Athar - Jun 13, 2020
PHP search the multidimensional array for key and return value. Farkhanda Athar - Apr 29, 2020
Unit Testing Technical Test & Interview Questions James Smith - Mar 26, 2020
Blockchain Fundamentals Tomas Herman - Jul 30
Binary search tree python Naymul01 - Oct 16
Fuzzy keyword search in cloud computing over encrypted data. Tania - Sep 15
Which Search Algorithm should be used on large arrays if speed is important? sakshi - Sep 11