疲劳是最舒适的枕头,努力工作吧。

急! 实现动态多条件下日志查询 自带discover不够详细

Kibana | 作者 yuxia6895 | 发布于2018年10月30日 | 阅读数:2442

想实现动态多条件下日志查询的功能  
我之前只写过固定匹配的字段,
比如之前查询某用户在某时间段的日志,这样data就只要传三个固定字段,user ,startDate,endDate
现在情况是查询条件变多,比如用户名,系统名,时间段,日志类型。 且不是每个条件都是必须填写,
所以我在server.route里写request.payload.start  request.payload.app等等把位置提前占好也没用的。
server.route({
path: "/log-monitor/backdate",
method: "POST",
config: {
payload: {
output: "data",
parse: true,
allow: "application/json"
}
},
handler(request, reply) {
console.log(request.payload);
const { callWithRequest } = server.plugins.elasticsearch.getCluster(
"data"
);
callWithRequest(request, "search", {
index: " logstash-*",
body: {
query: {
bool: {
must:[
{match: { user: request.payload.user }},
{match: { app: request.payload.app }}
],
filter: {
range: {
"@timestamp": {
gte: request.payload.start,
lte: request.payload.end
}
}
}
}
},
from: request.payload.currentPage,
size:25,
sort: [{ "@timestamp": "desc" }]
}
}).then(response => {
reply(response);
});
}
});
var data = {
user: $scope.userName,
start: startDate($scope.dateRangeStart),
end: endDate($scope.dateRangeEnd),
app:$scope.app,
level:$scope.level,
};
console.log(data)
$http.post(url, data).success(function(req) {
console.log(req)
})
已邀请:

rochy - rochy_he

赞同来自:

关键在于下面,你不是必需的字段最好是写为 should 而不是 must;此外你可以通过判断值是否为空来选择时候加入这个查询条件
bool: {  
must:[
{match: { user: request.payload.user }},
{match: { app: request.payload.app }}
],
filter: {
range: {
"@timestamp": {
gte: request.payload.start,
lte: request.payload.end
}
}
}
}

要回复问题请先登录注册