【发布时间】:2023-04-06 23:07:01
【问题描述】:
我需要如下聚合一个数组
两个文档示例:
{
"_index": "log",
"_type": "travels",
"_id": "tnQsGy4lS0K6uT3Hwzzo-g",
"_score": 1,
"_source": {
"state": "saopaulo",
"date": "2014-10-30T17",
"traveler": "patrick",
"registry": "123123",
"cities": {
"saopaulo": 1,
"riodejaneiro": 2,
"total": 2
},
"reasons": [
"Entrega de encomenda"
],
"from": [
"CompraRapida"
]
}
},
{
"_index": "log",
"_type": "travels",
"_id": "tnQsGy4lS0K6uT3Hwzzo-g",
"_score": 1,
"_source": {
"state": "saopaulo",
"date": "2014-10-31T17",
"traveler": "patrick",
"registry": "123123",
"cities": {
"saopaulo": 1,
"curitiba": 1,
"total": 2
},
"reasons": [
"Entrega de encomenda"
],
"from": [
"CompraRapida"
]
}
},
我想聚合cities 数组,以找出所有cities 和traveler 都去了。我想要这样的东西:
{
"traveler":{
"name":"patrick"
},
"cities":{
"saopaulo":2,
"riodejaneiro":2,
"curitiba":1,
"total":3
}
}
其中total 是cities 数组的长度减1。我尝试了术语聚合和总和,但无法输出所需的输出。
可以对文档结构进行更改,因此如果有任何类似的内容对我有帮助,我会很高兴知道。
【问题讨论】:
-
您是否有可以共享的该索引的映射?
标签: arrays json elasticsearch aggregation