简体   繁体   English

获取date_histogram,elasticsearch的平均值

[英]Get buckets average of a date_histogram, elasticsearch

I have the following query where get the a data and I create an aggregation of each past hour: 我有以下查询获取数据,我创建每过去一小时的聚合:

    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",

                },
            }
        }
    }

I would like to get the average of a field from each hour interval (each bucket created by the aggregation). 我想得到每小时间隔(由聚合创建的每个桶)的字段的平均值。 In this article they talk about something similar with what I wish to do: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/_looking_at_time.html ("What was the average latency of our website every hour in the last week?"). 在这篇文章中,他们谈论了类似于我想做的事情: http//www.elasticsearch.org/guide/en/elasticsearch/guide/current/_looking_at_time.html (“我们网站每小时的平均延迟是多少?在上周?“)。 However, they don't explain exactly what to do in this case. 但是,他们没有解释在这种情况下究竟要做什么。

Does anyone know how to do that? 有谁知道这是怎么做到的吗?

Just realized that I could do a nested aggregation and then calculate the average of a field inside a aggregation. 刚刚意识到我可以进行嵌套聚合,然后计算聚合内字段的平均值。 Here is what I did and it's working properly now: 这是我做的,它现在正常工作:

 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" } 
                        }
                    }
                }
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM