整体查询条件
{
"query": {
"bool": {
"must": [
{
"range": {
"attack_time": {
"from": "2023-02-07 16:00:00",
"to": "2023-02-07 16:00:03",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
},
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"bool": {
"should": [
{
"wildcard": {
"alarm_level": {
"wildcard": "*高*",
"boost": 1
}
}
},
{
"wildcard": {
"alarm_level": {
"wildcard": "*低*",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"terms": {
"log_format_type": [
"3"
],
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"bool": {
"must": [
{
"range": {
"alarm_level_id": {
"from": "2",
"to": null,
"include_lower": false,
"include_upper": true,
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"terms": {
"alarm_level_id": [
3
],
"boost": 1
}
},
{
"bool": {
"must_not": [
{
"script": {
"script": {
"source": "def seconds = doc['attack_time'].value.getHourOfDay()*3600+doc['attack_time'].value.getMinuteOfDay()*60+doc['attack_time'].value.getSecondOfDay(); return (doc['attack_time'].value.dayOfWeek)==params.week_day && seconds>params.begin_seconds && seconds<=params.end_seconds",
"lang": "painless",
"params": {
"end_seconds": 17999,
"week_day": 2,
"begin_seconds": 0
}
},
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
}
核心功能Java代码
查询并进行结果解析
Integer time = configItem.getInt("time");
Date dateFromTime = DateUtil.offsetSecond(dateAttackTime, -time);
String fromTime = DateUtil.formatDateTime(dateFromTime);
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("attack_time");
rangeQueryBuilder.from(fromTime).to(attackTime);
boolQueryBuilder.must(rangeQueryBuilder);
CountRequest countRequest = new CountRequest();
countRequest.query(boolQueryBuilder).indices(hitIndex);
CountResponse countResponse = null;
try {
countResponse = client.count(countRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new RuntimeException(e);
}
long count = countResponse.getCount();
if (count > configItem.getInt("minCount")
&& count < configItem.getInt("maxCount")) {
return true;
}