父级聚合
parent
聚合是一个桶聚合,它根据索引中定义的父子关系创建一个包含父文档的单个桶。此聚合使您能够对具有相同匹配子文档的父文档执行分析,从而实现强大的层次化数据分析。
parent
聚合与 join
字段类型一起使用,该字段类型在同一索引中的文档内建立父子关系。
parent
聚合识别具有匹配子文档的父文档,而 children
聚合识别匹配特定子关系的子文档。这两种聚合都将子关系名称作为输入。
参数
parent
聚合接受以下参数:
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
type | 必需 | 字符串 | 来自 join 字段的子类型名称。 |
示例
以下示例构建了一个包含三名员工的小型公司数据库。每个员工记录都与父部门记录具有子 join
关系。
首先,创建一个包含 join
字段的 company
索引,该字段将部门(父级)映射到员工(子级):
PUT /company
{
"mappings": {
"properties": {
"join_field": {
"type": "join",
"relations": {
"department": "employee"
}
},
"department_name": {
"type": "keyword"
},
"employee_name": {
"type": "keyword"
},
"salary": {
"type": "double"
},
"hire_date": {
"type": "date"
}
}
}
}
接下来,使用三个部门和三名员工填充数据。父子分配如下表所示。
部门(父级) | 员工(子级) |
---|---|
会计 | Abel Anderson 、Betty Billings |
工程 | Carl Carter |
人力资源部 | 无 |
routing
参数确保父文档和子文档存储在同一个分片上,这是 OpenSearch 中父子关系正常工作的必要条件。
POST _bulk?routing=1
{ "create": { "_index": "company", "_id": "1" } }
{ "type": "department", "department_name": "Accounting", "join_field": "department" }
{ "create": { "_index": "company", "_id": "2" } }
{ "type": "department", "department_name": "Engineering", "join_field": "department" }
{ "create": { "_index": "company", "_id": "3" } }
{ "type": "department", "department_name": "HR", "join_field": "department" }
{ "create": { "_index": "company", "_id": "4" } }
{ "type": "employee", "employee_name": "Abel Anderson", "salary": 120000, "hire_date": "2024-04-04", "join_field": { "name": "employee", "parent": "1" } }
{ "create": { "_index": "company", "_id": "5" } }
{ "type": "employee", "employee_name": "Betty Billings", "salary": 140000, "hire_date": "2023-05-05", "join_field": { "name": "employee", "parent": "1" } }
{ "create": { "_index": "company", "_id": "6" } }
{ "type": "employee", "employee_name": "Carl Carter", "salary": 140000, "hire_date": "2020-06-06", "join_field": { "name": "employee", "parent": "2" } }
最后,运行所有与一名或多名员工具有父子关系的部门的聚合。
GET /company/_search
{
"size": 0,
"aggs": {
"all_departments": {
"parent": {
"type": "employee"
},
"aggs": {
"departments": {
"terms": {
"field": "department_name"
}
}
}
}
}
}
示例响应
all_departments
父聚合返回所有包含员工子文档的部门。请注意,人力资源部门未在其中显示。
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 6,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"aggregations": {
"all_departments": {
"doc_count": 2,
"departments": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Accounting",
"doc_count": 1
},
{
"key": "Engineering",
"doc_count": 1
}
]
}
}
}
}