parent-aggregation.asciidoc 5.2 KB

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