一、索引设置
1、如果是需要时间聚合的话则需要设置时间是date索引类型
curl --location --request PUT 'http://10.211.55.3:9200/vote_logs_statics' \
--header 'Content-Type: application/json' \
--data-raw '{
"mappings" : {
"properties" : {
"activityId" : {
"type" : "long"
},
"statCount" : {
"type" : "long"
},
"pv" : {
"type" : "long"
},
"created_at": {
"type": "date",
"index":true,
"format": "strict_date_optional_time||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
},
"settings" : {
"index" : {
"number_of_shards" : 1,
"number_of_replicas" : 1
}
}
}'
二、查询语句
{
"query": {
"bool": {
"must": [
{
"range": {
"created_at": {
"gte": "2020-08-19 00:00:00",
"lte": "2020-08-20 00:00:00",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
]
}
},
"size": 0,
"aggs": {
"sales": {
"date_histogram": {
"field": "created_at",
"interval": "minute",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
}
三、时间聚合+去重
{
"query": {
"bool": {
"filter": [
{
"term": {
"activity_id": "1"
}
},
{
"range": {
"created_at": {
"gte": "2020-07-25 13:48:36",
"lt": "2020-09-25 15:48:36",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
]
}
},
"size": 0,
"aggs": {
"list": {
"date_histogram": {
"min_doc_count": 0,
"field": "created_at",
"interval": "minute",
"format": "yyyy-MM-dd HH:mm:ss"
},
"aggs": {
"user_count": {
"cardinality": {
"field": "user_id"
}
}
}
}
}
}