我正在接收来自大量客户端应用程序的请求/事件。我想使用elasticsearch 来找出我的最高流量点是什么时候。
我尝试过的一件事是使用嵌套直方图进行过滤器聚合,然后使用嵌套的“术语”聚合,通过脚本字段获取一天中的不同时间。以下是我的尝试,它的性能非常糟糕(正如我所期望的,因为我正在每个文档执行一个脚本)。
{
"aggs": {
"sites_within_range": {
"filter" : {
"range" : {
"occurred" : {
"gt" : "now-1M"
}
}
},
"aggs": {
"sites_over_time": {
"date_histogram": {
"field": "occurred",
"interval": "week"
},
"aggs":{
"site_names": {
"terms": {
"script": "doc['occurred'].date.getHourOfDay()",
"size": 10000
}
}
}
}
}
}
}
}
我还考虑过将我想要查询的日期元素存储为文档的不同部分,例如:
{
"date": "actual datetime",
"day": "monday",
"hour": 8
"minute": 37
}
这对我来说也像是错误的答案。
经过一番调查,看起来我可能对 1.1 中的新基数/百分比聚合感兴趣?
同类问题已经解决了这个线程 https://stackoverflow.com/questions/29002152/how-to-show-day-names-using-date-histogram-aggregation-in-elascticsearch.
根据您的问题调整解决方案,我们需要编写一个脚本将日期转换为一天中的小时:
Date date = new Date(doc['created_at'].value) ;
java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('HH');
format.format(date)
并在查询中使用它:
{
"aggs": {
"perWeekDay": {
"filter" : {
"range" : {
"occurred" : {
"gt" : "now-1M"
}
}
},
"aggs": {
"terms": {
"script": "Date date = new Date(doc['created_at'].value) ;java.text.SimpleDateFormat format = new java.text.SimpleDateFormat('HH');format.format(date)"
}
}
}
}
并且您可以了解一天中各个小时的流量。
注意:在文档中存储小时/天/分钟是进行此类聚合的最有效方法。我的回答假设您不想存储该信息。脚本通常效率不高。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)