Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | 5x 5x 5x 5x 5x 5x | const Promise = require("bluebird");
const elasticsearch = require("elasticsearch");
import { defaults, clone } from "lodash";
import { logger as defaultLogger } from "../utils/Logger";
import { AppError } from "../errors";
export class ElasticSearch {
private esClient: any; //elasticsearch.client;
private logger: any = null;
private tracer: any = null;
private config;
//https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/host-reference.html
constructor(opts?, _logger = defaultLogger, _tracer = undefined) {
let my = this;
my.logger = _logger;
my.tracer = _tracer;
my.config = defaults({}, opts, {
host: "localhost:9200", //hosts: [host:port, {host:, port: }]
//read this if u start banging head https://github.com/elastic/elasticsearch-js/issues/196
sniffOnStart: false, //sniffing additional nodes from seed host(s) above,
//!!! if true ES servers HAS TO LIST prop "http_address"
requestTimeout: 12000, //12s
keepAlive: true,
log: "warning" //error | warning | info | debug | trace
});
my.esClient = new elasticsearch.Client(my.config);
// my.esClient.ping({
// //requestTimeout: 30000,
// }, function (error) {
// if (error) {
// my.logger.error( my.host + ' cluster is down!', error);
// } else {
// my.logger.info('cluster up @' + my.host);
// }
// });
}
/**
* [index description]
* @param {Obj} opt {index:, type:, id:, body:, refresh:, pretty:,}
*/
public index(opt, cb = undefined) {
this._log(opt);
let my = this;
return new Promise((resolve, reject) => {
my.esClient.index(opt, my._reqHandler(resolve, reject));
}).nodeify(cb);
}
/**
* [delete description]
* @param {Obj} opt {}
*/
public delete(opt, cb = undefined) {
this._log(opt);
let my = this;
return new Promise((resolve, reject) => {
my.esClient.delete(opt, my._reqHandler(resolve, reject));
}).nodeify(cb);
}
/**
* [update description]
* @param {Obj} opt {index:, type:, id:, body:, size:, pretty:,}
*/
public update(opt, cb = undefined) {
this._log(opt);
let my = this;
return new Promise((resolve, reject) => {
my.esClient.update(opt, my._reqHandler(resolve, reject));
}).nodeify(cb);
}
/**
* [search description]
* @param {Obj} opt {index:, type:, id:, body:, size:, pretty:,}
*/
public search(opt, cb = undefined) {
this._log(opt);
let my = this;
return new Promise((resolve, reject) => {
my.esClient.search(opt, my._reqHandler(resolve, reject));
}).nodeify(cb);
}
/**
* [bulk description]
* @param {Obj} opt {body:,}
*/
public bulk(opt, cb = undefined) {
this._log(opt);
let my = this;
return new Promise((resolve, reject) => {
my.esClient.bulk(opt, my._reqHandler(resolve, reject));
}).nodeify(cb);
}
private _reqHandler(resolve, reject) {
let my = this;
return (err, res, status) => {
if (err) {
my.logger.debug(err.stack);
return reject(new AppError(err.message, "ESHelperError", status));
}
return resolve(res);
};
}
private _log(req) {
let my = this;
my.logger.debug(my.config.host + ": " + JSON.stringify(req));
}
}
|