子ドキュメントの集約

特定のタイプを持つ子ドキュメントを選択する特別な単一バケット集約で、これは join フィールド で定義されています。

この集約には単一のオプションがあります:

  • type - 選択すべき子タイプ。

例えば、質問と回答のインデックスがあるとしましょう。回答タイプには、マッピング内に次の join フィールドがあります:

Python

  1. resp = client.indices.create(
  2. index="child_example",
  3. mappings={
  4. "properties": {
  5. "join": {
  6. "type": "join",
  7. "relations": {
  8. "question": "answer"
  9. }
  10. }
  11. }
  12. },
  13. )
  14. print(resp)

Ruby

  1. response = client.indices.create(
  2. index: 'child_example',
  3. body: {
  4. mappings: {
  5. properties: {
  6. join: {
  7. type: 'join',
  8. relations: {
  9. question: 'answer'
  10. }
  11. }
  12. }
  13. }
  14. }
  15. )
  16. puts response

Js

  1. const response = await client.indices.create({
  2. index: "child_example",
  3. mappings: {
  4. properties: {
  5. join: {
  6. type: "join",
  7. relations: {
  8. question: "answer",
  9. },
  10. },
  11. },
  12. },
  13. });
  14. console.log(response);

コンソール

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

question ドキュメントにはタグフィールドが含まれ、answer ドキュメントにはオーナーフィールドが含まれています。children 集約を使用すると、タグバケットをオーナーバケットに単一のリクエストでマッピングできますが、2つのフィールドは異なる種類のドキュメントに存在します。

質問ドキュメントの例:

Python

  1. resp = client.index(
  2. index="child_example",
  3. id="1",
  4. document={
  5. "join": {
  6. "name": "question"
  7. },
  8. "body": "I have Windows 2003 server and i bought a new Windows 2008 server...",
  9. "title": "Whats the best way to file transfer my site from server to a newer one?",
  10. "tags": [
  11. "windows-server-2003",
  12. "windows-server-2008",
  13. "file-transfer"
  14. ]
  15. },
  16. )
  17. print(resp)

Ruby

  1. response = client.index(
  2. index: 'child_example',
  3. id: 1,
  4. body: {
  5. join: {
  6. name: 'question'
  7. },
  8. body: 'I have Windows 2003 server and i bought a new Windows 2008 server...',
  9. title: 'Whats the best way to file transfer my site from server to a newer one?',
  10. tags: [
  11. 'windows-server-2003',
  12. 'windows-server-2008',
  13. 'file-transfer'
  14. ]
  15. }
  16. )
  17. puts response

Js

  1. const response = await client.index({
  2. index: "child_example",
  3. id: 1,
  4. document: {
  5. join: {
  6. name: "question",
  7. },
  8. body: "I have Windows 2003 server and i bought a new Windows 2008 server...",
  9. title:
  10. "Whats the best way to file transfer my site from server to a newer one?",
  11. tags: ["windows-server-2003", "windows-server-2008", "file-transfer"],
  12. },
  13. });
  14. console.log(response);

コンソール

  1. PUT child_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. }

answer ドキュメントの例:

Python

  1. resp = client.index(
  2. index="child_example",
  3. id="2",
  4. routing="1",
  5. document={
  6. "join": {
  7. "name": "answer",
  8. "parent": "1"
  9. },
  10. "owner": {
  11. "location": "Norfolk, United Kingdom",
  12. "display_name": "Sam",
  13. "id": 48
  14. },
  15. "body": "Unfortunately you're pretty much limited to FTP...",
  16. "creation_date": "2009-05-04T13:45:37.030"
  17. },
  18. )
  19. print(resp)
  20. resp1 = client.index(
  21. index="child_example",
  22. id="3",
  23. routing="1",
  24. refresh=True,
  25. document={
  26. "join": {
  27. "name": "answer",
  28. "parent": "1"
  29. },
  30. "owner": {
  31. "location": "Norfolk, United Kingdom",
  32. "display_name": "Troll",
  33. "id": 49
  34. },
  35. "body": "Use Linux...",
  36. "creation_date": "2009-05-05T13:45:37.030"
  37. },
  38. )
  39. print(resp1)

Ruby

  1. response = client.index(
  2. index: 'child_example',
  3. id: 2,
  4. routing: 1,
  5. body: {
  6. join: {
  7. name: 'answer',
  8. parent: '1'
  9. },
  10. owner: {
  11. location: 'Norfolk, United Kingdom',
  12. display_name: 'Sam',
  13. id: 48
  14. },
  15. body: "Unfortunately you're pretty much limited to FTP...",
  16. creation_date: '2009-05-04T13:45:37.030'
  17. }
  18. )
  19. puts response
  20. response = client.index(
  21. index: 'child_example',
  22. id: 3,
  23. routing: 1,
  24. refresh: true,
  25. body: {
  26. join: {
  27. name: 'answer',
  28. parent: '1'
  29. },
  30. owner: {
  31. location: 'Norfolk, United Kingdom',
  32. display_name: 'Troll',
  33. id: 49
  34. },
  35. body: 'Use Linux...',
  36. creation_date: '2009-05-05T13:45:37.030'
  37. }
  38. )
  39. puts response

