Skip to content

Instantly share code, notes, and snippets.

@schmod
Last active December 2, 2020 06:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmod/db1a7f19a0ef96592a7f0c808e11e9dc to your computer and use it in GitHub Desktop.
Save schmod/db1a7f19a0ef96592a7f0c808e11e9dc to your computer and use it in GitHub Desktop.
elasticsearch.js, Bluebird, and AWS Elasticsearch with TypeScript

A quick example demonstrating how to use elasticsearch.js, Bluebird, and AWS Elasticsearch with TypeScript.

You'll need to install a few dependencies and types:

npm install --save @types/elasticsearch @types/http-aws-es elasticsearch http-aws-es bluebird

If you're using AWS Elasticsearch, provide an ES_TYPE environment variable set to aws, as well as values for AWS_ACCESS_KEY, AWS_SECRET_KEY, and AWS_REGION. (I make this configurable so it's easy to point applications at a local ES instance when developing/testing locally, and at AWS when deployed to production)

import { Client, ConfigOptions } from 'elasticsearch';
import * as HttpAmazonESConnector from 'http-aws-es';
import * as Bluebird from 'bluebird';
// Let TypeScript know that elasticsearch will return Bluebird promises
declare module 'elasticsearch' {
type Promise<T> = Bluebird<T>;
}
const config = <ConfigOptions> {
apiVersion: '5.5',
host: process.env.ES_HOST,
defer: function() {
return Bluebird.defer();
}
};
const type = process.env.ES_TYPE;
if (type === 'aws') {
config.connectionClass = HttpAmazonESConnector;
config.amazonES = {
accessKey: <string> process.env.AWS_ACCESS_KEY,
secretKey: <string> process.env.AWS_SECRET_KEY,
region: <string> process.env.AWS_REGION
};
}
export const client = new Client(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment