查询应该是这样的:
GET {index_name}/{type}/_search
{
"size": 0, // no need to display search result, can boost query speed
"aggs": {
"unique_visited_page": {
"terms": {
"field": "visited_page" // this must be indexed with keyword type
},
"aggs": {
"visit_page_per_hour" : {
"date_histogram" : {
"field" : "date_field",
"interval" : "hour"
}
}
}
}
}
}
我们首先按 visited_page 进行汇总,然后按每个 visited_page 进行汇总,我们每小时向下钻取以获取计数。
使用我的示例数据的示例响应
{
...
"hits": {
"total": 4,
"max_score": 0,
"hits": []
},
"aggregations": {
"unique_visited_page": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "contact.html",
"doc_count": 2,
"visit_page_per_hour": {
"buckets": [
{
"key_as_string": "2018-07-24T14:00:00.000Z",
"key": 1532440800000,
"doc_count": 1
},
{
"key_as_string": "2018-07-24T15:00:00.000Z",
"key": 1532444400000,
"doc_count": 1
}
]
}
},
{
"key": "index.html",
"doc_count": 1,
"visit_page_per_hour": {
"buckets": [
{
"key_as_string": "2018-07-24T13:00:00.000Z",
"key": 1532437200000,
"doc_count": 1
}
]
}
},
{
"key": "page.html",
"doc_count": 1,
"visit_page_per_hour": {
"buckets": [
{
"key_as_string": "2018-07-24T13:00:00.000Z",
"key": 1532437200000,
"doc_count": 1
}
]
}
}
]
}
}
}
结果的关键是我们的 visited_page 值,然后它将每小时汇总并返回 doc_count。 doc_count 可能是您想要的值。
希望对你有帮助。