最近做微服务监控系统,用到es,自己做定制的查询api,发现需要dls语言,
下面给个示例给xdm做个参考.
首先根据给定字段过滤空的空字段记录,然后根据组合字段过滤掉重复的记录,最后选择需要显示的字段
使用时要去掉注释
GET monitorapi-2022.02/_search
{"query": {//先过滤有空的记录
"bool": {
"must": [
{
"script": {
"script": {
"lang": "painless",
"source": "doc['modelName.keyword'].size()*doc['clazzName.keyword'].size()*doc['methodName.keyword'].size()*doc['hostname.keyword'].size()>0"
}
}
}
]
}
},
"aggs": {
"my":{
"terms":{//组合字段,过滤重复的记录
"script": "doc['modelName.keyword'].value+'#'+doc['clazzName.keyword'].value+'#'+doc['methodName.keyword'].value+'#'+doc['hostname.keyword'].value"
},
"aggs":{//展示需要的字段
"my_top_hits":{
"top_hits":{
"_source": {
"includes": [
"modelName",
"hostname",
"clazzName",
"clazzInfo",
"methodName",
"methodInfo"
]
},
"size":1
}
}
}
}
}
}
java 代码实现
String[] indexNames = "monitorapi-*".split(",");
SearchRequest request = new SearchRequest(indexNames);
SearchSourceBuilder builder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
ScriptQueryBuilder scriptQueryBuilder = new ScriptQueryBuilder(Script.parse("doc['modelName.keyword'].size()*doc['clazzName.keyword'].size()*doc['methodName.keyword'].size()*doc['hostname.keyword'].size()>0"));
boolQueryBuilder.must(scriptQueryBuilder);
builder.query(boolQueryBuilder);
TermsAggregationBuilder termsSourceTypeSourcePlaceGroup = AggregationBuilders.terms("my")
.script(Script.parse("doc['modelName.keyword'].value+'#'+doc['clazzName.keyword'].value+'#'+doc['methodName.keyword'].value+'#'+doc['hostname.keyword'].value"));
String[] arr = {"modelName", "hostname", "clazzName", "clazzInfo", "methodName", "methodInfo"};
String[] strings = new String[6];
TopHitsAggregationBuilder topHitsAggregationBuilder = AggregationBuilders.topHits("myTopHits")
.fetchSource(arr,null).size(1);
TermsAggregationBuilder aggregationBuilder = termsSourceTypeSourcePlaceGroup.subAggregation(topHitsAggregationBuilder);
builder.aggregation(aggregationBuilder);
request.source(builder);
转化的时候api名称和dls关键字是对应关系,可以先写个测试,查看是否能还原成dls来判断是否正确,当然直接看结果更加可靠.
es版本和配置
(一)log4j漏洞es升级
(二)log4j漏洞es升级