Elasticsearch with Node.js – A powerful real-time search and analytics engine

Elasticsearch is a powerful and flexible open-source, distributed, real-time search and analytics engine. In this article, we will show you how to set up an Elasticsearch client in Node.js to interact with your Elasticsearch cluster.

Prerequisites

Before we begin, make sure that you have installed Node.js and npm on your system. You can check if Node.js is installed by running the following command in your terminal:

node -v

Installing the Elasticsearch Client Library

The first step is to install the Elasticsearch client library for Node.js. You can do this by running the following command in your terminal:

npm install elasticsearch

Setting up the Elasticsearch Client

Now that you have installed the Elasticsearch client library, you can start setting up the client in your Node.js application. To do this, follow these steps:

  1. Create a new JavaScript file and require the ElasticSearch library:
const elasticsearch = require('elasticsearch');

2. Create a new Elasticsearch client instance and configure the connection:

const client = new elasticsearch.Client({
  host: 'localhost:9200',
  log: 'trace'
});

In this example, the host option specifies the host and port of the Elasticsearch cluster. You can replace localhost:9200 with the host and port of your Elasticsearch cluster. The log option is optional and it can be used to log information about the requests and responses.

Basic Operations in Elasticsearch:

Here is some sample code for basic operations in Elasticsearch using the official Elasticsearch client for Node.js:

  1. Indexing a document:
const indexDocument = async (index, type, body) => {
  try {
    const result = await client.index({
      index,
      type,
      body
    })
    console.log(`Indexed document with ID ${result.body._id}`)
  } catch (err) {
    console.error(err)
  }
}

// Example usage:
indexDocument('myindex', 'mytype', {
  title: 'Elasticsearch basics',
  body: 'This is a sample document for indexing.'
})

2. Searching for documents:

const searchDocuments = async (index, type, query) => {
  try {
    const result = await client.search({
      index,
      type,
      q: query
    })
    console.log(`Found ${result.body.hits.total.value} documents`)
    result.body.hits.hits.forEach(hit => {
      console.log(hit._source)
    })
  } catch (err) {
    console.error(err)
  }
}

3. Updating a document:

const updateDocument = async (index, type, id, body) => {
  try {
    const result = await client.update({
      index,
      type,
      id,
      body: {
        doc: body
      }
    })
    console.log(`Updated document with ID ${result.body._id}`)
  } catch (err) {
    console.error(err)
  }
}

4. Deleting a document:

const deleteDocument = async (index, type, id) => {
  try {
    const result = await client.delete({
      index,
      type,
      id
    })
    console.log(`Deleted document with ID ${result.body._id}`)
  } catch (err) {
    console.error(err)
  }
}

Conclusion

In this article, you have learned how to set up an Elasticsearch client & do some basic operations in Node.js to interact with your Elasticsearch cluster. The Elasticsearch client library for Node.js provides a lot of options and methods that you can use to customize your interactions with the Elasticsearch cluster.

Thank You!

Team VectoScalar

Latest

SENTRY integration in your React Native App for Error/Crash tracking

Sentry captures data by using an SDK within your...

Recall the concepts of useCallback.

useCallback hook is one of the best hooks offered...

Value of Comments in the code!!

During my journey of Software Development, I am always...

YOLO:Bullet Paced Algorithm

http://sh017.hostgator.tempwebhost.net/media/33949d0e61af4b50f374c534713f56b3 According to world health organization, more than 1.35 million...

Featured

Developing Enterprise Application in Node.js – CJS Vs ESM

Node.js is a popular runtime environment for building server-side...

Integrating your web react applications with their React Native(android and IOS) apps using QR code

Integrating a web application with Android and iOS apps...

YOLO: Bullet Paced Algorithm – popular choice for object detection in autonomous vehicles 

According to world health organization, more than 1.35 million...

LEAVE A REPLY

Please enter your comment!
Please enter your name here