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

贡献者 : @于永超,ApacheCNApache中文网

Reverse nested Aggregation

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

reverse_nested 聚合必须定义在nested之中

Options

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

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

  1. {
  2. ...
  3. "issue" : {
  4. "properties" : {
  5. "tags" : { "type" : "text" },
  6. "comments" : { 1
  7. "type" : "nested",
  8. "properties" : {
  9. "username" : { "type" : "keyword" },
  10. "comment" : { "type" : "text" }
  11. }
  12. }
  13. }
  14. }
  15. }

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

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

  1. {
  2. "query": {
  3. "match": {
  4. "name": "led tv"
  5. }
  6. },
  7. "aggs": {
  8. "comments": {
  9. "nested": {
  10. "path": "comments"
  11. },
  12. "aggs": {
  13. "top_usernames": {
  14. "terms": {
  15. "field": "comments.username"
  16. },
  17. "aggs": {
  18. "comment_to_issue": {
  19. "reverse_nested": {}, 1
  20. "aggs": {
  21. "top_tags_per_comment": {
  22. "terms": {
  23. "field": "tags"
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }

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

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

可能返回的代码片:

  1. {
  2. "aggregations": {
  3. "comments": {
  4. "top_usernames": {
  5. "buckets": [
  6. {
  7. "key": "username_1",
  8. "doc_count": 12,
  9. "comment_to_issue": {
  10. "top_tags_per_comment": {
  11. "buckets": [
  12. {
  13. "key": "tag1",
  14. "doc_count": 9
  15. },
  16. ...
  17. ]
  18. }
  19. }
  20. },
  21. ...
  22. ]
  23. }
  24. }
  25. }
  26. }