Elasticsearch实战 统计查询结果个数

整体查询条件

{
    "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;
            }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值