How to Install and Use Elasticsearch With MongoDB on Windows and Ubuntu

Mehvish Ashiq Feb 16, 2024
  1. What Is the Elasticsearch
  2. Install Elasticsearch on Windows 10
  3. Install Elasticsearch on Ubuntu 20.04
  4. the Primary Reason to Use Elasticsearch With MongoDB
How to Install and Use Elasticsearch With MongoDB on Windows and Ubuntu

This article is a step-by-step installation and configuration guide for the Elasticsearch on Windows and Ubuntu 20.04. It also demonstrates the use of Elasticsearch with MongoDB for both operating systems.

What Is the Elasticsearch

Elasticsearch is a NoSQL database, an open-source real-time distributed and analytics engine designed to store logs. The Elasticsearch is developed in Java programming and on top of an Apache Lucene.

Many well-known companies, including LinkedIn and OpenStack, use it. It supports full-text search based on the documents instead of schemas and tables.

It is also used for data migration, indexing, and synchronization. Elasticsearch is the best choice when the application needs too many search operations and filters.

Install Elasticsearch on Windows 10

Step 1: Check if Java Is Installed

To install the Elasticsearch on Windows 10, we must have the latest version of Java installed on our machine. You may also use any of the following commands to confirm that Java is already installed on your machine.

Remember, we must have Java version 7 or greater if we want to install Elasticsearch.

install and use elasticsearch on windows and ubuntu - check java version on windows

Step 2: Download Elasticsearch .zip File

Go to this website and click on the download link in the following screenshot. Next, select the platform we want to use Elasticsearch, and download the .zip file.

install and use elasticsearch on windows and ubuntu - download elasticsearch on windows

Step 3: Run the Elasticsearch Batch File

Once we download the .zip file, unzip that to your desired location. Then, navigate to the bin folder and double-click on the elasticsearch batch file.

install and use elasticsearch with mongodb on windows and ubuntu - run elasticsearch on windows

It is nothing but a standard Windows batch file with steps performed behind the scenes. Further, it helps bring up the Elasticsearch daemon on your machine that will look as follows.

Leave that open and follow the next steps.

install and use elasticsearch on windows and ubuntu - elasticsearch daemon on windows

Step 4: Disable Authentication (Just for Learning Purposes)

By default, the authentication is enabled, which means we have to input the login credentials. You can either use the default login credentials or change them by following this page.

For this article, we are disabling the authentication (it is only for learning purposes but recommended in the production environment). We use Notepad to open the elasticsearch.yml file that can be located at the %ES_HOME%\config\elasticsearch.yml path.

Here, the ES_HOME is your Elasticsearch home directory. For instance, in our case, the elasticsearch.yml can be located at C:\Users\DelftStack\Desktop\elasticsearch-8.2.2-windows-x86_64\elasticsearch-8.2.2\config.

Once the elasticsearch.yml file is opened, search for the xpack.security.enabled option and make its value false, as shown in the screenshot below.

install and use elasticsearch on windows and ubuntu - disable elasticsearch authentication on windows

Stop the Elasticsearch and start again by running the elasticsearch.bat file.

Step 5: Run Elasticsearch on Browser

Type http://localhost:9200 on your favorite browser. If you see the page similar to the following screenshot, the Elasticsearch is successfully up and running.

install and use elasticsearch on windows and ubuntu - elasticsearch is up and running on windows

Install Elasticsearch on Ubuntu 20.04

Step 1: Install the Required Dependencies

Update/Upgrade Package Index:

sudo apt update
sudo apt upgrade

Like Windows OS, Java is also required on Ubuntu for installing the Elasticsearch. If it is not installed already, execute the command below to install the default JDK on Ubuntu 20.04.

sudo apt install openjdk-8-jdk

Once installed, use the java -version to check if it is there.

install and use elasticsearch on windows and ubuntu - check java version on ubuntu

We also need to install an APT Transport package to give access to all our repositories via HTTPS.

sudo apt install apt-transport-https

install and use elasticsearch on windows and ubuntu - install apt transport on ubuntu

Step 2: Download and Install Elasticsearch on Ubuntu

The next job is to add the Elasticsearch repository. Use the wget query to pull the public key.

You must have the OK as output if everything goes well.

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

install and use elasticsearch with mongodb on windows and ubuntu - add elasticsearch repository on ubuntu

Now, add the repository to the system by executing the following command. We are writing the 8.x in the following command because this is the latest version at the moment of writing this tutorial.

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

Once again, update the package index by using the command given below.

sudo apt update

Install the Elasticsearch by executing the following command. It may take some time to install.

So, let it complete successfully.

sudo apt install elasticsearch

install and use elasticsearch on windows and ubuntu - install elasticsearch on ubuntu

