世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问:auto_date_histogram有什么特点?
答:
问:auto_date_histogram如何使用?
答:
# 删除
DELETE /auto_date_histogram_test
# 映射
PUT /auto_date_histogram_test
{
"mappings": {
"properties": {
"date": {"type": "date"}
}
}
}
# 索引
POST /auto_date_histogram_test/_bulk?refresh
{"index": {"_id": 1}}
{"date": "2021-09-27"}
{"index": {"_id": 2}}
{"date": "2021-08-27"}
{"index": {"_id": 3}}
{"date": "2021-07-23"}
{"index": {"_id": 4}}
{"date": "2021-05-19"}
{"index": {"_id": 5}}
{"date": null}
# 搜索
GET /auto_date_histogram_test/_search
{
"size": 0,
"aggs": {
"date_aggs": {
"auto_date_histogram": {
"field": "date",
"buckets": 10,
"format": "yyyy-MM-dd",
"missing": "2021-09-08"
}
}
}
}
# 结果
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"date_aggs" : {
"buckets" : [
{
"key_as_string" : "2021-05-01",
"key" : 1619827200000,
"doc_count" : 1
},
{
"key_as_string" : "2021-06-01",
"key" : 1622505600000,
"doc_count" : 0
},
{
"key_as_string" : "2021-07-01",
"key" : 1625097600000,
"doc_count" : 1
},
{
"key_as_string" : "2021-08-01",
"key" : 1627776000000,
"doc_count" : 1
},
{
"key_as_string" : "2021-09-01",
"key" : 1630454400000,
"doc_count" : 2
}
],
"interval" : "1M"
}
}
}



