reverse-nested-aggregation.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. [[search-aggregations-bucket-reverse-nested-aggregation]]
  2. === Reverse nested Aggregation
  3. A special single bucket aggregation that enables aggregating on parent docs from nested documents. Effectively this
  4. aggregation can break out of the nested block structure and link to other nested structures or the root document,
  5. which allows nesting other aggregations that aren't part of the nested object in a nested aggregation.
  6. The `reverse_nested` aggregation must be defined inside a `nested` aggregation.
  7. .Options:
  8. * `path` - Which defines to what nested object field should be joined back. The default is empty,
  9. which means that it joins back to the root / main document level. The path cannot contain a reference to
  10. a nested object field that falls outside the `nested` aggregation's nested structure a `reverse_nested` is in.
  11. For example, lets say we have an index for a ticket system with issues and comments. The comments are inlined into
  12. the issue documents as nested documents. The mapping could look like:
  13. [source,js]
  14. --------------------------------------------------
  15. PUT /issues
  16. {
  17. "mappings": {
  18. "issue" : {
  19. "properties" : {
  20. "tags" : { "type" : "keyword" },
  21. "comments" : { <1>
  22. "type" : "nested",
  23. "properties" : {
  24. "username" : { "type" : "keyword" },
  25. "comment" : { "type" : "text" }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. --------------------------------------------------
  33. // CONSOLE
  34. <1> The `comments` is an array that holds nested documents under the `issue` object.
  35. The following aggregations will return the top commenters' username that have commented and per top commenter the top
  36. tags of the issues the user has commented on:
  37. //////////////////////////
  38. [source,js]
  39. --------------------------------------------------
  40. POST /issues/issue/0?refresh
  41. {"tags": ["tag_1"], "comments": [{"username": "username_1"}]}
  42. --------------------------------------------------
  43. // CONSOLE
  44. // TEST[continued]
  45. //////////////////////////
  46. [source,js]
  47. --------------------------------------------------
  48. GET /issues/_search
  49. {
  50. "query": {
  51. "match_all": {}
  52. },
  53. "aggs": {
  54. "comments": {
  55. "nested": {
  56. "path": "comments"
  57. },
  58. "aggs": {
  59. "top_usernames": {
  60. "terms": {
  61. "field": "comments.username"
  62. },
  63. "aggs": {
  64. "comment_to_issue": {
  65. "reverse_nested": {}, <1>
  66. "aggs": {
  67. "top_tags_per_comment": {
  68. "terms": {
  69. "field": "tags"
  70. }
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. --------------------------------------------------
  81. // CONSOLE
  82. // TEST[continued]
  83. // TEST[s/_search/_search\?filter_path=aggregations/]
  84. As you can see above, the `reverse_nested` aggregation is put in to a `nested` aggregation as this is the only place
  85. in the dsl where the `reverse_nested` aggregation can be used. Its sole purpose is to join back to a parent doc higher
  86. up in the nested structure.
  87. <1> A `reverse_nested` aggregation that joins back to the root / main document level, because no `path` has been defined.
  88. Via the `path` option the `reverse_nested` aggregation can join back to a different level, if multiple layered nested
  89. object types have been defined in the mapping
  90. Possible response snippet:
  91. [source,js]
  92. --------------------------------------------------
  93. {
  94. "aggregations": {
  95. "comments": {
  96. "doc_count": 1,
  97. "top_usernames": {
  98. "doc_count_error_upper_bound" : 0,
  99. "sum_other_doc_count" : 0,
  100. "buckets": [
  101. {
  102. "key": "username_1",
  103. "doc_count": 1,
  104. "comment_to_issue": {
  105. "doc_count": 1,
  106. "top_tags_per_comment": {
  107. "doc_count_error_upper_bound" : 0,
  108. "sum_other_doc_count" : 0,
  109. "buckets": [
  110. {
  111. "key": "tag_1",
  112. "doc_count": 1
  113. }
  114. ...
  115. ]
  116. }
  117. }
  118. }
  119. ...
  120. ]
  121. }
  122. }
  123. }
  124. }
  125. --------------------------------------------------
  126. // TESTRESPONSE[s/\.\.\.//]