Get Started with Elastic Search NodeJs Client

Anurag Patel
Geek Farmer
Published in
4 min readDec 25, 2022

ES: Elastic Search

Prerequisites

In our previous articles on Elastic Search, we learned about the basics of Elastic Search, how it works, and how to create a new index. In this article, we will continue working with the Elastic Search NodeJS client and explore how to index new documents, update existing indexed documents, and delete indexed documents. By understanding these features, we can more effectively use Elastic Search to manage and update the data in our projects. Let’s get started and see how these functions work in action.

All below example are based on ES Node client.

Installation

You can use npm Elastic Search package in your nodejs application.

npm install @elastic/elasticsearch

Connecting

Our ES cluster is configured with security explicitly disabled that’s why we can connect via HTTP.

import { Client, errors } from '@elastic/elasticsearch';

const client = new Client({
node: 'http://example.com'
});

While connecting to ES http client, we can pass different type of configuration.

All other ES related references you can get here.

Ping

To check ES client is connected with defined http cluster, perform ping action.

client
.ping({
// ping usually has a 3000ms timeout
requestTimeout: 1000,
})
.then(() => {
this.logger.info('Elasticsearch connected');
})
.catch((err: errors.ConnectionError) => {
this.logger.error('Elasticsearch unavailable', { error: err });
});

Create ES Index

async createIndex(indexName: string) {
return this.client.indices.create({
index: indexName,
mappings: {},
settings: {}
});
};

Check Index Exists

Check if index exists or not.

async indexExists(indexName: string) {
return this.client.indices.exists({
index: indexName,
});
};

Add Settings

Before adding any document to index, it’s recommended to update index settings and add required analyzers based on your search functionality. Here is an example of index setting:

async indexSetting(indexName: string) {
return this.client.indices.putSettings({
index: indexName,
body: {
settings: {
max_ngram_diff: 19,
analysis: {
filter: {
autocomplete_filter: {
type: 'ngram',
min_gram: '1',
max_gram: '20',
},
},
analyzer: {
autocomplete: {
filter: ['lowercase', 'autocomplete_filter'],
type: 'custom',
tokenizer: 'standard',
},
},
},
number_of_replicas: '1',
},
}
});
};

In ES there are different type of built in analyzers are available but we can define our custom analyzer also.

Add Mappings

Before adding any document to index, it’s recommended to update index settings then update index mappings and assign analyzer properties with fields. Here is an example of index mappings:

As while updating settings, we added autocomplete analyzer which we can define in mappings as search property. Index mapping can be different for different indices.

const searchProperty = {
type: 'text',
analyzer: 'autocomplete',
search_analyzer: 'standard',
};

async indexMapping(indexName: string) {
return this.client.indices.putMapping({
index: indexName,
body: {
properties: {
id: { type: 'keyword' },
createdAt: { type: 'date' },
updatedAt: { type: 'date' },
caseCode: searchProperty,
policyNo: searchProperty,
claimRegNo: searchProperty,
vehicleRegNo: searchProperty,
vehicleChassisNo: searchProperty,
claimStatus: { type: 'keyword' },
lossType: { type: 'keyword' },
vehicleType: { type: 'keyword' },
zone: { type: 'keyword' },
insurer: { type: 'keyword' },
caseStatus: { type: 'keyword' },
business: { type: 'keyword' },
},
},
});
};

try to avoid to use ES dynamic mapping, always update your mapping with suitable data type. In future if we have to add any new field, first update mapping for that field, then add documents with new fields.

Add document

There are different ways of adding documents in an index:

Single Document

async addDocument(indexName: string, payload: DataType) {
return this.client.index({
index: indexName,
type: '_doc',
id: payload.id,
body: payload
});
};

Multi Documents

async addBulkDocuments(indexName: data = []) {
const payload = data.map((item) => {
return [
{
index: {
_index: indexName,
_type: '_doc',
_id: item.id,
},
},
item,
]
})
return this.client.bulk({
refresh: true,
body: payload
});
};

Update document

async updateDocument(indexName: string, payload: DataType) {
return this.client.update({
index: indexName,
type: '_doc',
id: payload.id,
body: payload
});
};

We can update single document as well as multi documents using ES query.

Update Single Document: Updates a document with a script or partial document.

client.update(...)

Update Documents by Query: Performs an update on every document in the index without changing the source, for example to pick up a mapping change.

client.updateByQuery(...)

Delete document

Delete a single document

async deleteDocument(indexName: string, id: string) {
return this.client.delete({
index: indexName,
id,
});
};

like create and update we can delete a single document as well as multi documents using ES query.

Delete Single Document: Updates a document with a script or partial document.

client.delete(...)

Delete Documents by Query: Performs an update on every document in the index without changing the source, for example to pick up a mapping change.

client.deleteByQuery(...)

References

Elasticsearch JavaScript Client [8.5] | Elastic

API Reference | Elasticsearch JavaScript Client [8.5] | Elastic

Examples | Elasticsearch JavaScript Client [8.5] | Elastic

Create a custom analyzer | Elasticsearch Guide [8.5] | Elastic

Built-in analyzer reference | Elasticsearch Guide [8.5] | Elastic

Field data types | Elasticsearch Guide [8.5] | Elastic

I hope you enjoyed reading about Elastic Search and how it can be used to optimize search and analysis in various applications. If you found this article helpful or have any further questions, please don’t hesitate to reach out to me through the comments.

For more updates and insights on the latest tech trends, be sure to follow me on Twitter or LinkedIn. Thanks for reading, and I look forward to connecting with you on social media.

Twitter: https://twitter.com/geekfarmer_

Linkedin: https://www.linkedin.com/in/geekfarmer

--

--