Elasticsearch is flexible and powerful open-source, distributed real-time search and analytics engine. Using a simple set of APIs provides the ability for full-text search. Elastic search is freely available under the Apache 2 license, which provides the most flexibility.

Step 1 – Prerequsities

Java is the primary requirement for installing Elasticsearch on any system. You can check the installed version of Java by executing the following command.

java -version

Step 2 – Setup Yum Repository

First of all, install GPG key for the elasticsearch rpm packages.

sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch

Then create yum repository file for the elasticsearch. Edit /etc/yum.repos.d/elasticsearch.repo file:

sudo vi /etc/yum.repos.d/elasticsearch.repo

Add below content:

[Elasticsearch-7]
name=Elasticsearch repository for 7.x packages
baseurl=https://artifacts.elastic.co/packages/7.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md

Step 3 – Install Elasticsearch

After adding yum repository, just install Elasticsearch on CentOS and RHEL system using the following command:

sudo yum install elasticsearch

After successful installation edit Elasticsearch configuration file “/etc/elasticsearch/elasticsearch.yml” and set the network.host to localhost. You can also change it to the system LAP IP address to make it accessible over the network.

vim /etc/elasticsearch/elasticsearch.yml
  network.host: localhost

Then enable the elasticsearch service and start it.

sudo systemctl enable elasticsearch
sudo systemctl start elasticsearch

The ElasticSearch has been successfully installed and running on your CentOS or RHEL system.

Run the following command to verify service:

curl -X GET "localhost:9200/?pretty"

You will see the results like below:

{
  "name" : "supportadmin",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "HY8HoLHnRCeb3QzXnTcmrQ",
  "version" : {
    "number" : "7.4.0",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "22e1767283e61a198cb4db791ea66e3f11ab9910",
    "build_date" : "2019-09-27T08:36:48.569419Z",
    "build_snapshot" : false,
    "lucene_version" : "8.2.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

Step 4 – Elasticsearch Examples (Optional)

The following examples will help you to add, fetch and search data in the Elasticsearch cluster.

Create New Bucket

curl -XPUT http://localhost:9200/mybucket

Output:

{"acknowledged":true}

Adding Data to Elasticsearch

Use following commands to add some data in Elasticsearch.
Command 1:

curl -XPUT 'http://localhost:9200/mybucket/user/johny' -d '{ "name" : "supportadmin " }'

Output:

{"_index":"mybucket","_type":"user","_id":"johny","_version":1,"created":true}

Command 2:

curl -XPUT 'http://localhost:9200/mybucket/post/1' -d '
{
    "user": "test",
    "postDate": "02-25-2019",
    "body": "This is Demo Post 1 in Elasticsearch" ,
    "title": "Demo Post 1"
}'

Output:

{"_index":"mybucket","_type":"post","_id":"1","_version":1,"created":true}

Command 3:

curl -XPUT 'http://localhost:9200/mybucket/post/2' -d '
{
    "user": "supportadmin",
    "postDate": "02-25-2019",
    "body": "This is Demo Post 2 in Elasticsearch" ,
    "title": "Demo Post 2"
}'

Output:

{"_index":"mybucket","_type":"post","_id":"2","_version":1,"created":true}

Fetching Data from Elasticsearch

Use the following command to GET data from ElasticSearch and read the output.

curl -XGET 'http://localhost:9200/mybucket/user/johny?pretty=true'
curl -XGET 'http://localhost:9200/mybucket/post/1?pretty=true'
curl -XGET 'http://localhost:9200/mybucket/post/2?pretty=true'

Searching in Elasticsearch

Use the following command to search data from elastic search. Below command will search all data associated with user johny.

curl 'http://localhost:9200/mybucket/post/_search?q=user:TecAdmin&pretty=true'

Output:

{
  "took" : 145,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.30685282,
    "hits" : [ {
      "_index" : "mybucket",
      "_type" : "post",
      "_id" : "2",
      "_score" : 0.30685282,
      "_source":
{
    "user": "supportadmin",
    "postDate": "02-25-2019",
    "body": "This is Demo Post 2 in Elasticsearch" ,
    "title": "Demo Post 2"
}
    } ]
  }
}