如何按数组列表中的重复字段进行分组:ElasticSearch

时间:2017-10-23 14:01:35

标签: elasticsearch

我在Elasticsearch中遇到嵌套聚合问题。我有嵌套字段的映射:

"Topics":{"type":"nested","properties":{ 
    "CategoryLev1":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}, 
    "CategoryLev2":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}} }}

索引文件后:

"Topics": [
                  {
             "CategoryRelevancy": "1.0",
                     "CategoryLev2": "Money",
                     "CategoryLev1": "Sales"
                  },
                  {
                     "CategoryRelevancy": "2.0",
                     "CategoryLev2": "Money",
                     "CategoryLev1": "Sales"
                  },
                  {
             "CategoryRelevancy": "1.0",
                     "CategoryLev2": "Electrical",
                     "CategoryLev1": "Product"
                  }
               ]


    "Topics": [
                  {
             "CategoryRelevancy": "1.0",
                     "CategoryLev2": "Money",
                     "CategoryLev1": "Sales"
                  },
         {
           "CategoryRelevancy": "2.0",
                     "CategoryLev2": "Methods",
                     "CategoryLev1": "Sales"
                  },
                  {
            "CategoryRelevancy": "1.0",
                     "CategoryLev2": "Engine",
                     "CategoryLev1": "Product"
                  }
               ]

如您所见,在我的嵌套数组中,我有两个主题,它们具有Duplicate键和Value字段然后我进行了这样的查询:

{
   "size": 10,

   "aggregations": {
      "resellers": {
         "nested": {
            "path": "Topics"
         },
         "aggregations": {
            "topicGroup": {
               "terms": {
                  "field": "Topics.CategoryLev1.keyword",
                  "size": 10
               },
               "aggregations": {
                  "Subtopic": {
                     "terms": {
                        "field": "Topics.CategoryLev2.keyword"
                     }
                  }
               }
            }
         }
      }
   }
}

然后我得到关于具有主题类别

的组的结果
{
   "hits": {
      "total": 2,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "resellers": {
         "doc_count": 6,
         "topicGroup": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "Sales",
                  "doc_count": 3,
                  "Subtopic": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "Money",
                           "doc_count": 3
                        },
                        {
                           "key": "Method",
                           "doc_count": 1
                        }
                     ]
                  }
               },
               {
                  "key": "Product",
                  "doc_count": 2,
                  "Subtopic": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "Electrical",
                           "doc_count": 1
                        },
                        {
                           "key": "Engine",
                           "doc_count": 1
                        }
                     ]
                  }
               }
            ]
         }
      }
   }
}             

但我想结果像这样

"buckets": [
               {
                  "key": "Sales",
                  "doc_count": 2,
                  "Subtopic": {
                     "buckets": [
                        {
                           "key": "Money",
                           "doc_count": 2
                        },
                        {
                           "key": "Method",
                           "doc_count": 1
                        }
                     ]
                  }
               },
               {
                  "key": "Product",
                  "doc_count": 2,
                  "Subtopic": {
                    "buckets": [
                        {
                           "key": "Electrical",
                           "doc_count": 1
                        },
                        {
                           "key": "Engine",
                           "doc_count": 1
                        }]
            }
}]

提前致谢:)

0 个答案:

没有答案