How To Query Elasticsearch Using JavaScript

JavaScript

02/07/2019


Elasticsearch is an amazing open-source tool to store, search and analyse data. With an emphasis on search. Fortunately, this query goodness isn't restricted to Kibana alone and its dev tools section. Elastic, the company behind this search engine, has also provided us with a feature packed NodeJS API client, allowing you to query directly from any backend application. There are also several official Elasticsearch clients as well as community contributed clients for other programming languages. However, in this article, I'll be showing you specifically how to query Elasticsearch using JavaScript! So let's get started, shall we?! 😆

Let's talk about Kibana

When it comes to using Elasticsearch, Kibana is a natural choice as an analytics and visualisation tool. It's like the Robin to Batman. 🦇 Moreover, should you ever find it lacking certain features you need, you can always fill that gap by creating your own plugin. I also have a step-by-step tutorial on how to create a Kibana plugin, in case you're interested! Each plugin automatically comes equipped with NodeJS on the backend, with HapiJS as the framework.

Kibana plugins are probably the most common place for needing the API client to query Elasticsearch. However, what you're about to read can be done anywhere, even in a simple script file! 😋

Installation

To install the JavaScript API client, simply run one of the commands below. Keep note that the library requires you to have at least v8 of NodeJS running. Moreover, it's best that you install the same major client version as Elasticsearch. For instance, if you're working with Elasticsearch 7, you should install version 7 of the NodeJS API client.

BASH
npm install @elastic/elasticsearch@7
yarn add @elastic/elasticsearch@7

Next, you want to import the library into a file of your choice and instantiate a new client with which you can start querying Elasticsearch. Make sure it is observing the right port number upon instantiating! 👏

JAVASCRIPT
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' })

Configuration

Before moving on, the default configuration should suffice for most people. However, if you do need to configure your client, you can get further details on that right here!

Querying Elasticsearch

To search indices in Elasticsearch, you will be using the search function, which allows you to use the Elasticsearch Query DSL.

JAVASCRIPT
client.search({
index: 'kibana_sample_data_ecommerce',
body: {
"query": {
"match_all": {}
},
"size": 0
}
})
.then(response => console.log(response))
.catch(error => console.log(error));

The search function receives at minimum an object with two properties: index and body. The former shows which index you would like to query, while the latter contains the instructions of your query. Afterwards, the function returns a promise for the results. Finally, the body property of the returned object contains the results of your query.

JAVASCRIPT
{ body: object | boolean,
statusCode: number,
headers: object,
warnings: [string],
meta: object }

Endless possibilites

Querying Elasticsearch might only represent a fraction of what you might need to do. The client has an overwhelming 😱 amount of other APIs you might want to consider, such as for creating new indices, adding and removing documents, etc.


WRITTEN BY

Code and stuff