【发布时间】:2021-02-22 20:33:46
【问题描述】:
我需要在 Timestamp 上创建 15m 的存储桶,然后在每个时间戳中,我需要对每种类型的书籍进行求和,当然还有书籍的总数。
例如,我的数据如下所示
[
{
"books":[
{
"id":0,
"count":10
},
{
"id":1,
"count":11
},
{
"id":2,
"count":7
},
{
"id":3,
"count":9
},
{
"id":4,
"count":16
}
],
"timestamp":1613693700000,
"total":53
},
{
"books":[
{
"id":0,
"count":0
},
{
"id":1,
"count":4
},
{
"id":2,
"count":9
},
{
"id":3,
"count":10
},
{
"id":4,
"count":1
}
],
"timestamp":1613694600000,
"total":24
}
]
我需要如下输出:
[
{
"timestamp":1613693700000,
"total_count":77,
"data":[
{
"id":0,
"count":10
},
{
"id":1,
"count":15
},
{
"id":2,
"count":16
},
{
"id":3,
"count":19
},
{
"id":4,
"count":17
}
]
}
]
我尝试了下面的查询,现在我被嵌套查询困住了,以获取每个时间戳存储桶中每种书籍类型的总和。在这方面需要帮助。
{
"aggs": {
"count": {
"date_histogram": {
"field": "timestamp",
"interval": "15m"
},
"aggs": {
"total_count": {
"sum": {
"field": "total"
}
}
}
}
}
}
【问题讨论】:
-
您能检查一下您的样本索引数据吗?两个文档是否都只包含
"id":0"?并且要计算两个文档中每个id对应的计数总和? -
抱歉错字,已在问题中修复。是的,我想计算每个id对应的count总和,可能有n个这样的文档
标签: elasticsearch nested date-histogram