世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。
问:rate有什么特点?
答:
问:rate如何使用?
答:
DELETE /rate_test
PUT /rate_test
{
"mappings": {
"properties": {
"date": {"type": "date"},
"num": {"type": "integer"}
}
}
}
POST /rate_test/_bulk
{"index": {"_id": 1}}
{"date": "2020-01-01", "num": 3}
{"index": {"_id": 2}}
{"date": "2020-01-10", "num": 7}
{"index": {"_id": 3}}
{"date": "2020-03-01", "num": 5}
{"index": {"_id": 4}}
{"date": "2020-03-05", "num": 9}
{"index": {"_id": 5}}
{"date": "2020-05-11", "num": 1}
# 年 - 每个月num和 * 12
GET /rate_test/_search
{
"size": 0,
"aggs": {
"date_aggs": {
"date_histogram": {
"field": "date",
"calendar_interval": "month"
},
"aggs": {
"rate_aggs": {
"rate": {
"field": "num",
"unit": "year"
}
}
}
}
}
}
# 结果
{
"took" : 2,
"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" : "2020-01-01T00:00:00.000Z",
"key" : 1577836800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 120.0
}
},
{
"key_as_string" : "2020-02-01T00:00:00.000Z",
"key" : 1580515200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-03-01T00:00:00.000Z",
"key" : 1583020800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 168.0
}
},
{
"key_as_string" : "2020-04-01T00:00:00.000Z",
"key" : 1585699200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-05-01T00:00:00.000Z",
"key" : 1588291200000,
"doc_count" : 1,
"rate_aggs" : {
"value" : 12.0
}
}
]
}
}
}
# 日 - 每个月num的和 / 30
GET /rate_test/_search
{
"size": 0,
"aggs": {
"date_aggs": {
"date_histogram": {
"field": "date",
"calendar_interval": "month"
},
"aggs": {
"rate_aggs": {
"rate": {
"field": "num",
"unit": "day"
}
}
}
}
}
}
# 结果
{
"took" : 0,
"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" : "2020-01-01T00:00:00.000Z",
"key" : 1577836800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 0.3225806451612903
}
},
{
"key_as_string" : "2020-02-01T00:00:00.000Z",
"key" : 1580515200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-03-01T00:00:00.000Z",
"key" : 1583020800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 0.45161290322580644
}
},
{
"key_as_string" : "2020-04-01T00:00:00.000Z",
"key" : 1585699200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-05-01T00:00:00.000Z",
"key" : 1588291200000,
"doc_count" : 1,
"rate_aggs" : {
"value" : 0.03225806451612903
}
}
]
}
}
}
# 月 - 每个月num的和
GET /rate_test/_search
{
"size": 0,
"aggs": {
"date_aggs": {
"date_histogram": {
"field": "date",
"calendar_interval": "month"
},
"aggs": {
"rate_aggs": {
"rate": {
"field": "num",
"unit": "month"
}
}
}
}
}
}
# 结果
{
"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" : "2020-01-01T00:00:00.000Z",
"key" : 1577836800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 10.0
}
},
{
"key_as_string" : "2020-02-01T00:00:00.000Z",
"key" : 1580515200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-03-01T00:00:00.000Z",
"key" : 1583020800000,
"doc_count" : 2,
"rate_aggs" : {
"value" : 14.0
}
},
{
"key_as_string" : "2020-04-01T00:00:00.000Z",
"key" : 1585699200000,
"doc_count" : 0,
"rate_aggs" : {
"value" : 0.0
}
},
{
"key_as_string" : "2020-05-01T00:00:00.000Z",
"key" : 1588291200000,
"doc_count" : 1,
"rate_aggs" : {
"value" : 1.0
}
}
]
}
}
}



