Elasticsearch client for Node.js: @elastic/elasticsearch

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Elasticsearch client for JavaScript (Node.js) — @elastic/elasticsearch



Elasticsearch is a powerful distributed search and analytics system built on top of Apache Lucene. It enables full-text search, data aggregation, and real-time log analysis. For interacting with Elasticsearch from Node.js applications, there is an official client — @elastic/elasticsearch. It provides a convenient API for all operations: from connecting to a cluster to complex search queries.



The library fully supports all versions of Elasticsearch 7.x and 8.x, works asynchronously with promises, and is ideal for building high-load search, analytics, and monitoring services. In this article, we will cover installation, basic configuration, and core data operations using Node.js.



Installation and connection



Install the client via the npm package manager:



npm install @elastic/elasticsearch


After installation, create a client instance by specifying the Elasticsearch server address and, if necessary, authentication parameters:



const { Client } = require('@elastic/elasticsearch');

const client = new Client({ node: 'http://localhost:9200', auth: { username: 'elastic', password: 'changeme' }});


For working with the Elastic Cloud service, use the cloud.id and auth.apiKey parameters.



Working with indices



An index in Elasticsearch is analogous to a database in relational DBMS. Let's look at the basic operations.



Creating an index



async function createIndex() {  const response = await client.indices.create({    index: 'products',    body: {      settings: {        number_of_shards: 1,        number_of_replicas: 1      },      mappings: {        properties: {          title: { type: 'text' },          description: { type: 'text' },          price: { type: 'float' },          created_at: { type: 'date' }        }      }    }  });  console.log('Index created:', response);}


Deleting an index



async function deleteIndex() {  await client.indices.delete({ index: 'products' });  console.log('Index deleted');}


Adding and updating documents



A document is the basic unit of data in Elasticsearch. Let's add a few records to the products index:



async function addDocuments() {  const doc1 = {    title: 'Apple MacBook Pro Laptop',    description: 'Powerful laptop for developers and designers',    price: 2499.99,    created_at: new Date()  };

const doc2 = { title: 'Logitech Mechanical Keyboard', description: 'Wireless keyboard with backlight', price: 149.99, created_at: new Date() };

// Adding with specified ID await client.index({ index: 'products', id: '1', body: doc1 });

// Adding without ID (auto-generated) await client.index({ index: 'products', body: doc2 });

// Force flush to disk await client.indices.refresh({ index: 'products' });}


For partial updates, use the update method:



await client.update({  index: 'products',  id: '1',  body: {    doc: {      price: 2399.99    }  }});


Searching data



Elasticsearch provides a powerful DSL for building queries. Let's look at a few examples.



Full-text search



async function searchProducts(query) {  const response = await client.search({    index: 'products',    body: {      query: {        match: {          description: query        }      }    }  });

console.log('Documents found:', response.hits.total.value); response.hits.hits.forEach

Recommendations