node操作es的查询语句
程序员文章站
2022-07-06 09:42:56
...
exports.getIPSList=function (req, res, next){
//定义变量
var objELkRequestBody= new ELk_requestbody.ELK_RequestBody();
//接受并响应
objELkRequestBody.RequestParamAnalysis(req,res,next);
//设置查询的库和表
objELkRequestBody.SetESIndex("ssa_envents*").SetESType("nsec");
//设置精准查询
objELkRequestBody.SetQueryKey( //设置精确查询
{
"program":"IPS"
}
);
//精准查询
if(req.query.search){
objELkRequestBody.SetQueryKey(req.query.search)
}
//设置排序
if(req.query.sort)
objELkRequestBody.SetSortField(req.query.sort); //设置排序
objELkRequestBody.SetSortField({"@timestamp":"desc"}); // 默认按照时间戳排序
//设置分页查询
objELkRequestBody.SetPagingCfg(req.query.page,req.query.size); //设置分页
//设置时间范围
objELkRequestBody.SetTimeRange( req.query.begTime,req.query.endTime); //设置时间范围
//模糊查询 查询体
if(req.query.search){
objELkRequestBody.SetQueryString(req.query.search); //设置模糊查询
}
//查询完成以后进行封装发送给前台
//发送请求获得响应体并封装
objELkRequestBody.SendELKRequest(req,res, function (err, resp){
//如果有错误的话就打印错误
if (err) { console.trace(err.message);}
//否则就输出
else {
//console.log(JSON.stringify(result));
if (resp.hits.hits.length <= 0){
var logUtil = require('../../../tools/LogUtil');
return logUtil.getLatestLog(req, res, objELkRequestBody);
}
var res_data = config.res_data(0);
var hits = resp.hits.hits;
res_data.data.total = resp.hits.total;
var data = [];
for(var i in hits){
data.push(hits[i])
}
res_data.data.rows = data;
config.cr_res_json( req, res,res_data);
}
});
};
里边涉及到模糊查询与精准查询