How to Install Elasticsearch on CentOS, RHEL


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

This article will help you to install Elasticsearch on CentOS, RHEL, Ubuntu and Debian Systems.

Step 1. Install JAVA

Before installing elasticsearch make sure you have java installed on your system. If you do not have java installed use following article to install Java.

Install JAVA 7 or JAVA 8 on CentOS/RHEL
Install JAVA 8 on Ubuntu

Step 2. Setup Repositories

First we need to setup repositories in package manager as per our operating system.

On CentOS/RHEL:

Import the Public Signing Key for elasticsearch
# rpm –import http://packages.elasticsearch.org/GPG-KEY-elasticsearch

Now create a yum repository file /etc/yum.repos.d/elasticsearch.repo and add following content to this file.

[elasticsearch-1.1]
name=Elasticsearch repository for 1.1.x packages
baseurl=http://packages.elasticsearch.org/elasticsearch/1.1/centosgpgcheck=1
gpgkey=http://packages.elasticsearch.org/GPG-KEY-elasticsearchenabled=1

On Ubuntu/Debian:

Import the Public Signing Key first using following command
$ wget -O – http://packages.elasticsearch.org/GPG-KEY-elasticsearch | apt-key add –

Now edit /etc/apt/sources.list file and add below entry to enable the repository
deb http://packages.elasticsearch.org/elasticsearch/1.1/debian stable main

Step 3. Install Elasticsearch

After adding required packages repository, lets install the elasticsearch package On CentOS/RHEL:
# yum install elasticsearch

On Ubuntu/Debian:
$ sudo apt-get update$ sudo apt-get install elasticsearch

Step 4. Start Elasticsearch

Make sure you have configured JAVA_HOME environment variable properly. Use following commands to start elasticsearch.
# service elasticsearch start# /sbin/chkconfig –add elasticsearch

Step 5. Elasticsearch is now installed. Let’s edit the configuration:

# vi /etc/elasticsearch/elasticsearch.ymlcluster.name: thenationwdhttp.port: 9200
Optional – Move the Elasticsearch data directory.

Your linux install may be configured in such a way that your install partition is only a few Gb in size, and placing a large Elasticsearch index there is not ideal.

In which case you will want to move the index directory to a different, larger, location (in this example /var/elasticsearch).

# path.data: /path/to/data
to
path.data: /var/elasticsearch (Create Dir. mkdir /var/elasticsearch)

You will also want to restrict outside access to your Elasticsearch instance, so outsiders can’t read your data or shut down your Elasticseach cluster through the HTTP API. Find the line that specifies network.host and uncomment it so it looks like this:
network.host: localhost

Add the following line somewhere in the file, to disable dynamic scripts:
script.disable_dynamic: true

Then disable multicast by finding the discovery.zen.ping.multicast.enabled item and uncommenting so it looks like this:
discovery.zen.ping.multicast.enabled: false

Save and exit elasticsearch.yml.

Step 6. Open up /elasticsearch/bin/service/elasticsearch.conf on line 2 edit

To a number suitable for the size of your forum.
set.default.ES_HEAP_SIZE=1024

I recommend approximately 1 GB for the HEAP_SIZE per 1 million posts on your forum.

1 Million Posts: 1024
2 Million Posts: 2048
3 Million Posts: 3072
4 Million Posts: 4096 etc.

This will not mean the service will use all that available memory, however it will have it at its disposal if required. So for example a 3 Million Post forum would edit
set.default.ES_HEAP_SIZE=1024
to
set.default.ES_HEAP_SIZE=3072

Step 7. Verify Setup

You have all done, just need to verify setup. elasticsearch works on port 9200, open your browser to point your server on port 9200, You will find some thing like below output

URL:   http://192.168.3.119:9200/
URL:   http://192.168.3.119:9200/_nodes
URL:   http://192.168.3.119:9200/A/_status
URL:   http://192.168.3.119:9200/_cluster/health

Examples of Data Indexing
Following examples will help to add, get and search data in Elasticsearch.

1. Adding Data to Elasticsearch
Use following commands to add some data in elasticsearch.

Command 1:
curl -XPUT ‘http://srv1.tecadmin.net:9200/demo/user/johny’ -d ‘{ “name” : “Johny Disuja” }’Output: {“ok”:true,”_index”:”demo”,”_type”:”user”,”_id”:”johny”,”_version”:1}

Command 2:
curl -XPUT ‘http://srv1.tecadmin.net:9200/demo/post/1’ -d ‘{    “user”: “johny”,    “postDate”: “05-15-2013”,    “body”: “This is Demo Post 1 in ElasticSearch” ,    “title”: “Demo Post 1”}’Output: {“ok”:true,”_index”:”demo”,”_type”:”post”,”_id”:”1″,”_version”:1}

Command 3:
curl -XPUT ‘http://srv1.tecadmin.net:9200/demo/post/2’ -d ‘{    “user”: “johny”,    “postDate”: “06-06-2013”,    “body”: “This is Demo Post 2 in ElasticSearch” ,    “title”: “Demo Post 2”}’Output: {“ok”:true,”_index”:”demo”,”_type”:”post”,”_id”:”2″,”_version”:1}

2. Get Data from Elasticsearch
Use following command to GET data from Elasticsearch and read the output.

curl -XGET ‘http://srv1.tecadmin.net:9200/demo/user/johny?pretty=true’curl -XGET ‘http://srv1.tecadmin.net:9200/demo/post/1?pretty=true’curl -XGET ‘http://srv1.tecadmin.net:9200/demo/post/2?pretty=true’

3. Search in Elasticsearch
Use following command to search data from elastic search. Below command will search all data assiciated with user johny.

curl ‘http://srv1.tecadmin.net:9200/demo/post/_search?q=user:johny&pretty=true’

Output:
{
“took” : 27,
“timed_out” : false,
“_shards” : {
“total” : 5,
“successful” : 5,
“failed” : 0
},
“hits” : {
“total” : 2,
“max_score” : 0.30685282,
“hits” : [ {
“_index” : “demo”,
“_type” : “post”,
“_id” : “1”,
“_score” : 0.30685282, “_source” :
{
“user”: “johny”,
“postDate”: “05-15-2013”,
“body”: “This is Demo Post 1 in ElasticSearch” ,
“title”: “Demo Post 1”
}
}, {
“_index” : “demo”,
“_type” : “post”,
“_id” : “2”,
“_score” : 0.30685282, “_source” :
{
“user”: “johny”,
“postDate”: “06-06-2013”,
“body”: “This is Demo Post 2 in ElasticSearch” ,
“title”: “Demo Post 2”
}
} ]
}
}

Congratulations! You have successfully configured elasticsearch on your Linux system.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s