Elasticsearch is an open-source search and analytic engine used to add search functionality to web-based applications. It is capable of searching and analyzing a large amount of data. It is powerful, stable, scalable, and supports RESTful with an HTTP URI to manipulate data. It is based on the Lucene library, written in Java, and is dual-licensed under the source-available Server Side Public License and the Elastic license. It has the ability to index many types of content including, a website search, application search, security analytics, enterprise search, geospatial data analysis and visualization, and more.

In this post, we will show you how to install and configure Elasticsearch on OracleLinux 8.

Step 1 – Install Java

Elasticsearch is a Java-based application, so you will need to install Java on your server. You can install it by running the following commands:

dnf update -y
dnf install java-11-openjdk-devel -y

Once Java is installed, verify the Java version using the following command:

java --version

Sample output:

openjdk 11.0.15 2022-04-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.15+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.15+10-LTS, mixed mode, sharing)

Step 2 – Create Elasticsearch Repository

By default, Elasticsearch is not included in the OracleLinux 8 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

After creating an ElasticSearch repo, 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

You can also check the status of 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 Sun 2022-05-29 12:41:40 EDT; 10s ago
     Docs: https://www.elastic.co
 Main PID: 3147 (java)
    Tasks: 76 (limit: 23694)
   Memory: 2.2G
   CGroup: /system.slice/elasticsearch.service
           ├─3147 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=>
           └─3355 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

May 29 12:41:12 oraclelinux8 systemd[1]: Starting Elasticsearch...
May 29 12:41:40 oraclelinux8 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" : "yA5vM_azSxu4oqGWB5lk4A",
  "version" : {
    "number" : "7.17.4",
    "build_flavor" : "default",
    "build_type" : "rpm",
    "build_hash" : "79878662c54c886ae89206c685d9f1051a9d6411",
    "build_date" : "2022-05-18T18:04:20.964345128Z",
    "build_snapshot" : false,
    "lucene_version" : "8.11.1",
    "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

At this point, ElasticSearch is installed and running. Now, we will add some data and use manual queries to test the Elasticsearch functionality.

First, 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!" }'

You should see the following 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 comment:

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
}

If you want to retrieve the modified data and display it in human-readable format, run 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, we explained how to install and use Elasticsearch on OracleLinux 8. You can now use Elasticsearch with your application to search and analyze data. Give it a try on your dedicated server from Atlantic.Net!