children-aggregation.asciidoc 5.3 KB

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