我认为您可以通过嵌套聚合中的术语聚合来获得所需的内容。
我设置了一个这样的简单索引:
PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"authors": {
"type": "nested",
"properties": {
"Country": {
"type": "string",
"index": "not_analyzed"
},
"LastName": {
"type": "string",
"index": "not_analyzed"
}
}
},
"title": {
"type": "string"
}
}
}
}
}
然后添加了几个文件:
PUT /test_index/doc/1
{
"title": "Some title",
"authors": [
{ "LastName": "Smith", "Country": "US"},
{ "LastName": "Smith", "Country": "UK"}
]
}
PUT /test_index/doc/2
{
"title": "another title",
"authors": [
{ "LastName": "Jones", "Country": "SA"},
{ "LastName": "Jones", "Country": "UK"}
]
}
然后运行这个查询:
POST /test_index/_search?search_type=count
{
"aggs": {
"authors": {
"nested": {
"path": "authors"
},
"aggs": {
"author_countries": {
"terms": {
"field": "authors.Country"
}
}
}
}
}
}
这似乎返回了你想要的:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0,
"hits": []
},
"aggregations": {
"authors": {
"doc_count": 4,
"author_countries": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "UK",
"doc_count": 2
},
{
"key": "SA",
"doc_count": 1
},
{
"key": "US",
"doc_count": 1
}
]
}
}
}
}
这是我用于测试的一些代码:
http://sense.qbox.io/gist/ccf7bd9d05f646507b3316e985dd6a50e905aed3