【发布时间】:2016-01-22 23:09:34
【问题描述】:
索引文档如下:
{
id: 1,
title: 'Blah',
...
platform: {id: 84, url: 'http://facebook.com', title: 'Facebook'}
...
}
我想要的是按平台计算和输出统计信息。
对于计数,我可以使用以 platform.id 为字段的术语聚合来计数:
aggs: {
platforms: {
terms: {field: 'platform.id'}
}
}
通过这种方式,我收到的统计信息是一个看起来像 {key: 8, doc_count: 162511} 的多个存储桶,正如预期的那样。
现在,我能否以某种方式将 platform.name 和 platform.url 添加到这些存储桶中(以获得漂亮的统计数据输出)?我带来的最好的看起来像:
aggs: {
platforms: {
terms: {field: 'platform.id'},
aggs: {
name: {terms: {field: 'platform.name'}},
url: {terms: {field: 'platform.url'}}
}
}
}
事实上,这很有效,并且在每个存储桶中返回了相当复杂的结构:
{key: 7,
doc_count: 528568,
url:
{doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [{key: "http://facebook.com", doc_count: 528568}]},
name:
{doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets: [{key: "Facebook", doc_count: 528568}]}},
当然,可以从这个结构中提取平台的名称和url(如bucket.url.buckets.first.key),但是有没有更简洁的方法来完成这项任务?
【问题讨论】:
-
您找到解决问题的方法了吗?我现在也面临同样的情况:(
-
我把它贴在下面作为我自己的“接受”答案:)
标签: elasticsearch