在弹性搜索中聚合数组的数组 [英] Aggregating array of values in elasticsearch

查看:144
本文介绍了在弹性搜索中聚合数组的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要汇总一个数组如下



两个文件示例:

 <$ 
_index:log,
_type:旅行,
_id:tnQsGy4lS0K6uT3Hwzzo-g,
_score :1,
_source:{
state:saopaulo,
date:2014-10-30T17,
traveler:patrick ,
注册表:123123,
cities:{
saopaulo:1,
riodejaneiro:2,
total :$ 2
},
reason:[
Entrega de encomenda
],
from:[
CompraRapida
]
}
},
{
_index:log,
_type:旅行,
_id :tnQsGy4lS0K6uT3Hwzzo-g,
_score:1,
_source:{
state:saopaulo,
date:2014-10 -31T17,
旅行者:patrick,
注册表:123123,
city:{
saopaulo:1,
curitiba:1,
total:2
},
reason b $ bEntrega de encomenda
],
from:[
CompraRapida
]
}
},

我想聚合城市数组,以查找所有城市 旅行者已经去了。我想要这样的东西:

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

c> total 是城市的长度数组减1。我尝试了术语聚合和总和,但不能输出所需的输出



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

解决方案

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



示例文档:

  cities:[
{
name:saopaulo
visit_count :2,

},
{
name:riodejaneiro
visit_count:1,

}
]

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

 mappings:{
< type_name>:{
properties:{
cities:{
type:nested,
properties:{
city:{
type:string
},
count:{
type:integer
},
value:{
type:long
}

},
date:{
type:date,
format:dateOptionalTime
},
注册表:{
type:string
},
state:{
type:string
},
traveler:{
type:string
}
}
}
}
/ pre>

之后您可以使用嵌套聚合以获得城市计数p呃用户。
查询将在以下行上显示:

  {
查询:{
match:{
traveler:patrick
}
},
aggregate:{
city_travelled:{
nested:{
path:cities
},
aggs:{
citycount:{
cardinality:{
field:cities.city
}
}
}
}
}
}


I need to aggregate an array as follows

Two document examples:

{
    "_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"
        ]
    }
},

I want to aggregate the cities array, to find out all the cities the traveler has gone to. I want something like this:

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

Where the total is the length of the cities array minus 1. I tried the terms aggregation and the sum, but couldn't output the desired output.

Changes in the document structure can be made, so if anything like that would help me, I'd be pleased to know.

解决方案

in the document posted above "cities" is not a json array , it is a json object. If changing the document structure is a possibility I would change cities in the document to be an array of object

example document:

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

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

   }
]

You would then need to set cities to be of type nested in the index mapping

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

After which you could use nested aggregation to get the city count per user. The query would look something on these lines :

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

这篇关于在弹性搜索中聚合数组的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