Skip to content

Commit

Permalink
Change New Elasticsearch Node.js Client
Browse files Browse the repository at this point in the history
Change Library
  elasticsearch 16.7.1 → @elastic/elasticsearch 5.6.22 / 6.8.8 / 7.9.1
Update Library
  eslint 7.8.1 → 7.10.0
  husky  4.2.5 → 4.3.0
Add Library
  axios 0.20.0
  • Loading branch information
nsano-rururu committed Oct 6, 2020
1 parent a4ad1d7 commit 2a8dd26
Show file tree
Hide file tree
Showing 8 changed files with 263 additions and 138 deletions.
10 changes: 8 additions & 2 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"presets": [
"@babel/preset-env"
[
"@babel/preset-env", {
"targets": {
"node": "current"
}
}
]
]
}
}
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module"
"sourceType": "module",
"ecmaVersion": 2021
},
"rules": {
"indent": [
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@
"@babel/preset-env": "^7.11.5",
"@babel/register": "^7.11.5",
"@hapi/joi": "^17.1.1",
"axios": "^0.20.0",
"body-parser": "^1.19.0",
"bunyan": "^1.8.14",
"cors": "^2.8.5",
"cpu-stat": "^2.0.1",
"elasticsearch": "^16.7.1",
"es5": "npm:@elastic/elasticsearch@^5.6.22",
"es6": "npm:@elastic/elasticsearch@^6.8.8",
"es7": "npm:@elastic/elasticsearch@^7.9.1",
"express": "^4.17.1",
"fs-extra": "^9.0.1",
"js-yaml": "^3.14.0",
Expand All @@ -44,14 +48,14 @@
"ws": "^7.3.1"
},
"devDependencies": {
"eslint": "^7.8.1",
"husky": "^4.2.5",
"eslint": "^7.10.0",
"husky": "^4.3.0",
"mocha": "~8.1.3"
},
"scripts": {
"build": "babel src -d lib",
"start": "sh ./scripts/start.sh",
"update-authors": "./scripts/update-authors.sh",
"precommit": "./node_modules/eslint/bin/eslint.js ."
"precommit": "./node_modules/eslint/bin/eslint.js --fix ."
}
}
184 changes: 125 additions & 59 deletions src/common/elasticsearch_client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import elasticsearch from 'elasticsearch';
import elasticsearch5 from 'es5';
import elasticsearch6 from 'es6';
import elasticsearch7 from 'es7';
//TODO: Elasticsearch 8.x
//import elasticsearch8 from 'es8';

import fs from 'fs';
import config from './config';
import axios from 'axios';

