Elasticsearch & Node.js [Getting Started]

Siddhartha Chowdhury
3 min readJan 23, 2017

Hello guys recently I have been working on implementing Elasticsearch on a Node.js platform and I found there is but very limited amount of information out there combining the two. Though the official documentation of Elasticsearch is quite adequate but its generic. So I decided to share my experience here.

Versions:
Elasticsearch 5.1.1
Node.js v6.9
Ubuntu 14.04

So here’s the set of basic requirements.

  1. Node.js and Java to be installed in your local system
  2. Download and execute/run Elasticsearch. [help]
  3. Create a node.js project (I was using Sails.js)
  4. Install npm package “elasticsearch
  5. Postman to test the APIs

THE BASIC OPERATIONS

Before I start explaining on how to configure your project to get Elasticsearch up and running , let me put down the basic operations below:

JavaScript Service / Library module

Now there are actually, only 3 things to be done to get it working.( 1 ) I started with creation of a “service” say a “library” of Elasticsearch CRUD operation (picture above). (2) Pointed each of the operations to a “route”. (3) Then I created a collection of REST APIs to use the service/library. That’s it, a high level details of how I implemented it. Following are the gists I created to share.

<SOURCE CODES>

  1. The ES service/library (picture above)[raw]
  2. The routes [raw]
  3. Postman API collection-exported [raw]

I titled each API in the “postman collection” with step number, so if you are a first timer(unlike me), I hope it will help you to understand the flow, from adding data to getting it as search result. Also the “service” and “route” are well documented as well, the steps to use are documented there too. Still I would like to mention steps here for quick look.

If you are new to Elasticsearch heres a quick description of the important terms used in elasticsearch:

An ElasticSearch cluster can contain multiple Indices (databases), which in turn contain multiple Types (tables). These types hold multiple Documents (rows), and each document has Properties (columns).

Taken from StackOverflow

Step 1 : You ping the elasticsearch to check if its running
Step 2 : Then you have to create an index( consider it as the database )
Step 3 : Check if the index is created (Same API can be used to check if an index exists before creation)
Step 4 : Once index is created you can either define the structure of data it will contain or just skip this step.
Step 5 : Now you add document to the created index
Step-6 : Use the search API with appropriate query string to get the result.

Now it is very important to learn about the search queries in elastic search. They are fairly simple and easy to understand. They have a great documentation on search query and its various options. Once you get a brief idea about it, you can easily take it forward on your own.

Please note: this is just an example of getting started with Node.js and Elasticseach. In general there shouldn’t be any REST API exposed to the outer-world which can communicate directly with your Elasticsearch.

--

--