Js

  1. const response = await client.index({
  2. index: "child_example",
  3. id: 2,
  4. routing: 1,
  5. document: {
  6. join: {
  7. name: "answer",
  8. parent: "1",
  9. },
  10. owner: {
  11. location: "Norfolk, United Kingdom",
  12. display_name: "Sam",
  13. id: 48,
  14. },
  15. body: "Unfortunately you're pretty much limited to FTP...",
  16. creation_date: "2009-05-04T13:45:37.030",
  17. },
  18. });
  19. console.log(response);
  20. const response1 = await client.index({
  21. index: "child_example",
  22. id: 3,
  23. routing: 1,
  24. refresh: "true",
  25. document: {
  26. join: {
  27. name: "answer",
  28. parent: "1",
  29. },
  30. owner: {
  31. location: "Norfolk, United Kingdom",
  32. display_name: "Troll",
  33. id: 49,
  34. },
  35. body: "Use Linux...",
  36. creation_date: "2009-05-05T13:45:37.030",
  37. },
  38. });
  39. console.log(response1);

コンソール

  1. PUT child_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 child_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. }

次のリクエストを構築して、2つを接続できます:

Python

  1. resp = client.search(
  2. index="child_example",
  3. size="0",
  4. aggs={
  5. "top-tags": {
  6. "terms": {
  7. "field": "tags.keyword",
  8. "size": 10
  9. },
  10. "aggs": {
  11. "to-answers": {
  12. "children": {
  13. "type": "answer"
  14. },
  15. "aggs": {
  16. "top-names": {
  17. "terms": {
  18. "field": "owner.display_name.keyword",
  19. "size": 10
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. },
  27. )
  28. print(resp)

Ruby

  1. response = client.search(
  2. index: 'child_example',
  3. size: 0,
  4. body: {
  5. aggregations: {
  6. "top-tags": {
  7. terms: {
  8. field: 'tags.keyword',
  9. size: 10
  10. },
  11. aggregations: {
  12. "to-answers": {
  13. children: {
  14. type: 'answer'
  15. },
  16. aggregations: {
  17. "top-names": {
  18. terms: {
  19. field: 'owner.display_name.keyword',
  20. size: 10
  21. }
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. )
  30. puts response

Js

  1. const response = await client.search({
  2. index: "child_example",
  3. size: 0,
  4. aggs: {
  5. "top-tags": {
  6. terms: {
  7. field: "tags.keyword",
  8. size: 10,
  9. },
  10. aggs: {
  11. "to-answers": {
  12. children: {
  13. type: "answer",
  14. },
  15. aggs: {
  16. "top-names": {
  17. terms: {
  18. field: "owner.display_name.keyword",
  19. size: 10,
  20. },
  21. },
  22. },
  23. },
  24. },
  25. },
  26. },
  27. });
  28. console.log(response);

コンソール

  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"
  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. }
type は、answer という名前のタイプ / マッピングを指します。

上記の例は、トップの質問タグと各タグのトップの回答オーナーを返します。

可能な応答:

コンソール-結果

  1. {
  2. "took": 25,
  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-tags": {
  20. "doc_count_error_upper_bound": 0,
  21. "sum_other_doc_count": 0,
  22. "buckets": [
  23. {
  24. "key": "file-transfer",
  25. "doc_count": 1,
  26. "to-answers": {
  27. "doc_count": 2,
  28. "top-names": {
  29. "doc_count_error_upper_bound": 0,
  30. "sum_other_doc_count": 0,
  31. "buckets": [
  32. {
  33. "key": "Sam",
  34. "doc_count": 1
  35. },
  36. {
  37. "key": "Troll",
  38. "doc_count": 1
  39. }
  40. ]
  41. }
  42. }
  43. },
  44. {
  45. "key": "windows-server-2003",
  46. "doc_count": 1,
  47. "to-answers": {
  48. "doc_count": 2,
  49. "top-names": {
  50. "doc_count_error_upper_bound": 0,
  51. "sum_other_doc_count": 0,
  52. "buckets": [
  53. {
  54. "key": "Sam",
  55. "doc_count": 1
  56. },
  57. {
  58. "key": "Troll",
  59. "doc_count": 1
  60. }
  61. ]
  62. }
  63. }
  64. },
  65. {
  66. "key": "windows-server-2008",
  67. "doc_count": 1,
  68. "to-answers": {
  69. "doc_count": 2,
  70. "top-names": {
  71. "doc_count_error_upper_bound": 0,
  72. "sum_other_doc_count": 0,
  73. "buckets": [
  74. {
  75. "key": "Sam",
  76. "doc_count": 1
  77. },
  78. {
  79. "key": "Troll",
  80. "doc_count": 1
  81. }
  82. ]
  83. }
  84. }
  85. }
  86. ]
  87. }
  88. }
  89. }
タグ file-transferwindows-server-2003 などを持つ質問ドキュメントの数。
タグ file-transferwindows-server-2003 などの質問ドキュメントに関連する回答ドキュメントの数。