parent-aggregation.asciidoc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. [[search-aggregations-bucket-parent-aggregation]]
  2. === Parent Aggregation
  3. A special single bucket aggregation that selects parent 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 parent_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 `parent`
  25. aggregation the owner buckets can be mapped to the tag 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 parent_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 parent_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 parent_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 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. // CONSOLE
  111. // TEST[continued]
  112. <1> The `type` points to type / mapping with the name `answer`.
  113. The above example returns the top answer owners and per owner the top question tags.
  114. Possible response:
  115. [source,js]
  116. --------------------------------------------------
  117. {
  118. "took": 9,
  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-names": {
  136. "doc_count_error_upper_bound": 0,
  137. "sum_other_doc_count": 0,
  138. "buckets": [
  139. {
  140. "key": "Sam",
  141. "doc_count": 1, <1>
  142. "to-questions": {
  143. "doc_count": 1, <2>
  144. "top-tags": {
  145. "doc_count_error_upper_bound": 0,
  146. "sum_other_doc_count": 0,
  147. "buckets": [
  148. {
  149. "key": "file-transfer",
  150. "doc_count": 1
  151. },
  152. {
  153. "key": "windows-server-2003",
  154. "doc_count": 1
  155. },
  156. {
  157. "key": "windows-server-2008",
  158. "doc_count": 1
  159. }
  160. ]
  161. }
  162. }
  163. },
  164. {
  165. "key": "Troll",
  166. "doc_count": 1,
  167. "to-questions": {
  168. "doc_count": 1,
  169. "top-tags": {
  170. "doc_count_error_upper_bound": 0,
  171. "sum_other_doc_count": 0,
  172. "buckets": [
  173. {
  174. "key": "file-transfer",
  175. "doc_count": 1
  176. },
  177. {
  178. "key": "windows-server-2003",
  179. "doc_count": 1
  180. },
  181. {
  182. "key": "windows-server-2008",
  183. "doc_count": 1
  184. }
  185. ]
  186. }
  187. }
  188. }
  189. ]
  190. }
  191. }
  192. }
  193. --------------------------------------------------
  194. // TESTRESPONSE[s/"took": 9/"took": $body.took/]
  195. <1> The number of answer documents with the tag `Sam`, `Troll`, etc.
  196. <2> The number of question documents that are related to answer documents with the tag `Sam`, `Troll`, etc.