children-aggregation.asciidoc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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,console]
  8. --------------------------------------------------
  9. PUT child_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 `children`
  24. aggregation the tag buckets can be mapped to the owner 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 child_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 child_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 child_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 child_example/_search?size=0
  81. {
  82. "aggs": {
  83. "top-tags": {
  84. "terms": {
  85. "field": "tags.keyword",
  86. "size": 10
  87. },
  88. "aggs": {
  89. "to-answers": {
  90. "children": {
  91. "type" : "answer" <1>
  92. },
  93. "aggs": {
  94. "top-names": {
  95. "terms": {
  96. "field": "owner.display_name.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 question tags and per tag the top answer owners.
  110. Possible response:
  111. [source,js]
  112. --------------------------------------------------
  113. {
  114. "took": 25,
  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-tags": {
  132. "doc_count_error_upper_bound": 0,
  133. "sum_other_doc_count": 0,
  134. "buckets": [
  135. {
  136. "key": "file-transfer",
  137. "doc_count": 1, <1>
  138. "to-answers": {
  139. "doc_count": 2, <2>
  140. "top-names": {
  141. "doc_count_error_upper_bound": 0,
  142. "sum_other_doc_count": 0,
  143. "buckets": [
  144. {
  145. "key": "Sam",
  146. "doc_count": 1
  147. },
  148. {
  149. "key": "Troll",
  150. "doc_count": 1
  151. }
  152. ]
  153. }
  154. }
  155. },
  156. {
  157. "key": "windows-server-2003",
  158. "doc_count": 1, <1>
  159. "to-answers": {
  160. "doc_count": 2, <2>
  161. "top-names": {
  162. "doc_count_error_upper_bound": 0,
  163. "sum_other_doc_count": 0,
  164. "buckets": [
  165. {
  166. "key": "Sam",
  167. "doc_count": 1
  168. },
  169. {
  170. "key": "Troll",
  171. "doc_count": 1
  172. }
  173. ]
  174. }
  175. }
  176. },
  177. {
  178. "key": "windows-server-2008",
  179. "doc_count": 1, <1>
  180. "to-answers": {
  181. "doc_count": 2, <2>
  182. "top-names": {
  183. "doc_count_error_upper_bound": 0,
  184. "sum_other_doc_count": 0,
  185. "buckets": [
  186. {
  187. "key": "Sam",
  188. "doc_count": 1
  189. },
  190. {
  191. "key": "Troll",
  192. "doc_count": 1
  193. }
  194. ]
  195. }
  196. }
  197. }
  198. ]
  199. }
  200. }
  201. }
  202. --------------------------------------------------
  203. // TESTRESPONSE[s/"took": 25/"took": $body.took/]
  204. <1> The number of question documents with the tag `file-transfer`, `windows-server-2003`, etc.
  205. <2> The number of answer documents that are related to question documents with the tag `file-transfer`, `windows-server-2003`, etc.