The Introduction and Quick Set up of Elasticsearch.

The Introduction and Quick Set up of Elasticsearch.

Search Engines will use a database to store the metadata to search. Using a Relational database in Search engines will retrieve metadata from multiple schema and tables. This will slow down the search process. Using NoSQL might help to overcome this issue, but we need more search engine-oriented engine to serve our purpose. So, here comes Elasticsearch. Elasticsearch is a Cross-platform Search Engine which is developed based on Apache Lucene. It will store, retrieve and manage the structured and document-oriented data. So, we can call Elasticsearch is a NoSQL distributed database. This will not have a schema, but it will index the data to quickly search. In this article, we will discuss the Introduction and Quick Set up of Elasticsearch.

Concepts of Elasticsearch

Concept of Elasticsearch is very simple. Before getting into the Working Concept, lets discuss the keywords which will help us understanding Elasticsearch easy.

  • Index: Elasticsearch application will have Index which is like schema in RDBMS, that will store similar kinds of documents. It is like an Index of the book which is useful to find “on which page data is”. Then, Elasticsearch uses Inverted Index which is based on Apache Lucene. Inverted Index means the index that searches from the keyword-centric data structure. But in normal indexing, the page-centric data structure is used.
  • Shards: It is the subset of an index. Which means, an Index is split into shards.
  • Nodes: Node is an instance that will have the part of metadata and it will perform indexing and querying.
  • Cluster: It is the group of one or more nodes. Nodes can be joined into any cluster by just configuring it.
  • Mapping type: Mapping type is like tables in RDBMS, which is like a property type of the index.
Concept of Elasticsearch
Concept of Elasticsearch
  • Query DSL: Since elastic Elasticsearch is based on Lucene, Query DSL will be composed using a JSON syntax. So, through API call, we can query as mentioned below.
$ curl -X POST "http://localhost:9200/app/_search?pretty=true" -d ‘
{
	"query": {
		"filter" : {
			"healt_status" : {
				"heart_rate" : "70",
			}
		}
	}
}’

Querying in Elasticsearch is very easy and capability is up to the level of developer’s creativity. So, Let’s see the Quick Set Up of Elasticsearch.

FutureLearn Limited

Quick Set Up of Elasticsearch

Since Elasticsearch is an Opensource application, installing it is very easy and it can be installed in couple of minutes.

Installing Elasticsearch.

In this article, we will see the installation of Elasticsearch in Windows, Linux and Mac environment.

Installing on Windows:

  1. Download the package (.zip) from the Elasticsearch artifact (https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0-windows-x86_64.zip)
  2. Then, Unzip the downloaded .zip file and keep it in your continent location
  3. Run Elasticsearch application using batch file inside bin folder (elasticsearch.bat)
  4. (Optionally). Run the batch file inside bin folder for making this windows service (elasticsearch-service.bat install)

Also, You can install using .msi file (https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.4.0.msi) with GUI.

Installing on Linux/Mac Environment.

  1. Install JDK 6 and above
  2. Then, Download Tarbell file (wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.2.0-linux-x86_64.tar.gz)
  3. Extract the file (tar -zxvf elasticsearch-7.2.0-linux-x86_64.tar.gz)
  4. Run the Executable file from bin folder (bin/elasticsearch)
  5. (Optionally) Run the Elasticsearch as Daemon (./bin/elasticsearch -d -p pid).

Regardless of the Operating system, Elasticsearch runs in port 9200. So, this can be accessible from http://localhost:9200 and you will get the following output.

{
  "name" : "localhost",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "0YRYO4OR26sHSZSZYO26sH",
  "version" : {
    "number" : "7.2.0",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "c98a2ba",
    "build_date" : "2019-10-15T10:12:22.325720Z",
    "build_snapshot" : false,
    "lucene_version" : "8.0.0",
    "minimum_wire_compatibility_version" : "6.8.0",
  },
  "tagline" : "You Know, for Search"
}

If you see the above on your web browser, your installation is done. Let’s see how to Index the data with the installed Elasticsearch server.

FutureLearn Limited

Basic Indexing.

API service of Elasticsearch made the Indexing process very easy. So, just by running the API with JSON payload with the cURL command, you can index the data.

$ curl -XPUT 'http://localhost:9200/app/user/digitalvarys' -d '{ "name" : "Digital Varys" }'
	
$ curl -XPUT 'http://localhost:9200/app/instance/1' -d '
{ 
    "user": "digitalvarys", 
    "instanceDate": "2019-10-15", 
    "description": "This is sample content for the application instance 1" ,
    "title": "app one"
}'
$ curl -XPUT 'http://localhost:9200/app/instance/2' -d '
{ 
    "user": "digitalvarys", 
    "instanceDate": "2019-10-15", 
    "description": "This is sample content for the application instance 2" ,
    "title": "app two"
}'

For every CURL command, you will get the following output.

{"ok":true,"_index":"app","_type":"instance","_id":"1","_version":1}
{"ok":true,"_index":"app","_type":"instance","_id":"2","_version":1}

If you see this output, Indexing was done successfully.

Searching in Elastic search.

Just like Indexing in Elasticsearch, we can search document in Elasticsearch using API calls line below.

$ curl 'http://localhost:9200/app/instance/_search?q=-title:two&pretty=true'

This will through the result in JSON format.

 {
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "app",
      "_type" : "instance",
      "_id" : "2",
      "_score" : 1.0, "_source" : 
		{ 
		    "user": "digitalvarys", 
		    "postDate": "2019-10-15", 
		    "description": "This is sample content for the application instance 2" ,
		    "title": "app two"
		}
    }]
  }

As you can see, the relevant document is coming as output in JSON format and this can be utilized in any available programming language.

These operations can be accessed using two ways

  1. API with JSON Payload
  2. Elasticsearch native client

Conclusion

In this article, we have discussed the Introduction and Quick Set up of Elasticsearch. Other than this, we have more operations and activities in Elasticsearch. Like, scaling Elasticsearch, Dockizing Elasticsearch, Practical Implementation of Elasticsearch and more. Also, we will see more Elasticsearch tutorials in our upcoming Article. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOpsAgileDevSecOps and App Development.

1 thought on “The Introduction and Quick Set up of Elasticsearch.”

Leave a Reply