gpt4 book ai didi

elasticsearch - Elasticsearch汇总超过每个文档的列表

转载 作者:行者123 更新时间:2023-12-03 00:00:47 29 4
gpt4 key购买 nike

我将带有时间戳的客户购买记录存储在Elasticsearch中,作为每个用户的列表:

{
"_index": "test",
"_type": "users",
"_id": "1",
"_score": 1,
"_source": {
"plist": [
{
"date": "2017-03-04 15:20:00",
"price": "57.20"
},
{
"date": "2017-03-05 10:20:00",
"price": "34.00"
}
]
}
}
}
{
"_index": "test",
"_type": "users",
"_id": "2",
"_score": 1,
"_source": {
"plist": [
{
"date": "2017-04-04 10:20:00",
"price": "7.00"
},
{
"date": "2017-04-05 10:20:00",
"price": "4.00"
},
{
"date": "2017-04-06 10:20:00",
"price": "54.00"
}
]
}
}

如何计算最近7天的平均购买金额,购买数量和每位用户的总消费金额?

最佳答案

如果可以选择重新编制索引,则将数据更改为:

{
"_index": "test",
"_type": "users",
"_id": "1",
"_score": 1,
"_source": {
"user_id": 1
"date": "2017-03-04 15:20:00",
"price": 57.20
}
}
{
"_index": "test",
"_type": "users",
"_id": "2",
"_score": 1,
"_source": {
"user_id": 1
"date": "2017-03-05 10:20:00",
"price": 34.00
}
}
{
"_index": "test",
"_type": "users",
"_id": "3",
"_score": 1,
"_source": {
"user_id": 2
"date": "2017-04-04 10:20:00",
"price": 7.00
}
}
...

然后,您可以运行如下查询:
GET test/users/_search
{
"query": {
"bool": {
"filter": {
"range": {
"date": {
"gte": "now() - 7d"
}
}
}
}
},
"aggs": {
"users": {
"terms": {
"field": "user_id",
"size": 10
},
"aggs": {
"count": {
"value_count": {
"field": "user_id"
}
},
"sum": {
"sum": {
"field": "price"
}
},
"average": {
"avg": {
"field": "price"
}
}
}
}
}
}

关于elasticsearch - Elasticsearch汇总超过每个文档的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49326511/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com