children-aggregation.asciidoc 5.4 KB

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