在Elasticsearch中汇总值数组

我需要如下汇总一个数组

两个文档示例:

{

"_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阵列,找出所有citiestraveler已经去了。我想要这样的东西:

{

"traveler":{

"name":"patrick"

},

"cities":{

"saopaulo":2,

"riodejaneiro":2,

"curitiba":1,

"total":3

}

}

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

可以对文档结构进行更改,因此,如果这样对我有所帮助,我将很高兴知道。

回答:

在“ ” 上方发布的文档中,它不是json数组,而是json对象。如果可以更改文档结构,则可以将文档中的城市更改为对象数组

 cities : [

{

"name" :"saopaulo"

"visit_count" :"2",

},

{

"name" :"riodejaneiro"

"visit_count" :"1",

}

]

然后,您需要将城市设置为嵌套在索引映射中的类型

   "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"

}

}

}

}

之后,您可以使用嵌套聚合来获取每个用户的城市计数。查询将在以下几行中显示:

{

"query": {

"match": {

"traveler": "patrick"

}

},

"aggregations": {

"city_travelled": {

"nested": {

"path": "cities"

},

"aggs": {

"citycount": {

"cardinality": {

"field": "cities.city"

}

}

}

}

}

}

以上是 在Elasticsearch中汇总值数组 的全部内容, 来源链接: utcz.com/qa/415163.html

回到顶部