我有一个包含订单数据的文件。我想知道每天每小时的平均订单量。该文件包含52周的订单数据。我设法得到每周一,周二等每小时的数据,但还没有设法得到周一,周二等每小时的平均订单。
search_body = {
"size": 0,
"aggs": {
"total_orders_per_day_of_week": {
"terms": {
"script": {
"lang": "painless",
"source": "doc['dateTime'].value.dayOfWeek"
}
},
"aggs": {
"countPerHour": {
"terms": {
"size": 24,
"script": "return doc['dateTime'].value.getHour();"
}
}
}
}
}
}
也许我需要做一些我做的事情来计算平均每天的订单?
search_body = {
"size" : 0,
"aggs" : {
"orders_per_day" : {
"terms" : {
"script" : {
"lang": "painless",
"source": "doc['dateTime'].value.dayOfWeek"
}
},
"aggs" : {
"numberOfOccurrences" : {
"cardinality": {
"script" : {
"lang": "painless",
"source": "doc['dateTime'].value.dayOfYear"
}
}
}
},
}
}
}
for day in result["aggregations"]["orders_per_day"]["buckets"]:
print (day["key"], day["doc_count"], day["numberOfOccurrences"]["value"],
day["doc_count"]/day["numberOfOccurrences"]["value"])
1条答案
按热度按时间uqdfh47h1#
如果我这次理解正确的话,你想得到星期一,星期二每小时的平均值。。。所以应该有全天候的结果。如果是,请执行以下操作:
代码改编自:https://discuss.elastic.co/t/average-per-day-of-week-aggregation/124132
我不确定我是否明白你的问题。听起来你想得到星期一,星期二的平均订单。。。星期天。如果是这个问题,你可以把每个桶的24小时分成两部分。
bucket聚合:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html
bucket doc\u count的路径:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline.html#_special_paths