export function escapeLuceneSyntax(str) {
return [].map
Expand Down Expand Up @@ -33,77 +39,137 @@ export function escapeLuceneSyntax(str) {
.join('');
}

export function getClientVersion(response) {
let client = getClient();
export async function getClientVersion() {

try {
let scheme = 'http';

if (config.get('es_ssl')) {
scheme = 'https';
}

let auth = '';

if (config.get('es_username') && config.get('es_password')) {
auth = `${config.get('es_username')}:${config.get('es_password')}@`;
}

const result = await axios.get(`${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`);
return parseInt(result.data.version['number'].split('.')[0], 10);
} catch (error) {
console.log(error);
}

return client.info().then(function (resp) {
return parseInt(resp.version.number.split('.')[0], 10);
}, function (err) {
response.send({
error: err
});
});
}

export function clientSearch(index, type, qs, request, response) {
let client = getClient();

client.search({
index,
type,
body: {
from: request.query.from || 0,
size: request.query.size || 100,
query: {
bool: {
must: [
{
query_string: { query: qs }
}
]
}
},
sort: [{ '@timestamp': { order: 'desc' } }]
export async function clientSearch(index, type, qs, request, response) {

try {
const es_version = await getClientVersion();
const client = await getClient();

if (es_version >= 7) {
type = undefined;
}
}).then(function (resp) {
resp.hits.hits = resp.hits.hits.map(h => h._source);
response.send(resp.hits);
}, function (err) {
response.send({
error: err

client.search({
index: index,
type: type,
body: {
from: request.query.from || 0,
size: request.query.size || 100,
query: {
bool: {
must: [
{
query_string: { query: qs }
}
]
}
},
sort: [{ '@timestamp': { order: 'desc' } }]
}
}, (err, {body}) => {
if (err) {
response.send({
error: err
});
} else {
body.hits.hits = body.hits.hits.map(h => h._source);
response.send(body.hits);
}
});
});
} catch (error) {
console.log(error);
}

}

export function getClient() {
let scheme = 'http';
let ssl_body = {};
export async function getClient() {

if (config.get('es_ssl')) {
scheme = 'https';
ssl_body.rejectUnauthorized = false;
try {
const es_version = await getClientVersion();

if (config.get('es_ca_certs')) {
ssl_body.ca = fs.readFileSync(config.get('es_ca_certs'));
}
if (config.get('es_client_cert')) {
ssl_body.cert = fs.readFileSync(config.get('es_client_cert'));
let scheme = 'http';
let ssl_body = {};

if (config.get('es_ssl')) {
scheme = 'https';
ssl_body.rejectUnauthorized = false;

if (config.get('es_ca_certs')) {
ssl_body.ca = fs.readFileSync(config.get('es_ca_certs'));
}
if (config.get('es_client_cert')) {
ssl_body.cert = fs.readFileSync(config.get('es_client_cert'));
}
if (config.get('es_client_key')) {
ssl_body.key = fs.readFileSync(config.get('es_client_key'));
}
}
if (config.get('es_client_key')) {
ssl_body.key = fs.readFileSync(config.get('es_client_key'));

let auth = '';

if (config.get('es_username') && config.get('es_password')) {
auth = `${config.get('es_username')}:${config.get('es_password')}@`;
}
}

let auth = '';
if (es_version === 5) {

if (config.get('es_username') && config.get('es_password')) {
auth = `${config.get('es_username')}:${config.get('es_password')}@`;
// Elasticsearch 5.x
const client5 = new elasticsearch5.Client({
node: [ `${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`],
ssl: ssl_body
});
return client5;
} else if (es_version == 6) {

// Elasticsearch 6.x
const client6 = new elasticsearch6.Client({
node: [ `${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`],
ssl: ssl_body
});
return client6;
} else if (es_version == 7) {

// Elasticsearch 7.x
const client7 = new elasticsearch7.Client({
node: [ `${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`],
ssl: ssl_body
});
return client7;
} else if (es_version == 8) {

//TODO: Elasticsearch 8.x
//const client8 = new elasticsearch8.Client({
// node: [ `${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`],
// ssl: ssl_body
//});
//return client8;
}
} catch (error) {
console.log(error);
}

var client = new elasticsearch.Client({
hosts: [ `${scheme}://${auth}${config.get('es_host')}:${config.get('es_port')}`],
ssl: ssl_body
});

return client;
}

28 changes: 17 additions & 11 deletions src/handlers/indices/get.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { getClient } from '../../common/elasticsearch_client';

export default function indicesHandler(request, response) {
export default async function indicesHandler(request, response) {
/**
* @type {ElastalertServer}
*/

var client = getClient();
try {
const client = await getClient();

client.cat.indices({
h: ['index']
}).then(function(resp) {
let indices = resp.trim().split('\n');
response.send(indices);
}, function(err) {
response.send({
error: err
client.cat.indices({
h: ['index']
}, (err, {body}) => {
if (err) {
response.send({
error: err
});
} else {
let indices = body.trim().split('\n');
response.send(indices);
}
});
});
} catch (error) {
console.log(error);
}

}
28 changes: 17 additions & 11 deletions src/handlers/mapping/get.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import { getClient } from '../../common/elasticsearch_client';

export default function mappingHandler(request, response) {
export default async function mappingHandler(request, response) {
/**
* @type {ElastalertServer}
*/

var client = getClient();

client.indices.getMapping({
index: request.params.index
}).then(function(resp) {
response.send(resp);
}, function(err) {
response.send({
error: err
try {
const client = await getClient();

client.indices.getMapping({
index: request.params.index
}, (err, {body}) => {
if (err) {
response.send({
error: err
});
} else {
response.send(body);
}
});
});
} catch (error) {
console.log(error);
}

}
Loading

0 comments on commit 2a8dd26

Please sign in to comment.