https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html
client config https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-connecting.html
const elasticsearch = require('elasticsearch');
const es_client = new elasticsearch.Client({
hosts: [
process.env.ES_HOST
]
})
module.exports = es_client;
router.post('/row', async (req, res) => {
console.log('/api/v1/threat/row');
const params = req.body;
let rt = {};
try {
const id = params._id;
const index = params._index;
const rs = await es_client.get({
index: index,
id: id
})
rt.error = false;
rt.msg = 'ok';
rt.result = rs._source;
} catch (err) {
console.error(err);
rt.error = true;
rt.msg = 'err';
rt.result = err.message;
}
res.send(rt);
})
router.post('/filter_save', async (req, res) => {
console.log('/api/v1/threat/filter_save');
const params = req.body;
console.table(params);
let rt = {};
try {
let data = {
type: 'threat_filter_test',
threat_filter_test : {
user_id:'admin',
user_nm: '관리자',
title: params.title,
desc: params.desc,
location: params.location,
mk_dt: makeDate('YYYY-MM-DD HH:mm:ss'),
upd_dt: makeDate('YYYY-MM-DD HH:mm:ss'),
data: params.filters
}
};
const rs = await es_client.index({
index:index_name_setting,
type: '_doc',
body: data
})
rt.error = false;
rt.msg = 'ok';
rt.result = rs;
} catch (err) {
rt.error = true;
rt.msg = 'err';
rt.result = err.message;
console.error(err);
}
res.send(rt);
});
router.post('/filter_update', async (req, res) => {
console.log('/api/v1/threat/filter_update');
const params = req.body;
console.table(params);
let rt = {};
try {
let data = {
"type": 'threat_filter_test',
"threat_filter_test" : {
"user_id":'admin',
"user_nm": '관리자',
"title": params.title,
"desc": params.desc,
"upd_dt": makeDate('YYYY-MM-DD HH:mm:ss'),
}
};
console.log(data);
const rs = await es_client.update({
index: index_name_setting,
type: '_doc',
id: params._id,
body: {doc : data }
})
rt.error = false;
rt.msg = 'ok';
rt.result = rs;
} catch (err) {
rt.error = true;
rt.msg = 'err';
rt.result = err.message;
console.error(err);
}
res.send(rt);
});
router.post('/filter_delete', async (req, res) => {
console.log('/api/v1/threat/filter_delete');
const params = req.body;
// console.table(params);
let rt = {};
try {
const rs = await es_client.delete({
index: index_name_setting,
type: '_doc',
id: params._id
});
rt.error = false;
rt.msg = 'ok';
rt.resutl = rs;
} catch (err) {
rt.error = true;
rt.msg = 'err';
rt.result = err.message;
console.error(err);
}
res.send(rt);
});
router.post('/threat_select', async (req, res) => {
console.log('/api/v1/threat/threat_select');
const params = req.body;
console.table(params);
let rt = {};
try {
let bulk = [];
for ( var i in params) {
const data = params[i];
const dt = data.date_time.substring(0,10).replace(/-/gi, '');
bulk.push({"update": {"_index": index_name + dt, "_type": "_doc", "_id": data._id}});
bulk.push({"doc":{"status": 1, "select_user_id": "admin", "select_user_nm": "관리자"}});
}
const rs = await es_client.bulk({
body: bulk,
refresh: 'wait_for'
})
if (rs.errors) {
rt.error = true;
rt.msg = 'bulk fail';
rt.result = JSON.stringify(rs);
} else {
rt.error = false;
rt.msg = 'ok';
rt.result = JSON.stringify(rs);
}
} catch (err) {
rt.error = true;
rt.msg = 'err';
rt.result = err.message;
console.error(err);
}
res.send(rt);
})
let rs = await ES_CLIENT.search({
index: idx_threat + data._datetime.substring(0, 4),
type: 'doc',
scroll: '3m',
body: query
})
while(rs.hits.hits.length > 0) {
rs.hits.hits.map( (doc) => {
bulk.push({update:{_index: idx_threat + doc._source._datetime.substring(0,4), _type: 'doc', _id: doc._id}});
bulk.push({doc: {TW_STATE: '3'}});
})
rs = await ES_CLIENT.scroll({ scrollId: rs._scroll_id, scroll: '3m' });
}
/**
bulk.push({"index": {"_index": index_name, "_type": "_doc"}});
bulk.push(data);
*/
function run_virus_bulk () {
let bulk = '';
const key = Object.keys(virus_name)
for (let item of key) {
bulk += `{"index":{"_index":"ts_cmn_code","_type":"doc","_id":"Virus_Total_"${item}}}\\n`;
bulk += `{"CC_PCODE":"Virus_Total","CC_CODE":"${item}","CC_NM":"${virus_name[item]}","CC_DESC":"","CC_TYPE":"ALL","IS_USE":true,"NEIS_CODE":false,"CC_GRP_LEVEL":"9","CC_FLAG":"att_type_3","CC_MK_DT":"2021-03-16 10:13:54"}\\n`;
}
console.log(bulk);
}
run_virus_bulk();
let rs = await es_client.search({
index: idx_manage,
type: '_doc',
body: query,
scroll: '1m'
})
let bulk = '';
console.log(rs);
while(rs.hits.hits.length > 0) {
rs.hits.hits.map( (doc) => {
bulk += JSON.stringify({index: {_index: doc._index, _type: doc._type, _id: doc._id}}) + '\\n';
bulk += JSON.stringify({doc: doc._source}) + '\\n';
})
rs = await es_client.scroll({ scrollId: rs._scroll_id, scroll: '1m'});
}