Introduction to http-aws-es
The http-aws-es
library is a Node.js HTTP connector for Amazon Elasticsearch Service (Amazon ES). It provides an easy and efficient way to connect, interact, and manage your Elasticsearch clusters hosted on AWS. It offers various APIs to carry out common Elasticsearch operations securely using AWS Signature Version 4 signing process.
Setup and Configuration
const AWS = require('aws-sdk'); const elasticsearch = require('elasticsearch'); const HttpAwsEs = require('http-aws-es'); AWS.config.update({ region: 'us-west-2', credentials: new AWS.Credentials({ accessKeyId: 'YOUR_AWS_ACCESS_KEY_ID', secretAccessKey: 'YOUR_AWS_SECRET_ACCESS_KEY' }) }); const client = new elasticsearch.Client({ hosts: 'https://your-elasticsearch-domain.us-west-2.es.amazonaws.com', connectionClass: HttpAwsEs, });
API Examples
Index a Document
client.index({ index: 'my-index', type: '_doc', id: '1', body: { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald', year: 1925 } }, (err, resp) => { if (err) console.log(err.message); else console.log(resp); });
Search for Documents
client.search({ index: 'my-index', body: { query: { match: { title: 'gatsby' } } } }, (err, resp) => { if (err) console.log(err.message); else console.log(resp.hits.hits); });
Update a Document
client.update({ index: 'my-index', type: '_doc', id: '1', body: { doc: { year: 1926 } } }, (err, resp) => { if (err) console.log(err.message); else console.log(resp); });
Delete a Document
client.delete({ index: 'my-index', type: '_doc', id: '1' }, (err, resp) => { if (err) console.log(err.message); else console.log(resp); });
Creating Indices
client.indices.create({ index: 'my-new-index', }, (err, resp) => { if (err) console.log(err.message); else console.log(resp); });
Deleting Indices
client.indices.delete({ index: 'my-new-index', }, (err, resp) => { if (err) console.log(err.message); else console.log(resp); });
Sample Application using http-aws-es
Below is an example of a simple Node.js application that uses various APIs provided by http-aws-es
to interact with Elasticsearch.
const express = require('express'); const app = express(); const bodyParser = require('body-parser'); const AWS = require('aws-sdk'); const elasticsearch = require('elasticsearch'); const HttpAwsEs = require('http-aws-es'); AWS.config.update({ region: 'us-west-2', credentials: new AWS.Credentials({ accessKeyId: 'YOUR_AWS_ACCESS_KEY_ID', secretAccessKey: 'YOUR_AWS_SECRET_ACCESS_KEY' }) }); const client = new elasticsearch.Client({ hosts: 'https://your-elasticsearch-domain.us-west-2.es.amazonaws.com', connectionClass: HttpAwsEs, }); app.use(bodyParser.json()); app.post('/index', (req, res) => { client.index({ index: 'my-index', type: '_doc', body: req.body }, (err, resp) => { if (err) res.status(500).send(err.message); else res.status(200).send(resp); }); }); app.get('/search', (req, res) => { client.search({ index: 'my-index', body: { query: { match: req.query } } }, (err, resp) => { if (err) res.status(500).send(err.message); else res.status(200).send(resp.hits.hits); }); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
In this example, we have created two endpoints: one for indexing documents and one for searching documents within the index. This simple application showcases how easy it is to integrate http-aws-es
with your Node.js applications to interact with Amazon Elasticsearch Service.
Using these APIs and concepts, you can build more complex solutions tailored to your business needs, leverage Elasticsearch’s full-text search capabilities, and maintain high data retrieval performance with secure access.
Hash: 2585bd212594142779f84f7a8dcaef427fa421c317ced18af871811dd4023add