以下几种应用场景
1.计算每个小时数据量(8月29到31号每个时段的数据量)
GET /core_petition/_search?size=0
{
"query": {
"bool": {
"filter": {
"range": {
"registorOn": {
"gte": "2022-08-29 16:06:00",
"lte": "2022-08-31 16:06:00"
}
}
}
}
},
"aggs": {
"group_by_state": {
"date_histogram": {
"field": "registorOn",
"calendar_interval":"hour",
"min_doc_count": 0
}
}
}
}
2.根据字段分组得出每年的数据量
会得到该字段各个时段的数据(相当于对字段分组再对时间分组)
GET /core_petition/_search?size=0
{
"aggs": {
"sourceWayCount": {
"terms": {
"field": "sourceWay",--分组字段
"size": 100
},
"aggs": {
"hourCount": {
"date_histogram": {
"field": "registorOn",
"calendar_interval": "year",
"format": "yyyy",
"min_doc_count": 1
}
}
}
}
},
"query": {
"range": {
"registorOn": {
"gte": "2021-01-01 00:00:00",
"lte": "2022-08-04 23:59:59"
}
}
}
}
3.将时间分组放于前可以得到按时间分组的值
得到各个时段该字段分组的数据(相当于对时间分组再对字段分组)
GET /core_petition/_search
{
"query": {
"bool": {
"must": [{
"range": {
"registorOn": {
"gte": "2021-01-01 00:00:00",
"lte": "2022-08-01 00:00:00"
}
}
}]
}
},"size": 0,
"aggs": {
"hourCount": {
"date_histogram": {
"field": "registorOn",
"calendar_interval": "year",
"format": "yyyy",
"min_doc_count": 1
},
"aggs": {
"typicalCount": {
"terms": {
"field": "complaintQuality",
"size": 3
}
}
}
}
}
}
calendar_interval:year, quarter, month, week, day, hour, minute, second(年份、季度、月、周、日、小时、分钟、秒)。