【发布时间】:2018-05-17 10:54:09
【问题描述】:
考虑我在弹性搜索索引中有一些文档(在下面提到的结构中)
{
"xid": "1234567",
"time": "12/5/12 5:49 AM",
"data": [
{
"id": "abc",
"amount": 400
},
{
"id": "def",
"amount": 200
}
]
}
{
"xid": "1234568",
"time": "13/5/12 7:23 AM",
"data": [
{
"id": "abc",
"amount": 400
},
{
"id": "ghi",
"amount": 300
}
]
}
现在在每个文档的数据数组中,我想按 id 分组并找到总和。
对于给定的 2 个文档,解决方案类似于
{
"id" : "abc",
"total" :800
},
{
"id" : "def",
"total" :200
},
{
"id" : "ghi",
"total" :300
}
请帮助我构建我的请求查询。
我最初的方法是
{
"aggs": {
"group_by_id": {
"terms": {
"field": "data.id.keyword"
},
"aggs": {
"total" : {
"sum": {
"field": "data.amount"
}
}
}
}
}
}
下面给出的查询结果不是预期的结果。
{
"id" : "abc",
"total" :1300
},
{
"id" : "def",
"total" :600
},
{
"id" : "ghi",
"total" :700
}
【问题讨论】:
标签: elasticsearch elasticsearch-5