children-aggregation.asciidoc 5.4 KB

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