es时间段查询
下面展示一些 内联代码片。es时间段查询记录一下学习es的艰辛之旅刚接触es 的时候,就来个需求dsl都不会写,直接硬上,结果就是第一段这种情况,先查范围再分割,蠢方法还觉得写的分割算法很厉害,结果过了半年再看直接es自带方法,虽然是es5,没那么多花里胡哨的方法,但是够用上,321上链接(时间段的起止可以用hutool工具包,更加方便一点,虽然但是能用就行)/*** 24小时HTTP请求方式**
·
下面展示一些 内联代码片
。
es时间段查询
记录一下学习es的艰辛之旅
刚接触es 的时候,就来个需求dsl都不会写,直接硬上,结果就是第一段这种情况,先查范围再分割,蠢方法还觉得写的分割算法很厉害,结果过了半年再看直接es自带方法,虽然是es5,没那么多花里胡哨的方法,但是够用上,321上链接(时间段的起止可以用hutool工具包,更加方便一点,虽然但是能用就行)
/**
* 24小时HTTP请求方式
*
* @param params
* @return
*/
@Override
public JSONObject findApiLogByThourHttp(JSONObject params) {
JSONObject result = new JSONObject();
try {
JSONObject ap = new JSONObject();
List<String> hintList = new ArrayList<String>();
//参数校验
// if (StringUtils.isBlank(params.getString("start_time"))) {
// hintList.add("start_time");
// }
if (StringUtils.isBlank(params.getString("time_range"))) {
hintList.add("time_range");
}
if (!hintList.isEmpty()) {
result.put("code", ResponseCode.C0001);
result.put("keyword", org.apache.commons.lang3.StringUtils.join(hintList));
return result;
}
List<InvokeModel> list = new ArrayList<>();
long current = System.currentTimeMillis();
long zero1 = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
long zero = getStartTimeOfDay();
long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
long l = System.currentTimeMillis() - System.currentTimeMillis() % 3600000;
//long onehour = current - 24 * 60 * 60 * 1000 - 1;
long onehour;
long jgtime = 0;
String ttnum = params.getString("time_range");
switch (ttnum) {
case "1":
onehour = current - 1 * 60 * 60 * 1000 - 1;
jgtime=60 * 1000;
break;
case "24":
onehour = current - 24 * 60 * 60 * 1000 - 1;
jgtime=60 * 60 * 1000;
break;
case "7":
onehour = zero - 7 * 24 * 60 * 60 * 1000 ;
current=twelve;
jgtime=24 * 60 * 60 * 1000;
break;
case "30":
/* Calendar now = Calendar.getInstance();
Date nowtime = new Date();
now.setTime(nowtime); //塞入当前日期
now.set(Calendar.HOUR_OF_DAY, 0); //将时分秒设置成0,便于格式获取,若格式的为yyyy-MM-dd则不需要
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.add(Calendar.MONTH, -1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
Date date = simpleDateFormat.parse(String.valueOf(now.getTime()));
onehour = date.getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();*/
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
Date date =new Date();
date.setTime(zero);
Calendar now = Calendar.getInstance();
now.setTime(date); //塞入当前日期
now.add(Calendar.MONTH, -1);
onehour = now.getTimeInMillis();
current=twelve;
jgtime=24 * 60 * 60 * 1000;
break;
default:
onehour = current - 1 * 60 * 60 * 1000 - 1;
jgtime=60 * 1000;
break;
}
// System.out.println("zerooo"+zero+"twwlwllw"+twelve);
long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2021-05-07 20:00:00", new ParsePosition(0)).getTime() / 1000;
// 闭区间//1小时范围内
QueryBuilder qb1 = QueryBuilders.rangeQuery("start_time").gte(onehour).lte(current);
// .includeLower(true) // 包含上界
//.includeUpper(true); // 包含下届
// 大于//成功的
// QueryBuilder qb2 = QueryBuilders.rangeQuery("id").gt(1);w
// 过滤多条件
QueryBuilder qb;
if (params.get("service_id") != null && params.get("service_id") != "") {
QueryBuilder qb3 = QueryBuilders.termQuery("service_id.keyword", params.get("service_id"));
// 单个字符串//状态是成功的
if (params.get("status") != null && params.get("status") != "") {
// 单个字符串//状态是成功的
QueryBuilder qb0 = QueryBuilders.termQuery("status", params.get("status"));
qb = QueryBuilders.boolQuery().must(qb1).must(qb0).must(qb3);
} else {
qb = QueryBuilders.boolQuery().must(qb1).must(qb3);
}
} else {
if (params.get("status") != null && params.get("status") != "") {
// 单个字符串//状态是成功的
QueryBuilder qb0 = QueryBuilders.termQuery("status", params.get("status"));
qb = QueryBuilders.boolQuery().must(qb1).must(qb0);
} else {
qb = QueryBuilders.boolQuery().must(qb1);
}
}
JSONObject json = new JSONObject();
json.put("query",JSON.parseObject(qb.toString()));
json.put("size",10000);
JSONArray jsonArray=new JSONArray();
JSONObject jobj=new JSONObject();
jobj.put("start_time","desc");
jsonArray.add(jobj);
json.put("sort",jsonArray);
Map<String, String> map = RestLowEsHttpUtil.RestLowEsHttpUtil(this.getHost(),json.toString(),"POST",database+"/ds_invoke");
if("-1".equals(map.get("code"))){
result.put("code", ResponseCode.FAILED);
return result;
}
JSONObject dataObject = JSON.parseObject(map.get("data"));
JSONObject hitsObject = (JSONObject) dataObject.get("hits");
JSONArray hitsListArray = (JSONArray) hitsObject.get("hits");
List<InvokeModel> invokeModelList = new ArrayList<>();
for(int i=0;i<hitsListArray.size();i++) {
JSONObject sourceObject = (JSONObject) hitsListArray.getJSONObject(i).get("_source");
InvokeModel invokeModel = JSON.toJavaObject(sourceObject,InvokeModel.class);
invokeModelList.add(invokeModel);
}
// System.out.println(invokeModelList);
int total = (Integer) hitsObject.get("total");
// System.out.println(dataObject);
if (invokeModelList.size() != 0) {
JSONObject timedun = new JSONObject();
long finalJgtime = jgtime;
long finalCurrent = current;
invokeModelList.stream().flatMap(e -> {
for (long i = onehour; i < finalCurrent; i += finalJgtime) {
int j = 0;
if (e.getStart_time() > i && e.getStart_time() < i + finalJgtime) {
j++;
}
if (j > 0) {
if ((timedun.get(stampToDate(String.valueOf(i))) != null) && (int) timedun.get(stampToDate(String.valueOf(i))) > 0) {
timedun.put(stampToDate(String.valueOf(i)), (int) timedun.get(stampToDate(String.valueOf(i))) + 1);
} else {
timedun.put(stampToDate(String.valueOf(i)), j);
}
}
}
return null;
}).collect(Collectors.toList());
ap.put("timedun", timedun);
}
ap.put("count", total);
//ap.put("list", list);
result.put("data", ap);
result.put("code", ResponseCode.OK);
} catch (Exception e) {
logger.error("es add err...\n", e);
result.put("code", ResponseCode.FAILED);
}
return result;
}
/**
* 曲线次数
*
* @param params
* @return
*/
public JSONObject findApiLogByThourHttpNew(JSONObject params) {
JSONObject result = new JSONObject();
try {
JSONObject ap = new JSONObject();
List<String> hintList = new ArrayList<String>();
if (StringUtils.isBlank(params.getString("time_range"))) {
hintList.add("time_range");
}
if (!hintList.isEmpty()) {
result.put("code", ResponseCode.C0001);
result.put("keyword", org.apache.commons.lang3.StringUtils.join(hintList));
return result;
}
// String now = DateUtil.now();
// long nowCurrent=DateUtil.currentSeconds();
//
// DateUtil.offsetMinute(Date date, int offset)
List<InvokeModel> list = new ArrayList<>();
long current = System.currentTimeMillis();
long zero1 = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
long zero = getStartTimeOfDay();
long twelve = zero + 24 * 60 * 60 * 1000 - 1;//今天23点59分59秒的毫秒数
long l = System.currentTimeMillis() - System.currentTimeMillis() % 3600000;
//long onehour = current - 24 * 60 * 60 * 1000 - 1;
long onehour;
long jgtime = 0;
String ttnum = params.getString("time_range");
switch (ttnum) {
case "1":
onehour = current - 1 * 60 * 60 * 1000 - 1;
jgtime=60 * 1000;
break;
case "24":
onehour = current - 24 * 60 * 60 * 1000 - 1;
jgtime=60 * 60 * 1000;
break;
case "7":
onehour = zero - 7 * 24 * 60 * 60 * 1000 ;
current=twelve;
jgtime=24 * 60 * 60 * 1000;
break;
case "30":
/* Calendar now = Calendar.getInstance();
Date nowtime = new Date();
now.setTime(nowtime); //塞入当前日期
now.set(Calendar.HOUR_OF_DAY, 0); //将时分秒设置成0,便于格式获取,若格式的为yyyy-MM-dd则不需要
now.set(Calendar.MINUTE, 0);
now.set(Calendar.SECOND, 0);
now.add(Calendar.MONTH, -1);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));
Date date = simpleDateFormat.parse(String.valueOf(now.getTime()));
onehour = date.getTime() / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();*/
// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
// sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
Date date =new Date();
date.setTime(zero);
Calendar now = Calendar.getInstance();
now.setTime(date); //塞入当前日期
now.add(Calendar.MONTH, -1);
onehour = now.getTimeInMillis();
current=twelve;
jgtime=24 * 60 * 60 * 1000;
break;
default:
onehour = current - 1 * 60 * 60 * 1000 - 1;
jgtime=60 * 1000;
break;
}
// System.out.println("zerooo"+zero+"twwlwllw"+twelve);
long time = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse("2021-05-07 20:00:00", new ParsePosition(0)).getTime() / 1000;
// 闭区间//1小时范围内
QueryBuilder qb1 = QueryBuilders.rangeQuery("start_time").gte(onehour).lte(current);
// .includeLower(true) // 包含上界
//.includeUpper(true); // 包含下届
// 大于//成功的
// QueryBuilder qb2 = QueryBuilders.rangeQuery("id").gt(1);w
// 过滤多条件
QueryBuilder qb;
if (params.get("service_id") != null && params.get("service_id") != "") {
QueryBuilder qb3 = QueryBuilders.termQuery("service_id.keyword", params.get("service_id"));
// 单个字符串//状态是成功的
if (params.get("status") != null && params.get("status") != "") {
// 单个字符串//状态是成功的
QueryBuilder qb0 = QueryBuilders.termQuery("status", params.get("status"));
qb = QueryBuilders.boolQuery().must(qb1).must(qb0).must(qb3);
} else {
qb = QueryBuilders.boolQuery().must(qb1).must(qb3);
}
} else {
if (params.get("status") != null && params.get("status") != "") {
// 单个字符串//状态是成功的
QueryBuilder qb0 = QueryBuilders.termQuery("status", params.get("status"));
qb = QueryBuilders.boolQuery().must(qb1).must(qb0);
} else {
qb = QueryBuilders.boolQuery().must(qb1);
}
}
AggregationBuilder agg ;
switch (ttnum) {
case "1":
agg = AggregationBuilders.dateHistogram("groupDate").field("start_time").dateHistogramInterval(DateHistogramInterval.MINUTE).offset("-8h");
break;
case "24":
agg = AggregationBuilders.dateHistogram("groupDate").field("start_time").dateHistogramInterval(DateHistogramInterval.HOUR).offset("-8h");
break;
case "7":
agg = AggregationBuilders.dateHistogram("groupDate").field("start_time").dateHistogramInterval(DateHistogramInterval.DAY).offset("-8h");
break;
case "30":
agg = AggregationBuilders.dateHistogram("groupDate").field("start_time").dateHistogramInterval(DateHistogramInterval.DAY).offset("-8h");
break;
default:
agg = AggregationBuilders.dateHistogram("groupDate").field("start_time").dateHistogramInterval(DateHistogramInterval.DAY).offset("-8h");
}
JSONObject aggjson=JSONObject.parseObject(agg.toString());
String dsl1=aggjson.toString();
aggjson.getJSONObject("groupDate").getJSONObject("date_histogram").remove("order");
// JSONObject json = new JSONObject();
// json.put("query",JSON.parseObject(qb.toString()));
// json.put("size",10000);
// JSONArray jsonArray=new JSONArray();
// JSONObject jobj=new JSONObject();
// jobj.put("start_time","desc");
// jsonArray.add(jobj);
// json.put("sort",jsonArray);
JSONObject json = new JSONObject();
json.put("query", JSON.parseObject(qb.toString()));
json.put("size", "0");
json.put("aggs",aggjson);
String dsl=json.toString();
Map<String, String> map = RestLowEsHttpUtil.RestLowEsHttpUtil(this.getHost(),dsl,"POST",database+"/ds_invoke");
if("-1".equals(map.get("code"))){
result.put("code", ResponseCode.FAILED);
return result;
}
JSONObject dataObject = JSON.parseObject(map.get("data"));
JSONObject hitsObject = dataObject.getJSONObject("aggregations").getJSONObject("groupDate");
JSONArray buckets = hitsObject.getJSONArray("buckets");
int total = (Integer) dataObject.getJSONObject("hits").get("total");
JSONObject timedun = new JSONObject();
buckets.stream().forEach(e->{
JSONObject obj = (JSONObject) e;
timedun.put(stampToDate(obj.getString("key")), obj.getIntValue("doc_count"));
});
ap.put("timedun", timedun);
ap.put("count", total);
//ap.put("list", list);
result.put("data", ap);
result.put("code", ResponseCode.OK);
} catch (Exception e) {
logger.error("es add err...\n", e);
result.put("code", ResponseCode.FAILED);
}
return result;
}
上dsl语句
{
"size": "0",
"query": {
"bool": {
"adjust_pure_negative": true,
"must": [
{
"range": {
"start_time": {
"include_lower": true,
"include_upper": true,
"from": 1642521600000,
"boost": 1,
"to": 1643212799999
}
}
},
{
"term": {
"status": {
"boost": 1,
"value": "false"
}
}
}
],
"boost": 1
}
},
"aggs": {
"groupDate": {
"date_histogram": {
"field": "start_time",
"offset": -28800000,
"interval": "1d",
"keyed": false,
"min_doc_count": 0
}
}
}
}
如果对你有用,有帮助的话,点个赞再走吧
更多推荐
已为社区贡献2条内容
所有评论(0)