Children Aggregation

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html

译文链接 : http://www.apache.wiki/display/Elasticsearch(修改该链接为 ApacheCN 对应的译文链接)

贡献者 : @于永超,ApacheCNApache中文网

一种特殊的单桶聚合,可以将父文档类型上的桶聚合到子文档上。

此聚合依赖于映射中的_parent字段。 此聚合有一个选项:

  • type - 应该映射到父空间中的子类型的buckets(桶)

例如,假设我们有一个问题和答案的索引。 答案类型在映射中具有以下_parent字段

  1. PUT child_example
  2. {
  3. "mappings": {
  4. "answer" : {
  5. "_parent" : {
  6. "type" : "question"
  7. }
  8. }
  9. }
  10. }

问题类型的文档包含标签字段,答案类型的文档包含所有者字段。 使用子集合,即使两个字段存在于两种不同类型的文档中,标记桶也可以在单个请求中映射到所有者桶。

一个问题类型文档的例子

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

回答类型文件的例子:

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

可以构建将两者连接在一起的以下请求:

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

#1 type 指向使用名称为answer的 type / mapping

以上示例返回顶级问题标签和每个标签的顶级答案所有者。

下面是可能的返回结果:

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

#1 带有file-transfer,windows-server-2003等标签问题文档数量

#3

#5

#2 带有file-transfer, windows-server-2003等标签的与问题文档相关的答复文档的数量

#4

#6