Share AT

Reading Time: 5 mins.

Overview:

Elasticsearch is a free and open source analytics and full-text search engine, i.e. fully document-based rather than schema and tables. It is widely-used for enabling search functionality for applications, i.e to search various kinds of data such as products, blog posts, categories on a web-shop, blogs and other product-based sites and applications. Similar to Google search engine, you can build a complex search functionality using Elasticsearch, and it involves correcting-typos, auto-completion, adjusting relevance, highlighting matches, handling synonyms and so much more.

Developed by Java, it is popularly used for Single Page Application Projects. Apart from using Elasticsearch as a full-text search, you can also perform query structured data such as aggregate data and numbers, and then use Elasticsearch as an analytics platform.

Prerequisites:

  1. An Ubuntu system logged in with sudo privileges (non-root user).
  2. Installed OpenJDK 11, and used a minimum volume of 4GB RAM and 2CPUs (the volume differs based on the volume of logs that your Elasticsearch needs.

Step 1: Installing and Checking Java (Version)

Make sure you have already installed Java, if not use the below commands to install it. Generally Elasticsearch 7.x will work with Java 8, but the recommended one is Java 11. Therefore, here we are going to install Java 11.

sudo apt install openjdk-11-jdk

Soon after the installation, you need to check Java and its jdk version.

sudo Javac --version

Output

javac 11.0.8

To check the jdk version, type the below command:

sudo java --version

Output

openjdk 11.0.8 2020-07-14
OpenJDK Runtime Environment (build 11.0.8+10-post-Ubuntu-0ubuntu120.04)
OpenJDK 64-Bit Server VM (build 11.0.8+10-post-Ubuntu-0ubuntu120.04, mixed mode, sharing)

Note: As mentioned earlier, Elasticsearch is developed using Java. Therefore Java is mandatory for installing and using Elasticsearch on any system. For more details, go to the official site: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/setup.html

To check whether your environment variable, JAVA_HOME is configured, enter the following command.

sudo echo $JAVA_HOME
output
/usr/lib/jvm/java-11-oracle

Step 2: Installing Elasticsearch on Ubuntu

You can install Elasticsearch from its official apt repository. To download the apt packages, type:

sudo apt-get install apt-transport-https

Next, import the GPG key for the downloaded Elasticsearch packages.

sudo curl -fsSL https://artifacts.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -

Output

Ok

Now, you have to configure the apt repository to add the repository to the sources.list.d directory using the below command.

sudo echo "deb https://artifacts.elastic.co/packages/7.x/apt stable main" | sudo tee -a /etc/apt/sources.list.d/elastic-7.x.list

Output

deb https://artifacts.elastic.co/packages/7.x/apt stable main

To install Elasticsearch packages on your Ubuntu system, first update the cache by typing the below command followed by the install command.

sudo apt-get update
sudo apt-get install elasticsearch

To check the status of the Elasticsearch service, type:

sudo service elasticsearch status

Output:

● elasticsearch.service - Elasticsearch
Loaded: loaded (/lib/systemd/system/elasticsearch.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2020-08-20 10:42:44 IST; 16s ago
Docs: https://www.elastic.co
Main PID: 14888 (java)
Tasks: 64 (limit: 18736)
Memory: 1.2G
CGroup: /system.slice/elasticsearch.service
├─14888 /usr/share/elasticsearch/jdk/bin/java -Xshare:auto -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.head>
└─15093 /usr/share/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller

Aug 20 10:42:28 adoldev-HP-EliteBook-840-G2 systemd[1]: Starting Elasticsearch...
Aug 20 10:42:44 adoldev-HP-EliteBook-840-G2 systemd[1]: Started Elasticsearch.

Note: Here we go with Elasticsearch version 7.x, if you’re looking for a specific version, please find it from the official repository for more details, and navigate to the respective version in the drop-down to get your required version https://www.elastic.co/guide/en/elasticsearch/reference/6.7/release-notes-6.7.0.html

Step 3: Configuring Elasticsearch

Now that the Elasticsearch has been installed on your system, you can edit the Elasticsearch configuration file to update the port and host numbers.

Open the configuration file using the following command:

sudo nano /etc/elasticsearch/elasticsearch.yml
Find the following network directive, by default it will look like the following image.

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
#network.host: 192.168.1.0
#
# Set a custom port for HTTP:
#
#http.port: 9200
#
# For more information, consult the network module documentation.

Here, we need to enable the network and port section as shown in below.

# ---------------------------------- Network -----------------------------------
#
# Set the bind address to a specific IP (IPv4 or IPv6):
#
network.host: 127.0.0.1
#
# Set a custom port for HTTP:
#
http.port: 92
#
# For more information, consult the network module documentation.

Note: If you are using Elasticsearch on a remote server, you have to add the respective ip address and enable the firewall rule for incoming and outgoing requests.

  1. network.host – To make it public, change the network host as 0.0.0.0 so as to listen on all interfaces. For LAN access, you can only use a LAN address.
  2. cluster.name – It simply defines the name of the cluster. In the case of a multi-node cluster, every node must follow the same cluster name.
  3. node.name – This is to set a unique name for every node in order to identify in a cluster.

Step 4: Running Elasticsearch

To make the Elasticsearch automatically up and running when the system boots up, type the below code.

sudo /bin/systemctl enable elasticsearch.service

As you can see in the below code, Elasticsearch will automatically start and stop the service.

sudo systemctl start elasticsearch.service
sudo systemctl stop elasticsearch.service

Step 5: Testing Elasticsearch

From the previous step, we have verified that the Elasticsearch service is all set to use. To test its setup, run the “GET” command using the curl command-line utility.

You will see the Elasticsearch cluster details with the version on your screen.

sudo curl -XGET 'http://localhost:9200'

The below output shows the Elasticsearch’s cluster details along with the version number.

Output

{
"name" : "adoldev-HP-EliteBook-840-G2",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "BOeHk29-Q2Gi5UbC-982JQ",
"version" : {
"number" : "7.9.0",
"build_flavor" : "default",
"build_type" : "deb",
"build_hash" : "a479a2a7fce0389512d6a9361301708b92dff667",
"build_date" : "2020-08-11T21:36:48.204330Z",
"build_snapshot" : false,
"lucene_version" : "8.6.0",
"minimum_wire_compatibility_version" : "6.8.0",
"minimum_index_compatibility_version" : "6.0.0-beta1"
},
"tagline" : "You Know, for Search"
}

Note: If you want to see the detailed view of Elasticsearch indexes, nodes and indices, you can go with ELK stack https://www.elastic.co/what-is/elk-stack

Conclusion:

In this article, you have gone through what Elasticsearch is, why it is used followed by the installation procedures of Elasticsearch on Ubuntu 20.04. Started off with the prerequisites for installing Elasticsearch, you then verified the installation of Java and OpenJDK, downloaded the apt packages, installed Elasticsearch and configured the necessary settings. To validate the successful installation of Elasticsearch and to validate whether its service is active, we then used the curl command-line utility. Need to configure Elasticsearch with Magento? Visit the following blog post on How to Configure Elasticsearch on Magento 2.x.x