原英文版地址: https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-reverse-nested-aggregation.html, 原文档版权归 www.elastic.co 所有
本地英文版地址: ../en/search-aggregations-bucket-reverse-nested-aggregation.html
本地英文版地址: ../en/search-aggregations-bucket-reverse-nested-aggregation.html
重要: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 当前版本文档。
一个特殊的单桶聚合,支持从嵌套文档聚合父文档。 这种聚合可以有效地打破嵌套块结构,并链接到其他嵌套结构或根文档,这允许在嵌套聚合中嵌套不属于嵌套对象的其他聚合。
reverse_nested聚合必须定义在nested聚合内部。
选项:
-
path- 定义应该将哪个嵌套对象字段联接回去。 默认值为空,这意味着它联接回根/主文档级别。 该路径(path)不能包含对嵌套对象字段的引用,该嵌套对象字段不在reverse_nested所在的nested聚合的嵌套结构中。
例如,假设我们有一个包含问题(issue)和评论(comment)的票据系统的索引。 评论作为嵌套文档内联到问题文档中。 映射看起来可能像这样:
PUT /issues
{
"mappings": {
"properties" : {
"tags" : { "type" : "keyword" },
"comments" : {
"type" : "nested",
"properties" : {
"username" : { "type" : "keyword" },
"comment" : { "type" : "text" }
}
}
}
}
}
下面的聚合将返回已发表评论的前几名评论者的用户名,以及这几个评论者各自对用户已发表评论的问题的前几个标签:
GET /issues/_search
{
"query": {
"match_all": {}
},
"aggs": {
"comments": {
"nested": {
"path": "comments"
},
"aggs": {
"top_usernames": {
"terms": {
"field": "comments.username"
},
"aggs": {
"comment_to_issue": {
"reverse_nested": {},
"aggs": {
"top_tags_per_comment": {
"terms": {
"field": "tags"
}
}
}
}
}
}
}
}
}
}
正如你在上面看到的,reverse_nested聚合被放入nested聚合中,因为这是 DSL 中唯一可以使用reverse_nested聚合的地方。
它的唯一目的是联接回嵌套结构中更高一级的父文档。
可能的响应片段:
{
"aggregations": {
"comments": {
"doc_count": 1,
"top_usernames": {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets": [
{
"key": "username_1",
"doc_count": 1,
"comment_to_issue": {
"doc_count": 1,
"top_tags_per_comment": {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets": [
{
"key": "tag_1",
"doc_count": 1
}
...
]
}
}
}
...
]
}
}
}
}