【发布时间】:2018-09-12 08:04:30
【问题描述】:
我想在某个日期范围内每小时只获取一条记录。例如,如果我想获取这几天(28-08-2018 - 15-09-2018)之间的数据,几乎有 10000 条记录,但我想过滤结果以每小时只显示一条记录,所以我将 aggregation 与 date_histogram 一起使用,我每小时只能看到一条记录
查询:
{
"size" : 0,
"query": {
"bool": {
"must": [
{
"range": {
"createdtime": {
"gte": "1535201500000",
"lte": "1536756706000",
"boost": 2.0
}
}
},
{
"match": {
"gen": 1
}
},
{
"match": {
"Mid": 350404
}
}
]
}
},
"aggregations" : {
"runtime" : {
"date_histogram" : {
"field" : "createdtime",
"interval" : "1H",
"min_doc_count": 1
},"aggs": {
"tops": {
"top_hits": {
"size": 1
}
}
}
}
}
}
这里的问题是我想使用分页来显示这些结果,因为我知道到目前为止没有办法使用大小和聚合查询,我想知道有没有其他方法可以只获取一条记录一小时
【问题讨论】:
-
如果你真的需要分页,这意味着你的结果中有大量的桶。在这种情况下,您可以考虑规范化数据,例如拆分为基于小时的索引,然后您可以尝试分页。否则只需实现您自己的分页逻辑。
-
@GkhnSr for the given example dates there is less number of records, but there is a possibility to get more data when chose the date difference in months, years
标签: elasticsearch aggregate-functions