Elasticsearch:如何在嵌套存储桶上聚合

问题描述

我有两个字段,query_timestamp和country。我希望看到70天内每天的平均文档数。我希望看到每个国家的这个数字。

例如加纳平均每天有20笔交易,美国平均每天有15笔交易,等等。

在砸了我的键盘和文档之后,我离我很近,但还不很远。

我的查询

{
"size": 0,"query": {
    "range": {
        "query_timestamp": {
            "gt": "Now-70d"
        }
    }
},"aggs": {
    "orders_per_day": {
        "date_histogram": {
            "field": "query_timestamp","interval": "day"
        },"aggs": {
            "keys": {
                "terms": {
                    "field": "country.keyword"
                }
            },"average_per_country": {
                "avg_bucket": {
                    "buckets_path": "keys._count"
                }
            }
        }
    }
}

}

我点击了_search端点,并使用POST请求传递了此数据。

以下是回复

{
"took": 431,"timed_out": false,"_shards": {
    "total": 78,"successful": 78,"skipped": 0,"Failed": 0
},"hits": {
    "total": 5335542,"max_score": 0,"hits": []
},"aggregations": {
    "orders_per_day": {
        "buckets": [
            {
                "key_as_string": "2020-06-19T00:00:00.000Z","key": 1592524800000,"doc_count": 3000,"keys": {
                    "doc_count_error_upper_bound": 133,"sum_other_doc_count": 7356,"buckets": [
                         {
                            "key": "COL","doc_count": 0
                        },{
                            "key": "IND","doc_count": 3000
                        }
                    ]
                },"average_per_country": {
                    "value": 1500
                }
            },{
                "key_as_string": "2020-06-20T00:00:00.000Z","key": 1592611200000,"doc_count": 2000,"keys": {
                    "doc_count_error_upper_bound": 316,"sum_other_doc_count": 17405,"buckets": [
                        {
                            "key": "COL","doc_count": 2000
                        }
                    ]
                },"average_per_country": {
                    "value": 1000
                }
            },]
    }
}

响应实际上是数千行,但是我删除了多余的信息,所以您明白了。关键是,我没有获得整个时间段内每个国家的平均值,而是得到了所有国家每天的平均值。有什么想法吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)