ElasticSearch using Nodejs and JSON

Dipak Belsare
Jan 7 · 4 min read
Image source

Introduction

Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents.

  • NodeJS v10.21.0

Connecting to ElasticSearch using Node.js

  1. Create a new npm project
mkdir elasticsearch-node

cd elasticsearch-node

npm init --yes
npm install elasticsearch
const es = require('elasticsearch');const esClient = new es.Client({host: 'localhost:9200',log: 'trace'});module.exports = esClient;
const esClient = require('./client');esClient.ping({    // ping usually has a 3000ms timeout    requestTimeout: 1000}, function (error) {if (error) {    console.trace('elasticsearch cluster is down!');} else {    console.log('All is well');}});
const esClient = require('./client');   const createIndex = async function(indexName){      return await esClient.indices.create({      index: indexName  });}module.exports = createIndex;async function test(){try {     const resp = await createIndex('used-bikes');     console.log(resp);   } catch (e) {     console.log(e);   }}test();
const esClient = require('./client');const addmappingToIndex = async function(indexName, mapping){      console.log(mapping);      return await esClient.indices.putMapping({      index: indexName,      body: mapping    });}module.exports = addmappingToIndex;// test function to explain how to invoke.async function test(){const mapping = {   properties: {     bike_name: {     type: "text"    },    price: {       type: "integer"    },    city: {       type: "text"    },    kms_driven: {    type: "float"   },   owner: {      type: "text"   },   age: {      type: "float"   },   power: {       type: "float"   },   brand: {      type: "text"   },  }}try {     const resp = await addmappingToIndex('used-bikes', mapping);     console.log(resp);   } catch (e) {     console.log(e);  }}test();
[
{
"bike_name": "TVS Star City Plus Dual Tone 110cc",
"price": 35000,"city": "Ahmedabad","kms_driven": 17654,"owner": "First Owner","age": 3,"power": 110,"brand": "TVS"},{
"bike_name": "Royal Enfield Classic 350cc",
"price": 119900,"city": "Delhi","kms_driven": 11000,"owner": "Second Owner","age": 4,"power": 350,"brand": "Royal Enfield"},
{
"bike_name": "Triumph Daytona 675R","price": 600000,"city": "Delhi","kms_driven": 110,"owner": "Third Owner","age": 8,"power": 675,"brand": "Triumph"}
]
const esClient = require('./client');const fs = require("fs");
function test() { const fileName = "used_bikes.json"; fs.readFile(fileName, "utf8", (err, jsonString) => { if (err) { console.log("Error reading file from disk:", err); return; } console.log("Json file data:", jsonString); const body = jsonString; const bulkResponse = esClient.bulk({ refresh: true, body }); });}test();
const esClient = require('./client');const searchDoc = async function(indexName, payload){      return await esClient.search({         index: indexName,         body: payload       });   }module.exports = searchDoc;async function test(){/* DSL Query */// var query =// {//     "query" : {//         "match" : {//         }//     }// };/* DSL Query with filter */// var query =// {//     "query" : {//         "bool" : {//             "must" : {//                 "match" : {//                     "bike_name" : "TVS Star City Plus Dual Tone 110cc"//                 }//             },//             "filter" : {//                 "range" : {//                     "age" : { "gt" : 3 } //range filter: older than 3//                 }//             }//         }//     }// };/* Full-Text Search */// var query ={//     "query" : {//         "match" : {//             "owner" : "Second Owner"//         }//     }// };/* Phrase Search */var query ={"query" : {"match_phrase" : {              //match exact "First Owner""owner" : "First Owner"}}};try {  const resp = await searchDoc('used-bikes', query);
console.log(resp);
} catch (e) {
console.log(e);
}
}
test();
$ node searchUsedBikes.js

Conclusion

In this article, you learned the ElasticSearch in detail. In terms of the synchronization of the data, this technique can be used. We can also use a queue-based system to perform the synchronization.
That’s all! I hope you enjoyed reading this article and learned something new. Do share this article if you find it useful.

nonstopio

A Bespoke Engineering Studio