Parent Aggregation

A special single bucket aggregation that selects parent documents that have the specified type, as defined in a join field.

This aggregation has a single option:

  • type - The child type that should be selected.

For example, let’s say we have an index of questions and answers. The answer type has the following join field in the mapping:

  1. PUT parent_example
  2. {
  3. "mappings": {
  4. "properties": {
  5. "join": {
  6. "type": "join",
  7. "relations": {
  8. "question": "answer"
  9. }
  10. }
  11. }
  12. }
  13. }

The question document contain a tag field and the answer documents contain an owner field. With the parent aggregation the owner buckets can be mapped to the tag buckets in a single request even though the two fields exist in two different kinds of documents.

An example of a question document:

  1. PUT parent_example/_doc/1
  2. {
  3. "join": {
  4. "name": "question"
  5. },
  6. "body": "<p>I have Windows 2003 server and i bought a new Windows 2008 server...",
  7. "title": "Whats the best way to file transfer my site from server to a newer one?",
  8. "tags": [
  9. "windows-server-2003",
  10. "windows-server-2008",
  11. "file-transfer"
  12. ]
  13. }

Examples of answer documents:

  1. PUT parent_example/_doc/2?routing=1
  2. {
  3. "join": {
  4. "name": "answer",
  5. "parent": "1"
  6. },
  7. "owner": {
  8. "location": "Norfolk, United Kingdom",
  9. "display_name": "Sam",
  10. "id": 48
  11. },
  12. "body": "<p>Unfortunately you're pretty much limited to FTP...",
  13. "creation_date": "2009-05-04T13:45:37.030"
  14. }
  15. PUT parent_example/_doc/3?routing=1&refresh
  16. {
  17. "join": {
  18. "name": "answer",
  19. "parent": "1"
  20. },
  21. "owner": {
  22. "location": "Norfolk, United Kingdom",
  23. "display_name": "Troll",
  24. "id": 49
  25. },
  26. "body": "<p>Use Linux...",
  27. "creation_date": "2009-05-05T13:45:37.030"
  28. }

The following request can be built that connects the two together:

  1. POST parent_example/_search?size=0
  2. {
  3. "aggs": {
  4. "top-names": {
  5. "terms": {
  6. "field": "owner.display_name.keyword",
  7. "size": 10
  8. },
  9. "aggs": {
  10. "to-questions": {
  11. "parent": {
  12. "type" : "answer"
  13. },
  14. "aggs": {
  15. "top-tags": {
  16. "terms": {
  17. "field": "tags.keyword",
  18. "size": 10
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }

The type points to type / mapping with the name answer.

The above example returns the top answer owners and per owner the top question tags.

Possible response:

  1. {
  2. "took": 9,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total" : {
  12. "value": 3,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": []
  17. },
  18. "aggregations": {
  19. "top-names": {
  20. "doc_count_error_upper_bound": 0,
  21. "sum_other_doc_count": 0,
  22. "buckets": [
  23. {
  24. "key": "Sam",
  25. "doc_count": 1,
  26. "to-questions": {
  27. "doc_count": 1,
  28. "top-tags": {
  29. "doc_count_error_upper_bound": 0,
  30. "sum_other_doc_count": 0,
  31. "buckets": [
  32. {
  33. "key": "file-transfer",
  34. "doc_count": 1
  35. },
  36. {
  37. "key": "windows-server-2003",
  38. "doc_count": 1
  39. },
  40. {
  41. "key": "windows-server-2008",
  42. "doc_count": 1
  43. }
  44. ]
  45. }
  46. }
  47. },
  48. {
  49. "key": "Troll",
  50. "doc_count": 1,
  51. "to-questions": {
  52. "doc_count": 1,
  53. "top-tags": {
  54. "doc_count_error_upper_bound": 0,
  55. "sum_other_doc_count": 0,
  56. "buckets": [
  57. {
  58. "key": "file-transfer",
  59. "doc_count": 1
  60. },
  61. {
  62. "key": "windows-server-2003",
  63. "doc_count": 1
  64. },
  65. {
  66. "key": "windows-server-2008",
  67. "doc_count": 1
  68. }
  69. ]
  70. }
  71. }
  72. }
  73. ]
  74. }
  75. }
  76. }

The number of answer documents with the tag Sam, Troll, etc.

The number of question documents that are related to answer documents with the tag Sam, Troll, etc.