Children Aggregation
一种特殊的单桶聚合,可以将父文档类型上的桶聚合到子文档上。
此聚合依赖于映射中的_parent字段。 此聚合有一个选项:
type - 应该映射到父空间中的子类型的buckets(桶)
例如,假设我们有一个问题和答案的索引。 答案类型在映射中具有以下_parent字段
PUT child_example
{
"mappings": {
"answer" : {
"_parent" : {
"type" : "question"
}
}
}
}
问题类型的文档包含标签字段,答案类型的文档包含所有者字段。 使用子集合,即使两个字段存在于两种不同类型的文档中,标记桶也可以在单个请求中映射到所有者桶。
一个问题类型文档的例子
PUT child_example/question/1
{
"body": "<p>I have Windows 2003 server and i bought a new Windows 2008 server...",
"title": "Whats the best way to file transfer my site from server to a newer one?",
"tags": [
"windows-server-2003",
"windows-server-2008",
"file-transfer"
]
}
回答类型文件的例子:
PUT child_example/answer/1?parent=1&refresh
{
"owner": {
"location": "Norfolk, United Kingdom",
"display_name": "Sam",
"id": 48
},
"body": "<p>Unfortunately you're pretty much limited to FTP...",
"creation_date": "2009-05-04T13:45:37.030"
}
PUT child_example/answer/2?parent=1&refresh
{
"owner": {
"location": "Norfolk, United Kingdom",
"display_name": "Troll",
"id": 49
},
"body": "<p>Use Linux...",
"creation_date": "2009-05-05T13:45:37.030"
}
可以构建将两者连接在一起的以下请求:
POST child_example/_search?size=0
{
"aggs": {
"top-tags": {
"terms": {
"field": "tags.keyword",
"size": 10
},
"aggs": {
"to-answers": {
"children": {
"type" : "answer" #1
},
"aggs": {
"top-names": {
"terms": {
"field": "owner.display_name.keyword",
"size": 10
}
}
}
}
}
}
}
}
1 | type 指向使用名称为answer的 type / mapping |
以上示例返回顶级问题标签和每个标签的顶级答案所有者。
下面是可能的返回结果:
{
"timed_out": false,
"took": 25,
"_shards": { "total": 5, "successful": 5, "failed": 0 },
"hits": { "total": 3, "max_score": 0.0, hits: [] },
"aggregations": {
"top-tags": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "file-transfer",
"doc_count": 1, #1
"to-answers": {
"doc_count": 2, #2
"top-names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Sam",
"doc_count": 1
},
{
"key": "Troll",
"doc_count": 1
}
]
}
}
},
{
"key": "windows-server-2003",
"doc_count": 1, #3
"to-answers": {
"doc_count": 2, #4
"top-names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Sam",
"doc_count": 1
},
{
"key": "Troll",
"doc_count": 1
}
]
}
}
},
{
"key": "windows-server-2008",
"doc_count": 1, #5
"to-answers": {
"doc_count": 2, #6
"top-names": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Sam",
"doc_count": 1
},
{
"key": "Troll",
"doc_count": 1
}
]
}
}
}
]
}
}
}
#1 带有file-transfer,windows-server-2003等标签问题文档数量
#3
#5
#2 带有file-transfer
, windows-server-2003等标签的与问题文档相关的答复文档的数量
#4
#
Last updated