elasticsearch,如何获取每天每小时的订单

dgsult0t  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(452)

我有一个包含订单数据的文件。我想知道每天每小时的平均订单量。该文件包含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"])
uqdfh47h

uqdfh47h1#

如果我这次理解正确的话,你想得到星期一,星期二每小时的平均值。。。所以应该有全天候的结果。如果是,请执行以下操作:

{
  "size": 0,
  "aggs": {
    "orders_per_hour_of_day": {
      "terms": {
        "script" : {
            "lang": "painless",
            "source": "doc['dateTime'].value.getHour()"
        },
        "size": 24
      },
      "aggs": {
        "total_orders_per_day_of_week": {
          "terms": {
            "script": {
              "lang": "painless",
              "source": "doc['dateTime'].value.getDayOfWeekEnum()"
            }
          },
          "aggs": {
            "number_of_weeks": {
              "date_histogram": {
                "field": "dateTime",
                "calendar_interval": "week"
              }
            },
            "average_orders_per_hour_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"
              }
            }
          }
        }
      }
    }
  }
}

代码改编自:https://discuss.elastic.co/t/average-per-day-of-week-aggregation/124132
我不确定我是否明白你的问题。听起来你想得到星期一,星期二的平均订单。。。星期天。如果是这个问题,你可以把每个桶的24小时分成两部分。

{
  "size": 0,
  "aggs": {
    "orders_per_day": {
      "terms": {
        "script": {
          "lang": "painless",
          "source": "doc['dateTime'].value.dayOfWeek"
        }
      },
      "aggs": {
        "avg_per_hour": {
          "bucket_script": {
            "buckets_path": {
              "count": "_count"
            },
            "script": "params.count / 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

相关问题