UNPKG

@gdksoftware/elasticsearch

Version:
140 lines 4.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const AWS = require("aws-sdk"); const ElasticSearch = require("elasticsearch"); const HttpAWSEs = require("http-aws-es"); class ElasticSearchClientFactory { static create(config) { let options = { hosts: config.hosts, connectionClass: HttpAWSEs, awsConfig: new AWS.Config({ credentials: new AWS.Credentials(config.accessKeyId, config.secretAccessKey), region: config.region }), httpOptions: {} }; return ElasticSearch.Client(options); } } exports.default = ElasticSearchClientFactory; class ElasticSearchBulkIdentifier { constructor(index, id) { this._index = index; this._id = id; this._type = 'doc'; } } exports.ElasticSearchBulkIdentifier = ElasticSearchBulkIdentifier; class ElasticSearchBulkInsertRecord { constructor(index, id, content) { this.index = new ElasticSearchBulkIdentifier(index, id); this.document = content; } } exports.ElasticSearchBulkInsertRecord = ElasticSearchBulkInsertRecord; class ElasticSearchBulkUpdateRecord { constructor(index, id, content) { this.index = new ElasticSearchBulkIdentifier(index, id); this.document = content; } } exports.ElasticSearchBulkUpdateRecord = ElasticSearchBulkUpdateRecord; class ElasticSearchBulk { constructor(index, config) { this.items = []; this.type = 'doc'; this.index = index; this.config = config; } insert(id, content) { let record = new ElasticSearchBulkInsertRecord(this.index, id, content); record.index._type = this.type; this.items.push({ index: record.index }, content); return record; } update(id, content) { let record = new ElasticSearchBulkUpdateRecord(this.index, id, content); record.index._type = this.type; let document = {}; document[this.type] = content; this.items.push({ update: record.index }, document); return record; } execute() { return new Promise((resolve, reject) => { let client = ElasticSearchClientFactory.create(this.config); client.bulk({ maxRetries: 5, index: this.index, type: this.type, body: this.items }, (err, resp) => { if (err) { return reject(err); } resolve(resp); }); }); } } exports.ElasticSearchBulk = ElasticSearchBulk; class ElasticSearchConfiguration { } exports.ElasticSearchConfiguration = ElasticSearchConfiguration; class ElasticSearchSeek { constructor(config) { this.config = config; this.options = new ElasticSearchOptions(); } start() { return this; } index(value) { this.options.index = value; return this; } type(value) { this.options.type = value; return this; } body(value) { this.options.body = value; return this; } size(value) { this.options.size = value; return this; } search(options) { let request = options || this.options; return new Promise((resolve, reject) => { let client = ElasticSearchClientFactory.create(this.config); client.search(request, (err, results) => { if (err) { return reject(err); } resolve(results); }); }); } } exports.ElasticSearchSeek = ElasticSearchSeek; class ElasticSearchOptions { } exports.ElasticSearchOptions = ElasticSearchOptions; class SimpleMatchQuery { constructor(field, value, size = 5) { this.size = size; this.query = { bool: { must: [ { match: {} } ] } }; this.query.bool.must[0].match[`${field}`] = value; } } exports.SimpleMatchQuery = SimpleMatchQuery; //# sourceMappingURL=elasticsearch.js.map