Elasticsearch is an open-source search engine based on Apache Lucene, offers a real-time distributed full-text search engine with an HTTP web interface and schema-free JSON documents.
Elasticsearch is developed in Java. The open-source version of Elasticsearch is released under the Apache 2.0 License, and the commercial version is released under Elastic License. Currently, it is the most popular enterprise search engine followed Apache Solr.
This post will help you to install Elasticsearch on CentOS 8 / RHEL 8.
Prerequisites
Contents
Elasticsearch is built using Java, and the package includes a bundled version of OpenJDK which means you need not to install Java separately for Elasticsearch installation. The bundled OpenJDK is located within the JDK directory of the Elasticsearch home directory /usr/share/elasticsearch.
Install Java
To use your own version of Java, install the LTS version of Java and set the JAVA_HOME environment variable on your system.
READ: How To install Java on CentOS 8 / RHEL 8
Verify the version of Java installed on the system.
java -version
Output:
java version "1.8.0_181" Java(TM) SE Runtime Environment (build 1.8.0_181-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
Set JAVA_HOME with the below command.
export JAVA_HOME=<JAVA_PATH>
After you install Elasticsearch on your system set JAVA_HOME in the /etc/sysconfig/elasticsearch file to let Elasticsearch use custom Java version.
Setup Elasticsearch Repository
Elastic offers a pre-built binary (rpm) package for RHEL and its derivatives. Download and install the Elastic’s public signing key.
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
Free Version
This package is free to use. It contains open source, free and paid commercial features. When you install a package, you will receive a basic license. You can visit the subscription page to list all the features included in the free and commercial versions.
You can start a 30-day trial to try all paid features. At the end of the trial period, the commercial features operate in a degraded mode. You can revert license to a basic license, extend the trial, or purchase a subscription.
cat << EOF > /etc/yum.repos.d/elasticsearch.repo [elasticsearch-7.x] 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 EOF
Open-source Version
This package is free to use and contains only features that are available under the Apache 2.0 License.
cat << EOF > /etc/yum.repos.d/elasticsearch.repo [elasticsearch-7.x] name=Elasticsearch repository for 7.x packages baseurl=https://artifacts.elastic.co/packages/oss-7.x/yum gpgcheck=1 gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch enabled=1 autorefresh=1 type=rpm-md EOF
Install Elasticsearch
Install Elasticsearch (v7.x) by using the following command.
Free Version
yum install -y elasticsearch
Open-source Version
yum install -y elasticsearch-oss
Enable Elasticsearch to auto-start during system startup.
systemctl daemon-reload systemctl enable elasticsearch systemctl start elasticsearch
Once you started the Elasticsearch service, wait for at least a minute to let it get fully started. Otherwise, testing will fail.
Elastisearch should now be listening on port 9200 for processing HTTP requests. Use curl to get the response.
curl -X GET 'http://localhost:9200'
You should get a response like below.
{ "name" : "centos8.itzgeek.local", "cluster_name" : "elasticsearch", "cluster_uuid" : "s6PwCJnoS7e7Pb3QMzkINg", "version" : { "number" : "7.4.0", "build_flavor" : "oss", "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" }
Configure Elasticsearch
Elasticsearch configuration files are found in /etc/elasticsearch/ directory. elasticsearch.yml is the main configuration file of Elasticsearch, and it contains default settings for running development cluster. Elasticsearch logs are stored in the /var/log/elasticsearch directory.
Cluster Name
The setting cluster.name is used to discover and auto-join node with other nodes. If a group of Elasticsearch nodes on the same network has the same cluster name, they will discover each other and form a cluster.
Change the default cluster name of Elasticsearch node to avoid auto-joining with other nodes on the same network. Make sure you are using a unique cluster name.
Edit elasticsearch.yml file.
vi /etc/elasticsearch/elasticsearch.yml
Replace els-cluster with your Elasticsearch cluster name.
cluster.name: els-cluster
Node Name
The node.name is like a hostname for the Elasticsearch server, dynamically generated during the service startup. You can set the Node name by updating the below line. Replace els-cluster-node01 with your Elasticsearch node name.
node.name: els-cluster-node01
Listening Address
Elasticsearch binds to localhost (127.0.0.1) and listens on port number 9200 for HTTP traffic by default. It uses port number 9300 – 9400 for communication between nodes within the cluster.
To form a multinode Elasticsearch cluster or bring the Elasticsearch node for production use to let applications to access Elasticsearch node, you need to configure Elasticsearch to listen to the system’s IP address.
### Listening on particular IPv4 ### network.host: 192.168.0.10
OR
### Listen on All IP Address ### network.host: 0.0.0.0
You also need to update the below settings with your system IP address.
discovery.seed_hosts: ["192.168.0.10"]
Restart the Elasticsearch service.
systemctl restart elasticsearch
Once you restart the Elasticsearch service, wait for at least a minute to let the Elasticsearch get fully started.
Check if Elasticsearch is listening on port 9200 on all IP addresses or a particular IP address with netstat command.
READ: netstat Command not found on CentOS 8 / RHEL 8
netstat -antup | grep -i 9200
Output:
tcp6 0 0 :::9200 :::* LISTEN 2143/java
Verify both Node Name and the Cluster Name have been set correctly by using the curl.
curl -X GET 'http://192.168.0.10:9200'
You should get a response like below.
{ "name" : "els-cluster-node01", "cluster_name" : "els-cluster", "cluster_uuid" : "s6PwCJnoS7e7Pb3QMzkINg", "version" : { "number" : "7.4.0", "build_flavor" : "oss", "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" }
Work With Elasticsearch
Let’s add some data to Elasticsearch. You can use the curl command to read, add, delete, and update the data into Elasticsearch over port 9200 using a RESTful API.
Documents are stored in the following format
Elasticsearch ⇒ Indices ⇒ Types ⇒ Documents ⇒ Fields
Indices: Plural of an index, where the data of Elasticsearch is stored.
Types: It contains multiple documents, and it is like the type of data.
Documents: It contains the data fields.
Fields: Actual data.
Add Data
Use the following curl command to add data to our Elasticsearch.
curl -XPUT 'http://localhost:9200/itzgeek/howtos/1' -H 'Content-Type: application/json' -d ' { "Title" : "Install Elasticsearch On RHEL 8", "Date" : "May 2019", "Tag" : "RHEL" }'
You should get the following response with “result”:”created.”
{"_index":"itzgeek","_type":"howtos","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
Where,
itzgeek: The index of the Elasticsearch cluster.
howtos: The type of document
1: The id of the entry under howtos and itzgeek index.
Read Data
Use the following command to query data on Elasticsearch.
curl -X GET 'http://localhost:9200/itzgeek/howtos/1'
Append ?pretty=true to the above command to get a formatted output.
curl -X GET 'http://localhost:9200/itzgeek/howtos/1?pretty=true'
The output will look like below.
{ "_index" : "itzgeek", "_type" : "howtos", "_id" : "1", "_version" : 1, "_seq_no" : 0, "_primary_term" : 1, "found" : true, "_source" : { "Title" : "Install Elasticsearch On RHEL 8", "Date" : "May 2019", "Tag" : "RHEL" } }
Update Data
Use the following command to update the data.
curl -XPUT 'http://localhost:9200/itzgeek/howtos/1' -H 'Content-Type: application/json' -d ' { "Title" : "Install Elasticsearch On RHEL 8", "Date" : "May 2019", "Tag" : "RHEL" }'
There will be no change in Index, type, and document. But, fields will have modified data.
The response should look like below, contain “_version”:2 and “result”:”updated” which means that the document has been updated.
{"_index":"itzgeek","_type":"howtos","_id":"1","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}
Remove Data
Use the following command to delete the document.
curl -X DELETE 'http://localhost:9200/itzgeek/howtos/1'
The response will look like below. If the document is found you will get “result”:”deleted”.
{"_index":"itzgeek","_type":"howtos","_id":"1","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}
If the document is not found, you will get “result”:”not_found”.
{"_index":"itzgeek","_type":"howtos","_id":"1","_version":4,"result":"not_found","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":3,"_primary_term":1}
Conclusion
I hope you learned how to install Elasticsearch on CentOS 8 / RHEL 8 and add, read, delete, and update data in Elasticsearch. You can also set up a multi-node Elasticsearch cluster to handle a large amount of data.