Skip to content

Instantly share code, notes, and snippets.

@liesislukas
Created December 9, 2021 11:34
Show Gist options
  • Save liesislukas/1fab4cee8b13310e119ad8df074d49c7 to your computer and use it in GitHub Desktop.
Save liesislukas/1fab4cee8b13310e119ad8df074d49c7 to your computer and use it in GitHub Desktop.
opensearch_sample_codes
var AWS = require('aws-sdk');
var region = 'us-east-1'; // e.g. us-west-1
var domain = 'search-boringgrowth-production-i6qoaz4c44rfxl25pkiwtvwgvm.us-east-1.es.amazonaws.com'; // e.g. search-domain.region.es.amazonaws.com
var index = 'lukas';
var type = '_doc';
var id = '1';
var json = {
"title": "Moneyball",
"director": "Bennett Miller",
"year": "2011"
}
function indexDocument(document) {
return new Promise((resolve) => {
var endpoint = new AWS.Endpoint(domain);
var request = new AWS.HttpRequest(endpoint, region);
request.method = 'PUT';
request.path += index + '/' + type + '/' + id;
request.body = JSON.stringify(document);
request.headers['host'] = domain;
request.headers['Content-Type'] = 'application/json';
request.headers['Content-Length'] = Buffer.byteLength(request.body);
var credentials = new AWS.EnvironmentCredentials('AWS');
var signer = new AWS.Signers.V4(request, 'es');
signer.addAuthorization(credentials, new Date());
var client = new AWS.HttpClient();
client.handleRequest(request, null, function(response) {
console.log(response.statusCode + ' ' + response.statusMessage);
var responseBody = '';
response.on('data', function(chunk) {
responseBody += chunk;
});
response.on('end', function(chunk) {
console.log('Response body: ' + responseBody);
resolve();
});
}, function(error) {
console.log('Error: ' + error);
resolve();
});
})
}
module.exports.handler = async() => {
await indexDocument(json);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment