多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# Reverse nested Aggregation 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html#search-aggregations-bucket-reverse-nested-aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html#search-aggregations-bucket-reverse-nested-aggregation) 译文链接 : [Reverse nested Aggregation](http://apache.wiki/display/Elasticsearch/Reverse+nested+Aggregation?src=contextnavpagetreemode) 贡献者 : @于永超,[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) ## Reverse nested Aggregation 一个特殊的单桶聚合,可以从嵌套文档中聚合父文档。实际上,这种聚合可以从嵌套的块结构中跳出来,并链接到其他嵌套的结构或根文档.这允许嵌套不是嵌套对象的一部分的其他聚合在嵌套聚合中。 reverse_nested 聚合必须定义在nested之中 ## **Options** 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 }, ... ] } } }, ... ] } } } } ```