gpt4 book ai didi

arrays - 在 Elasticsearch 中聚合值数组

转载 作者:行者123 更新时间:2023-11-29 02:48:53 24 4
gpt4 key购买 nike

我需要如下聚合一个数组

两个文档示例:

{
"_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数组,找出traveler去过的所有cities。我想要这样的东西:

{
"traveler":{
"name":"patrick"
},
"cities":{
"saopaulo":2,
"riodejaneiro":2,
"curitiba":1,
"total":3
}
}

其中 totalcities 数组的长度减 1。我尝试了术语聚合和总和,但无法输出所需的输出。

可以对文档结构进行更改,所以如果这样的事情对我有帮助,我很乐意知道。

最佳答案

在上面发布的文档中“cities”不是一个 json 数组,它是一个 json 对象。如果可以更改文档结构,我会将文档中的城市更改为对象数组

示例文档:

 cities : [
{
"name" :"saopaulo"
"visit_count" :"2",

},
{
"name" :"riodejaneiro"
"visit_count" :"1",

}
]

然后您需要将城市设置为 nested 类型在索引映射中

   "mappings": {
"<type_name>": {
"properties": {
"cities": {
"type": "nested",
"properties": {
"city": {
"type": "string"
},
"count": {
"type": "integer"
},
"value": {
"type": "long"
}
}
},
"date": {
"type": "date",
"format": "dateOptionalTime"
},
"registry": {
"type": "string"
},
"state": {
"type": "string"
},
"traveler": {
"type": "string"
}
}
}
}

之后你可以使用 nested aggregation获取每个用户的城市计数。查询看起来像这些行:

{
"query": {
"match": {
"traveler": "patrick"
}
},
"aggregations": {
"city_travelled": {
"nested": {
"path": "cities"
},
"aggs": {
"citycount": {
"cardinality": {
"field": "cities.city"
}
}
}
}
}
}

关于arrays - 在 Elasticsearch 中聚合值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26743204/

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