After installation, we have to start the Elasticsearch because it does not start itself. We also need to rerun the Elasticsearch if we reboot the machine.

To avoid this situation, execute the commands below to automatically reload the Elasticsearch after the system reboots.

sudo systemctl daemon-reload
sudo systemctl enable elasticsearch.service
sudo systemctl start elasticsearch.service

Now, check the status of the Elasticsearch by using the following command.

sudo systemctl status elasticsearch

install and use elasticsearch on windows and ubuntu - elasticsearch status on ubuntu

Step 3: Configure Elasticsearch

By default, the Elasticsearch is preconfigured for basic use. We do not have to make any changes in the configuration file if we want to use one node in our setup.

If you want to edit the Elasticsearch configuration, edit the elasticsearch.yml file in the /etc/elasticsearch directory. You can also find the configuration for logging at /var/log/elasticsearch/logging.yml.

We are editing the elasticsearch.yml file to disable the authentication for this tutorial. We change the value of xpack.security.enabled from true to false.

Remember, whenever you edit the elasticsearch.yml file, do not forget to restart the services using the following command.

sudo systemctl restart elasticsearch.service

Next, use the following command to check if the Elasticsearch is up and running.

curl localhost:9200

install and use elasticsearch on windows and ubuntu - elasticsearch is up and running on ubuntu

We can also type http://localhost:9200 on the browser, and it should show the same results as the curl command given above. Further, you can check this article to secure Elasticsearch.

install and use elasticsearch with mongodb on windows and ubuntu - run elasticsearch on ubuntu

the Primary Reason to Use Elasticsearch With MongoDB

We use MongoDB to store and query the data, while Elasticsearch is used for full-text indexing over the data. Hence, the combination of both (Elasticsearch for indexing and MongoDB for storing) is one of the common architectures that many companies follow.

Elasticsearch is developed for search purposes, and it also provides advanced capabilities for data indexing. It works alongside Kibana and Logstash for data analysis.

One of the advantages of using Elasticsearch is that it provides fast searching for all the fields within the specified document. We can think of it as having our personal Google search for our data.

We can also use the Elasticsearch in different scenarios. For instance, we can perform synchronization, build a search engine, data migration, indexing, and more.

We will learn to use Elasticsearch with MongoDB for synchronization purposes for this tutorial.

Use Elasticsearch for Performing Synchronization

We have to follow the steps given below to perform synchronization using Elasticsearch and MongoDB on Windows/Ubuntu. Before that, make sure you have all the dependencies.

For instance, we are using Node.js for this code example. Further, keep running both database engines (MongoDB and Elasticsearch).

After that, let’s follow the steps.

  • Import the mongoose and mongoosastic packages.
  • Establish a successful connection with the MongoDB engine.
  • Create a MongoDB schema.
  • Use the mongoosastic plugin to connect with Elasticsearch.
  • First, create the MongoDB model and then, Elasticsearch mapping.
  • Add data to your MongoDB database, and it must be synced with Elasticsearch.

Example Code (put the whole code in a file, we have it in the app.js file):

// Step 1:
const mongoose = require('mongoose');
const mongoosastic = require('mongoosastic');

// Step 2:
mongoose.connect('mongodb://localhost:27017/studentdb');

// Step 3:
var StudentSchema =
    new mongoose.Schema({firstname: String, lastname: String, city: String});

// Step 4:
StudentSchema.plugin(mongoosastic, {'host': 'localhost', 'port': 9200});

// Step 5:
var Student = mongoose.model('student', StudentSchema);
Student.createMapping((err, mapping) => {
  console.log('mapping created');
});

// Step 6:
var newStudent =
    new Student({firstname: 'Mehvish', lastname: 'Ashiq', city: 'Lahore'});

newStudent.save((err) => {
  if (err) {
    console.log(err);
  }
  console.log('Student is added in both databases');
})

newStudent.on('es-indexed', (err, result) => {
  console.log('indexed to elastic search');
});

Now, run this code; it would be as follows.

install and use elasticsearch with mongodb on windows and ubuntu - run file

Confirm the entry in MongoDB as follows.

install and use elasticsearch with mongodb on windows and ubuntu - confirm entry in mongodb

Next, open http://localhost:9200/students/_search to confirm it on Elasticsearch as well. It will look similar to the following screenshot.

install and use elasticsearch with mongodb on windows and ubuntu - confirm entry in elasticsearch

Mehvish Ashiq avatar Mehvish Ashiq avatar

Mehvish Ashiq is a former Java Programmer and a Data Science enthusiast who leverages her expertise to help others to learn and grow by creating interesting, useful, and reader-friendly content in Computer Programming, Data Science, and Technology.

LinkedIn GitHub Facebook