yandex-tank-ammo-generator
Version:
Cartridge generation for Yandex tank
98 lines (97 loc) • 3.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const elasticsearch_1 = require("@elastic/elasticsearch");
const logger_1 = require("../../packages/infrastructure/logger");
const log = new logger_1.Log().setLocation('ELASTIC CLIENT');
class ElasticClient {
constructor() {
//
this._index = (process.env.SEARCH_INDEX || 'catalog').toString();
if (!process.env.hasOwnProperty('ELASTIC_URI') || !process.env.hasOwnProperty('ELASTIC_AUTH')) {
throw new Error('Не заданы переменные окружения для подключения к ELASTIC');
}
const auth = (process.env.ELASTIC_AUTH || 'username:Password').split(':');
if (auth[0] !== 'username') {
this._client = new elasticsearch_1.Client({
nodes: process.env.ELASTIC_URI.toString().split(','),
auth: {
password: auth[1],
username: auth[0],
},
compression: 'gzip',
});
}
else {
this._client = new elasticsearch_1.Client({
nodes: process.env.ELASTIC_URI.toString().split(','),
compression: 'gzip',
});
}
}
get client() {
return this._client;
}
async index(params) {
return this._client.index(params);
}
async putSettings(params) {
return this._client.indices.put_settings(params);
}
async catIndices(params) {
return this._client.cat.indices(params);
}
async ping() {
return this._client.ping();
}
async bulk(params) {
return this._client.bulk(params);
}
async search(searchParams, requestParams) {
return this._client.search(this.prepareRawQuery(searchParams, requestParams));
}
async aggregate(searchParams, requestParams) {
return this._client.search(this.prepareRawQuery(searchParams, requestParams));
}
async searchAndAggregate(searchParams, requestParams) {
return this._client.search(this.prepareRawQuery(searchParams, requestParams));
}
async count(searchParams) {
return this._client.count({
index: this._index,
body: searchParams,
});
}
async executeBulk(bulk) {
return this._client.bulk({
body: bulk,
error_trace: true,
pretty: true,
});
}
async maping(mapping) {
return this._client.indices.put_mapping({
index: this._index,
body: {
properties: mapping,
},
});
}
prepareBulkErrors(bulkResponse, bulk) {
const erroredDocuments = [];
bulkResponse.body.items.forEach((action, i) => {
const operation = Object.keys(action)[0];
if (action[operation].error) {
erroredDocuments.push({
status: action[operation].status,
error: action[operation].error,
document: bulk[i * 2 + 1],
});
}
});
return erroredDocuments;
}
prepareRawQuery(query, requestParams) {
return Object.assign(Object.assign({ index: this._index }, requestParams), { body: query });
}
}
exports.ElasticClient = ElasticClient;