【问题标题】:Aggregating array of values in elasticsearch在弹性搜索中聚合值数组
【发布时间】: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


【解决方案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"
               }
            }
         }
      }
   }
}

【讨论】:

  • 但是,我在第 30 天和第 31 天访问过它,这不会重复计算“saopaulo”吗?
  • @PatrickVillela 是的,我误解了我使用基数编辑答案的问题elasticsearch.org/guide/en/elasticsearch/reference/current/… 给出了所需不同城市的总数,但是必须减去 -1 才能占总数,可能是总数无论如何都不应该是“城市”对象/字段的一部分,并且应该是外部的单独字段
  • 你好。抱歉我的延误,但我不得不为另一个问题设计一个解决方案。我可能会在本周晚些时候回到这个。不过,我认为它可能会起作用......
猜你喜欢
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-10
  • 1970-01-01
  • 1970-01-01
  • 2015-06-06
相关资源
最近更新 更多