每周每小时每小时的Elasticsearch订单/时间

时间:2019-11-02 18:05:49

标签: python json elasticsearch aggregate

print("Hello everybody1")

对于我的硕士,我有1门编程课程,这是第一次开设该课程,因此会导致很多问题。我们对Elasticsearch的理解是有限的。对于作业,我有一个文件,其中包含按工作日排序的订单的旅行时间。目前,我遇到以下两个问题:

  • 平均旅行时间秒(每周工作日每小时每小时)。下面的代码包含每周每个工作日的总订单,我不知道如何将一天划分为几个小时。
searchbody_total_hours ={
                "size": 0,
                "aggs":{
                    "time_per_hour_per_day_of_week":   {
                        "terms":   {
                            "size": 24,
                            "script":{
                                "lang": "painless",
                                "source": "doc['datetime_of_order'].value.dayOfWeek"
                            }
                        },
                        "aggs": {
                            "number_of_weeks":  {
                                "date_histogram":   {
                                    "field": "datetime_of_order",
                                    "interval": "week"
                                }
                            },
                            "aggs": {
                                "totaltime": {
                                    "sum": {
                                        "field": "travel_time"
                                    }
                                },
                                "average_travel_time":  {
                                    "bucket_script":  {
                                        "buckets_path":  {
                                            "totaltime": "travel_time",
                                            "number_of_weeks": "number_of_weeks._bucket_count"
                                        },
                                        "script": "params.totaltime / params.number_of_weeks"  
                                    }
                                }
                            }
                        }
                    }
                }
}
  • 平均订单(每周工作日每小时订购)。
searchbody_total_orders = {
        "query":{
            "bool": {
                "must":{
                    "match": {"weekdayName" : "Monday"}
                }
            }
        },
        "aggs": {
            "total_orders_per_hour_per_week_of_day":   {
                "terms":{
                    "size": 15,
                    "script":   {
                        "lang": "painless",
                        "source": "doc['datetime_of_order'].value.hourOfDay"
                    }
                },
                "aggs": {
                    "number_of_weeks":{
                        "date_histogram":{
                            "field": "datetime_of_order",
                            "interval": "week"
                        }
                    },
                    "average_orders_per_hour_per_day_of_week":  {
                        "bucket_script":{
                            "buckets_path": {
                                "doc_count": "_count",
                                "number_of_weeks": "number_of_weeks._bucket_count",

                            },
                            "script": "params.doc_count / params.number_of_weeks"
                        }
                    }   
                }
            }
        }
}

如果有人可以通过某些方法来改进我的代码或拥有更多信息的链接,我将不胜感激。

亲切的问候

科恩

0 个答案:

没有答案