Elasticsearch is a free, open-source, distributed search and analytics engine capable of handling a large amounts of data. It is used for real-time full-text searches in applications where a large amount of data needs to be analyzed. It is very popular due to its usability, powerful features, and scalability. It supports RESTful with an HTTP URI to manipulate data. Elasticsearch is easy to use, offering features such as automatic node recovery, improved security, scalability and resiliency, automatic data balancing, and more.
In this post, we will show you how to install and configure Elasticsearch on Rocky Linux 8.
Step 1 – Install Java
Elasticsearch is a Java-based application, so Java must be installed on your server. If not installed, you can install it by running the following command:
dnf install java-11-openjdk-devel -y
After the installation, verify the Java version using the following command:
java --version
Sample output:
openjdk 11.0.12 2021-07-20 LTS OpenJDK Runtime Environment 18.9 (build 11.0.12+7-LTS) OpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7-LTS, mixed mode, sharing)
Step 2 – Create Elasticsearch Repository
By default, Elasticsearch is not included in the Rocky Linux default repository, so you will need to create a repository for it.
First, download and import the GPG key with the following command:
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
Next, create an Elasticsearch repo with the following command:
nano /etc/yum.repos.d/elasticsearch.repo
Add the following lines:
[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
Save and close the file when you are finished.
Step 3 – Install and Configure Elasticsearch
Now, install the Elasticsearch package with the following command:
dnf install elasticsearch -y
After installing Elasticsearch, edit the Elasticsearch main configuration file:
nano /etc/elasticsearch/elasticsearch.yml
Change the following lines:
cluster.name: test cluster node.name: node-1 path.data: /var/lib/elasticsearch network.host: 127.0.0.1
Save and close the file, then start the Elasticsearch service and enable it to start at system reboot:
systemctl start elasticsearch systemctl enable elasticsearch
Now, check the status of the Elasticsearch with the following command:
systemctl status elasticsearch
You should get the following output:
● elasticsearch.service - Elasticsearch Loaded: loaded (/usr/lib/systemd/system/elasticsearch.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2021-09-22 07:21:47 UTC; 16s ago Docs: https://www.elastic.co Main PID: 8075 (java) Tasks: 68 (limit: 11411) Memory: 1.1G CGroup: /system.slice/elasticsearch.service ├─8075 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=1> └─8240 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller Sep 22 07:20:41 RockyLinux8 systemd[1]: Starting Elasticsearch... Sep 22 07:21:47 RockyLinux8 systemd[1]: Started Elasticsearch.
You can now verify Elasticsearch using the following command:
curl -X GET 'http://localhost:9200'
If everything is fine, you should get the following output:
{ "name" : "node-1", "cluster_name" : "test cluster", "cluster_uuid" : "DaBUNHdlSxCaPMt8IpxGVw", "version" : { "number" : "7.14.2", "build_flavor" : "default", "build_type" : "rpm", "build_hash" : "6bc13727ce758c0e943c3c21653b3da82f627f75", "build_date" : "2021-09-15T10:18:09.722761972Z", "build_snapshot" : false, "lucene_version" : "8.9.0", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1" }, "tagline" : "You Know, for Search" }
Step 4 – How to Use Elasticsearch
After installing Elasticsearch, we will add some data and use manual queries to test Elasticsearch’s functionality.
Add some content to the Elastisearch using the Curl command:
curl -H 'Content-Type: application/json' -X POST 'http://localhost:9200/tutorial/helloworld/1' -d '{ "message": "Hello World!" }'
Sample output:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
Now, run the following command the retrieve the added entry:
curl -X GET 'http://localhost:9200/tutorial/helloworld/1'
Sample output:
{"_index":"tutorial","_type":"helloworld","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":{ "message": "Hello World!" }}
If you want to modify the existing entry, run the following command:
curl -H 'Content-Type: application/json' -X PUT 'localhost:9200/tutorial/helloworld/1?pretty' -d ' { "message": "Welcome Back!" }'
Sample output:
{ "_index" : "tutorial", "_type" : "helloworld", "_id" : "1", "_version" : 2, "result" : "updated", "_shards" : { "total" : 2, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }
Now, retrieve the modified data and display it in a human-readable format using the following command:
curl -X GET 'http://localhost:9200/tutorial/helloworld/1?pretty'
Sample output:
{ "_index" : "tutorial", "_type" : "helloworld", "_id" : "1", "_version" : 2, "_seq_no" : 1, "_primary_term" : 1, "found" : true, "_source" : { "message" : "Welcome Back!" } }
Conclusion
In the above guide, you learned how to install and use Elasticsearch on Rocky Linux 8. You can now use Elasticsearch with other tools, such as Kibana and Logstash to search and display data via a graphical interface. Start using Elasticsearch on dedicated hosting from Atlantic.Net.