在elasticsearch中聚合值数组

Pat*_*ela 5 arrays json aggregation 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"
        ]
    }
},
Run Code Online (Sandbox Code Playgroud)

我想聚集cities阵列,找出所有citiestraveler已经去了.我想要这样的东西:

{
    "traveler":{
        "name":"patrick"
    },
    "cities":{
        "saopaulo":2,
        "riodejaneiro":2,
        "curitiba":1,
        "total":3
    }
}
Run Code Online (Sandbox Code Playgroud)

其中数组total的长度cities减1.我尝试了聚合和总和,但无法输出所需的输出.

可以对文档结构进行更改,因此如果有任何相关内容可以帮助我,我很高兴知道.

kee*_*ety 14

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

示例文件:

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

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

   }
]
Run Code Online (Sandbox Code Playgroud)

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

   "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"
               }
            }
         }
      }
Run Code Online (Sandbox Code Playgroud)

之后,您可以使用嵌套聚合来获取每个用户的城市数量.查询将在这些行上看起来像:

{
   "query": {
      "match": {
         "traveler": "patrick"
      }
   },
   "aggregations": {
      "city_travelled": {
         "nested": {
            "path": "cities"
         },
         "aggs": {
            "citycount": {
               "cardinality": {
                  "field": "cities.city"
               }
            }
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)