【问题标题】:Get buckets average of a date_histogram, elasticsearch获取 date_histogram、elasticsearch 的桶平均值
【发布时间】:2015-02-12 22:33:15
【问题描述】:

我有以下查询,其中获取 a 数据并创建过去一小时的聚合:

    query = {
        "query": {
            "bool": {          
                "must": [
                    { "term": {"deviceId":device} },
                    { "match": {"eventType":"Connected"} } 
                ],
                "must_not":[{
                        "query_string": {
                            "query": "Pong",
                            "fields": ["data.message"]
                        }
                    },
                ] 
            },

        },
        "size": 0,
        "sort": [{ "timestamp": { "order": "desc" }}],
        "aggs" : {
            "time_buckets" : {
                "date_histogram" : {
                    "field" : "timestamp",
                    "interval" : "hour",

                },
            }
        }
    }

我想从每小时间隔(聚合创建的每个存储桶)中获取字段的平均值。在这篇文章中,他们谈到了与我想做的类似的事情: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_looking_at_time.html (“上周我们网站每小时的平均延迟是多少?”)。但是,他们没有准确解释在这种情况下该怎么做。

有人知道怎么做吗?

【问题讨论】:

    标签: javascript elasticsearch average datahistory


    【解决方案1】:

    刚刚意识到我可以进行嵌套聚合,然后计算聚合内字段的平均值。这是我所做的,它现在可以正常工作:

     query = {
                "query": {
                    "bool": {          
                        "must": [
                            { "term": {"deviceId":device} },
                            { "match": {"eventType":"Connected"} } 
                        ],
                        "must_not":[{
                                "query_string": {
                                    "query": "Pong",
                                    "fields": ["data.message"]
                                }
                            },
                        ] 
                    },
    
                },
                "size": 0,
                "sort": [{ "timestamp": { "order": "desc" }}],
                "aggs" : {
                    "time_buckets" : {
                        "date_histogram" : {
                            "field" : "timestamp",
                            "interval" : "day"
                        },
                        "aggs" : {
                            "avg_battery" : {
                                "avg": { "field": "data.battery-level" } 
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2021-05-30
      • 2017-04-25
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-10
      • 2018-07-11
      相关资源
      最近更新 更多