首页 > 解决方案 > 在结果中具有相同属性的对象数组上的 Elasticsearch 聚合

问题描述

我有以下文档结构。

 {"tagsData": [
      {
         "hashtag": "computer",
         "displayName": "Computer",
         "urlKeyword": "computer"
      },
      {
         "hashtag": "artificialIntelligence",
         "displayName": "Artificial Intelligence",
         "urlKeyword": "artificial-intelligence"
      }
  ]}

我的要求是在标签字段上应用聚合并获得如下结果

 {"aggregations": {
    "tags": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
            {
                "key": "computer",
                "displayName": "Computer",
                "urlKeyword": "computer"   
                "doc_count": 1
            },
            {
                "key": "artificialIntelligence",
                "displayName": "Artificial Intelligence",
                "urlKeyword": "artificial-intelligence"
                "doc_count": 1
            }]}
   }}

Elasticsearch 5.3 版请告诉我如何实现这一点?映射和查询应该是什么?

编辑

我正在尝试的映射。

{
  "test": {
    "properties" : {
      "tagsData" : {
        "type" : "object",
        "properties": {
          "hashtag" : {"type": "keyword", "index": "not_analyzed"},
          "displayName" : {"type": "keyword", "index": "not_analyzed"},
          "urlKeyword" : {"type": "keyword", "index": "not_analyzed"}
        }
      }
    }
  }
}

标签: elasticsearchelasticsearch-5

解决方案


推荐阅读