elasticsearch:如何在嵌套的bucket上聚合

xn1cxnb4  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(0)|浏览(265)

我有两个字段,查询时间戳和国家。我想看看70天内每天的平均文档数。我想看看每个国家的这个数字。
e、 g.加纳平均每天有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"
                }
            }
        }
    }
}

}
我点击了\u搜索端点,使用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": 0
                        },
                        {
                            "key": "IND",
                            "doc_count": 2000
                        }
                    ]
                },
                "average_per_country": {
                    "value": 1000
                }
            },
        ]
    }
}

回复实际上有几千行,但我已经删除了多余的信息,所以你得到的想法。关键是,我没有得到整个时间段内每个国家的平均值,而是得到所有国家每天的平均值。有什么想法吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题