parent-aggregation.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. [[search-aggregations-bucket-parent-aggregation]]
  2. === Parent Aggregation
  3. A special single bucket aggregation that selects parent documents that have the specified type, as defined in a <<parent-join,`join` field>>.
  4. This aggregation has a single option:
  5. * `type` - The child type that should be selected.
  6. For example, let's say we have an index of questions and answers. The answer type has the following `join` field in the mapping:
  7. [source,console,id=parent-aggregation-example]
  8. --------------------------------------------------
  9. PUT parent_example
  10. {
  11. "mappings": {
  12. "properties": {
  13. "join": {
  14. "type": "join",
  15. "relations": {
  16. "question": "answer"
  17. }
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. The `question` document contain a tag field and the `answer` documents contain an owner field. With the `parent`
  24. aggregation the owner buckets can be mapped to the tag buckets in a single request even though the two fields exist in
  25. two different kinds of documents.
  26. An example of a question document:
  27. [source,console]
  28. --------------------------------------------------
  29. PUT parent_example/_doc/1
  30. {
  31. "join": {
  32. "name": "question"
  33. },
  34. "body": "<p>I have Windows 2003 server and i bought a new Windows 2008 server...",
  35. "title": "Whats the best way to file transfer my site from server to a newer one?",
  36. "tags": [
  37. "windows-server-2003",
  38. "windows-server-2008",
  39. "file-transfer"
  40. ]
  41. }
  42. --------------------------------------------------
  43. // TEST[continued]
  44. Examples of `answer` documents:
  45. [source,console]
  46. --------------------------------------------------
  47. PUT parent_example/_doc/2?routing=1
  48. {
  49. "join": {
  50. "name": "answer",
  51. "parent": "1"
  52. },
  53. "owner": {
  54. "location": "Norfolk, United Kingdom",
  55. "display_name": "Sam",
  56. "id": 48
  57. },
  58. "body": "<p>Unfortunately you're pretty much limited to FTP...",
  59. "creation_date": "2009-05-04T13:45:37.030"
  60. }
  61. PUT parent_example/_doc/3?routing=1&refresh
  62. {
  63. "join": {
  64. "name": "answer",
  65. "parent": "1"
  66. },
  67. "owner": {
  68. "location": "Norfolk, United Kingdom",
  69. "display_name": "Troll",
  70. "id": 49
  71. },
  72. "body": "<p>Use Linux...",
  73. "creation_date": "2009-05-05T13:45:37.030"
  74. }
  75. --------------------------------------------------
  76. // TEST[continued]
  77. The following request can be built that connects the two together:
  78. [source,console]
  79. --------------------------------------------------
  80. POST parent_example/_search?size=0
  81. {
  82. "aggs": {
  83. "top-names": {
  84. "terms": {
  85. "field": "owner.display_name.keyword",
  86. "size": 10
  87. },
  88. "aggs": {
  89. "to-questions": {
  90. "parent": {
  91. "type" : "answer" <1>
  92. },
  93. "aggs": {
  94. "top-tags": {
  95. "terms": {
  96. "field": "tags.keyword",
  97. "size": 10
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. }
  105. }
  106. --------------------------------------------------
  107. // TEST[continued]
  108. <1> The `type` points to type / mapping with the name `answer`.
  109. The above example returns the top answer owners and per owner the top question tags.
  110. Possible response:
  111. [source,console-result]
  112. --------------------------------------------------
  113. {
  114. "took": 9,
  115. "timed_out": false,
  116. "_shards": {
  117. "total": 1,
  118. "successful": 1,
  119. "skipped": 0,
  120. "failed": 0
  121. },
  122. "hits": {
  123. "total" : {
  124. "value": 3,
  125. "relation": "eq"
  126. },
  127. "max_score": null,
  128. "hits": []
  129. },
  130. "aggregations": {
  131. "top-names": {
  132. "doc_count_error_upper_bound": 0,
  133. "sum_other_doc_count": 0,
  134. "buckets": [
  135. {
  136. "key": "Sam",
  137. "doc_count": 1, <1>
  138. "to-questions": {
  139. "doc_count": 1, <2>
  140. "top-tags": {
  141. "doc_count_error_upper_bound": 0,
  142. "sum_other_doc_count": 0,
  143. "buckets": [
  144. {
  145. "key": "file-transfer",
  146. "doc_count": 1
  147. },
  148. {
  149. "key": "windows-server-2003",
  150. "doc_count": 1
  151. },
  152. {
  153. "key": "windows-server-2008",
  154. "doc_count": 1
  155. }
  156. ]
  157. }
  158. }
  159. },
  160. {
  161. "key": "Troll",
  162. "doc_count": 1,
  163. "to-questions": {
  164. "doc_count": 1,
  165. "top-tags": {
  166. "doc_count_error_upper_bound": 0,
  167. "sum_other_doc_count": 0,
  168. "buckets": [
  169. {
  170. "key": "file-transfer",
  171. "doc_count": 1
  172. },
  173. {
  174. "key": "windows-server-2003",
  175. "doc_count": 1
  176. },
  177. {
  178. "key": "windows-server-2008",
  179. "doc_count": 1
  180. }
  181. ]
  182. }
  183. }
  184. }
  185. ]
  186. }
  187. }
  188. }
  189. --------------------------------------------------
  190. // TESTRESPONSE[s/"took": 9/"took": $body.took/]
  191. <1> The number of answer documents with the tag `Sam`, `Troll`, etc.
  192. <2> The number of question documents that are related to answer documents with the tag `Sam`, `Troll`, etc.