【发布时间】:2017-08-11 11:24:09
【问题描述】:
Elasticsearch 嵌套聚合允许您有效地按多个字段进行分组。但它返回的是为您分组的每个字段嵌套的存储桶。
我需要的是每个组组合的对象数组。
我的查询:
{
index : 'stats',
type : 'click',
size : 0,
body : {
aggs : {
publisher : {
terms : {
field : 'publisherData.id'
},
aggs : {
advertiser : {
terms : {
field : 'advertiserData.id'
},
aggs : {
country : {
terms : {
field : 'request.location.country.iso_code'
},
aggs : {
revenue : {
sum : {
field : 'revenueData.data.USD'
}
},
cost : {
sum : {
field : 'costData.data.USD'
}
}
}
}
}
}
}
}
}
}
}
结果,每个字段限制为一个条目。通常会有更多,因此所有嵌套字段的组合都必须映射到数组以在表格中显示。
{
"took": 562,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4812178,
"max_score": 0,
"hits": []
},
"aggregations": {
"publisher": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 3114671,
"buckets": [
{
"key": 4,
"doc_count": 1697507,
"advertiser": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 555390,
"buckets": [
{
"key": 5,
"doc_count": 1142117,
"country": {
"doc_count_error_upper_bound": 13807,
"sum_other_doc_count": 544585,
"buckets": [
{
"key": "us",
"doc_count": 424137,
"revenue": {
"value": 772282
},
"cost": {
"value": 53698.84903321415
}
}
]
}
}
]
}
}
]
}
}
}
我需要什么(通常这里会有多个对象,每个嵌套字段组合一个):
[{
publisher:4,
advertiser:5,
country:'us',
cost:53698.84903321415,
revenue:772282
}]
从上述嵌套结构中获得此结果的最佳方法是什么,甚至更好,如果可能的话,从 elasticsearch 本身获得。
非常感谢任何帮助。
【问题讨论】:
标签: javascript node.js elasticsearch elasticsearch-aggregation