我的文档集里存储的是订单的每一步操作的状态,现在我的需求是统计每个订单的操作耗时,并根据耗时的时间段统计为直方图.
子聚集的代码如下:
GET /logstash-2020.05.18/_search
{
"size": 0,
"aggs": {
"order_bucket": {
"terms": {
"field": "fields.MerchantOrderNumber.keyword"
},
"aggs": {
"max_time": {
"max": {
"field": "@timestamp"
}
},
"min_time": {
"min": {
"field": "@timestamp"
}
},
"cost": {
"bucket_script": {
"buckets_path": {
"max_time": "max_time",
"min_time": "min_time"
},
"script": "params.max_time - params.min_time"
}
},
"selector": {
"bucket_selector": {
"buckets_path": {
"total_cost": "cost"
},
"script": {
"source": "params.total_cost > 5000"
}
}
}
}
}
}
}
得到的结果:
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"order_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 348,
"buckets" : [
{
"key" : "56E4E726FFFB95EA",
"doc_count" : 124,
"max_time" : {
"value" : 1.589791513266E12,
"value_as_string" : "2020-05-18T08:45:13.266Z"
},
"min_time" : {
"value" : 1.589791494322E12,
"value_as_string" : "2020-05-18T08:44:54.322Z"
},
"cost" : {
"value" : 18944.0
}
},
{
"key" : "6277B030C01E03DC",
"doc_count" : 124,
"max_time" : {
"value" : 1.589792736638E12,
"value_as_string" : "2020-05-18T09:05:36.638Z"
},
"min_time" : {
"value" : 1.589792719778E12,
"value_as_string" : "2020-05-18T09:05:19.778Z"
},
"cost" : {
"value" : 16860.0
}
}
...
]
}
}
}
子聚集的代码如下:
GET /logstash-2020.05.18/_search
{
"size": 0,
"aggs": {
"order_bucket": {
"terms": {
"field": "fields.MerchantOrderNumber.keyword"
},
"aggs": {
"max_time": {
"max": {
"field": "@timestamp"
}
},
"min_time": {
"min": {
"field": "@timestamp"
}
},
"cost": {
"bucket_script": {
"buckets_path": {
"max_time": "max_time",
"min_time": "min_time"
},
"script": "params.max_time - params.min_time"
}
},
"selector": {
"bucket_selector": {
"buckets_path": {
"total_cost": "cost"
},
"script": {
"source": "params.total_cost > 5000"
}
}
}
}
}
}
}
得到的结果:
{
"took" : 21,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 10000,
"relation" : "gte"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"order_bucket" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 348,
"buckets" : [
{
"key" : "56E4E726FFFB95EA",
"doc_count" : 124,
"max_time" : {
"value" : 1.589791513266E12,
"value_as_string" : "2020-05-18T08:45:13.266Z"
},
"min_time" : {
"value" : 1.589791494322E12,
"value_as_string" : "2020-05-18T08:44:54.322Z"
},
"cost" : {
"value" : 18944.0
}
},
{
"key" : "6277B030C01E03DC",
"doc_count" : 124,
"max_time" : {
"value" : 1.589792736638E12,
"value_as_string" : "2020-05-18T09:05:36.638Z"
},
"min_time" : {
"value" : 1.589792719778E12,
"value_as_string" : "2020-05-18T09:05:19.778Z"
},
"cost" : {
"value" : 16860.0
}
}
...
]
}
}
}
0 个回复