索引文档如下:
{
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
and 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
),但是有没有更干净、更简单的方法来完成任务?