Elastic Search With Node.JS Part 1

Posted By : Hotam Singh | 23-Jan-2018
Elasticsearch With Node.JS Part 1

In this article, we are going to discuss getting started with elastic search and Node.JS. We are going to connect to the elastic search server, index documents and perform some basic search using Node.JS.

 

What is Elasticsearch?

Elastic search is an open-source, distributed search and analytics engine. It is built on Apache Lucene. The first version of Elasticsearch was released in 2010. It has quickly become the most popular search engine, and it is widely used for log analytics, text search etc. Elasticsearch can be used to provide near-real-time analytics using a big amount of log data. Elasticsearch is very much popular because of its easy-to-use searching APIs which allows us to add powerful search functionality into your application easily.

 

Features of Elasticsearch

There are some most important features about elastic search which made it the most popular search engine.

  1. Fast: Elastic search finds the match for our full-text search from a big data set quickly.
  2. Easy-to-use APIs: Elasticsearch provides REST-based API that uses schema-free JSON based documents. It helps to query our data very fast.
  3. Complementary tooling and plug-ins: Elasticsearch provides integration with kibana (A popular visualization and reporting tool) and logstash(Easily transform source data and data into an index).
  4. Real-time index updates: Elasticsearch updates the index in near real-time.
  5. Support for your favourite development language: Elasticsearch provides support for many languages that you can integrate with the language you love. Some of the supported languages are Java, Python, Node.JS, PHP, JavaScript, Ruby and many others.

 

Why Elasticsearch?

Elasticsearch is good for searching. It is based on core Apache engine(Lucene) which provides great search features. It is especially good for log analytics and its interface is suitable for JSON documents.

 

Set up Node Environment

First, install Node and npm if not already installed.

We’ll need following modules for getting started

  • elasticsearch
  • get-json

Install above modules using

 

Connect To Elasticsearch 

We can connect to and interact with elastic search cluster easily by using elasticsearch node module. We'll create a new file connection.js and will use this file in subsequent files.

Create a new file connection.js and paste the following code.

    var elasticsearch = require('elasticsearch');

    var client = new elasticsearch.Client({
        hosts: 'localhost:9200',
        log: 'trace'
        
        //configuration for production server
        /*hosts: [
    		'https://[username]:[password]@[server]:[port]/',
    		'https://[username]:[password]@[server]:[port]/'
    	]*/
    });
    
    module.exports = client;
    

If you want to use this configuration on production server, just uncomment the commented part and comment 

hosts: 'localhost:9200',

We will create a separate nodejs file for single functionality and we will combine them in-depth upcoming examples. So, now we need to test whether our connection.js code connects to elastic search cluster or not. To test, create a new file info.js and paste the following code.

    var client = require('./connection.js');

    client.cluster.health({},function(err,resp,status) {  
        console.log("-- Client Health --",resp);
      });
      

When you will run this code, you will get output like this as below:

If you do not get output like this, go back and check your connection configuration. Otherwise, it is time to create an index.

 

Indexing

Indexing in elastic search is a bit different in comparison to indexing in other databases. 

In Elasticsearch, an index is a place where we can store related documents and the process of storing these documents is called indexing. In case of other database systems, we need to specify and create indexes so that it can improve the efficiency of related operations. In elasticsearch, this indexing is automatically created. In elasticsearch, When we index a document, all fields in a document are indexed by default.We will demonstrate creating indices and storing documents in it. 

Now we are going to create an index "test". Create a new file create.js and paste the following code.

    var client = require('./connection.js');

    client.indices.create({
        index: 'test'
    }, function (err, resp, status) {
        if (err) {
            console.log(err);
        }
        else {
            console.log("index created : ", resp);
        }
    });
    

Save this file and run using node create.js, you will get something like this as below:

 

Deleting an Index

Deleting an index is quite easy as simple as creating an index. Create a new file delete.js and paste following code

   var client = require('./connection.js');

    client.indices.delete({ index: 'test' }, function (err, resp, status) {
        console.log("index deleted", resp);
    });
    

Save this file and run using node delete.js command, you will get something like this:

 

Next

In next article, we will discuss adding documents to an index, adding documents in a bulk and basic searching in elasticsearch.

About Author

Author Image
Hotam Singh

Hotam has 1.5 years of experience in Node.JS. He has worked on Front End, JavaScript, Jquery, Backbone.JS, Database: MySQL, MongoDB. His hobbies are playing Sudoku/Puzzles and watching movies.

Request for Proposal

Name is required

Comment is required

Sending message..