親の集約

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

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

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

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

Python

  1. resp = client.indices.create(
  2. index="parent_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: 'parent_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: "parent_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 parent_example
  2. {
  3. "mappings": {
  4. "properties": {
  5. "join": {
  6. "type": "join",
  7. "relations": {
  8. "question": "answer"
  9. }
  10. }
  11. }
  12. }
  13. }

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

質問ドキュメントの例:

Python

  1. resp = client.index(
  2. index="parent_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: 'parent_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: "parent_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 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. }

answer ドキュメントの例:

Python

  1. resp = client.index(
  2. index="parent_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="parent_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: 'parent_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: 'parent_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: "parent_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: "parent_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 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. }

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

Python

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

Ruby

  1. response = client.search(
  2. index: 'parent_example',
  3. size: 0,
  4. body: {
  5. aggregations: {
  6. "top-names": {
  7. terms: {
  8. field: 'owner.display_name.keyword',
  9. size: 10
  10. },
  11. aggregations: {
  12. "to-questions": {
  13. parent: {
  14. type: 'answer'
  15. },
  16. aggregations: {
  17. "top-tags": {
  18. terms: {
  19. field: 'tags.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: "parent_example",
  3. size: 0,
  4. aggs: {
  5. "top-names": {
  6. terms: {
  7. field: "owner.display_name.keyword",
  8. size: 10,
  9. },
  10. aggs: {
  11. "to-questions": {
  12. parent: {
  13. type: "answer",
  14. },
  15. aggs: {
  16. "top-tags": {
  17. terms: {
  18. field: "tags.keyword",
  19. size: 10,
  20. },
  21. },
  22. },
  23. },
  24. },
  25. },
  26. },
  27. });
  28. console.log(response);

コンソール

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

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

可能な応答:

コンソール-結果

  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. }
タグ SamTroll などを持つ回答ドキュメントの数。
タグ SamTroll などを持つ回答ドキュメントに関連する質問ドキュメントの数。