# Reverse nested Aggregation

一个特殊的单桶聚合，可以从嵌套文档中聚合父文档。实际上，这种聚合可以从嵌套的块结构中跳出来，并链接到其他嵌套的结构或根文档.这允许嵌套不是嵌套对象的一部分的其他聚合在嵌套聚合中。

reverse\_nested 聚合必须定义在nested之中

## **Options** <a href="#reversenestedaggregation-options" id="reversenestedaggregation-options"></a>

path － 定义了哪些嵌套的应该被连接起来对象字段， 默认为空，这意味着它将返回到 root / main档级别，该路径不能包含对嵌套对象字段的引用，该对象字段位于nested聚合的嵌套结构之外，reverse\_nested里面

例如，假设我们有一个具有问题和评论的票证系统的索引。 这些意见内嵌在问题文件中作为嵌套文件。 映射可能看起来像

```
{
    ...

    "issue" : {
        "properties" : {
            "tags" : { "type" : "text" },
            "comments" : {  ＃1
                "type" : "nested",
                "properties" : {
                    "username" : { "type" : "keyword" },
                    "comment" : { "type" : "text" }
                }
            }
        }
    }
}
```

＃1 注释是在问题对象下保存嵌套文档的数组。

下面的聚合将返回已注释的顶级评论者的用户名，以及用户评论过的问题的顶部标签

```
{
  "query": {
    "match": {
      "name": "led tv"
    }
  },
  "aggs": {
    "comments": {
      "nested": {
        "path": "comments"
      },
      "aggs": {
        "top_usernames": {
          "terms": {
            "field": "comments.username"
          },
          "aggs": {
            "comment_to_issue": {
              "reverse_nested": {},  ＃1
              "aggs": {
                "top_tags_per_comment": {
                  "terms": {
                    "field": "tags"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
```

如上所述，将reverse\_nested聚合放入嵌套聚合，因为这是dsl中唯一可以使用reverse\_nested聚合的位置。 其唯一的目的是加入到嵌套结构中较高的父文档。

＃1 因为没有定义任何路径，所以reverse\_nested聚合返回到根/主文档级别。 通过路径选项，如果在映射中定义了多个分层的嵌套对象类型，则reverse\_nested聚合可以返回到不同的级别

可能返回的代码片：

```
{
  "aggregations": {
    "comments": {
      "top_usernames": {
        "buckets": [
          {
            "key": "username_1",
            "doc_count": 12,
            "comment_to_issue": {
              "top_tags_per_comment": {
                "buckets": [
                  {
                    "key": "tag1",
                    "doc_count": 9
                  },
                  ...
                ]
              }
            }
          },
          ...
        ]
      }
    }
  }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/362tong-ju540828-bucketaggregations/reverse-nested-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
