parent-field.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. [[mapping-parent-field]]
  2. === `_parent` field
  3. A parent-child relationship can be established between documents in the same
  4. index by making one mapping type the parent of another:
  5. [source,js]
  6. --------------------------------------------------
  7. PUT my_index
  8. {
  9. "mappings": {
  10. "my_parent": {},
  11. "my_child": {
  12. "_parent": {
  13. "type": "my_parent" <1>
  14. }
  15. }
  16. }
  17. }
  18. PUT my_index/my_parent/1 <2>
  19. {
  20. "text": "This is a parent document"
  21. }
  22. PUT my_index/my_child/2?parent=1 <3>
  23. {
  24. "text": "This is a child document"
  25. }
  26. PUT my_index/my_child/3?parent=1 <3>
  27. {
  28. "text": "This is another child document"
  29. }
  30. GET my_index/my_parent/_search
  31. {
  32. "query": {
  33. "has_child": { <4>
  34. "type": "my_child",
  35. "query": {
  36. "match": {
  37. "text": "child document"
  38. }
  39. }
  40. }
  41. }
  42. }
  43. --------------------------------------------------
  44. // AUTOSENSE
  45. <1> The `my_parent` type is parent to the `my_child` type.
  46. <2> Index a parent document.
  47. <3> Index two child documents, specifying the parent document's ID.
  48. <4> Find all parent documents that have children which match the query.
  49. See the <<query-dsl-has-child-query,`has_child`>> and
  50. <<query-dsl-has-parent-query,`has_parent`>> queries,
  51. the <<search-aggregations-bucket-children-aggregation,`children`>> aggregation,
  52. and <<parent-child-inner-hits,inner hits>> for more information.
  53. The value of the `_parent` field is accessible in queries, aggregations, scripts,
  54. and when sorting:
  55. [source,js]
  56. --------------------------
  57. GET my_index/_search
  58. {
  59. "query": {
  60. "terms": {
  61. "_parent": [ "1" ] <1>
  62. }
  63. },
  64. "aggs": {
  65. "parents": {
  66. "terms": {
  67. "field": "_parent", <2>
  68. "size": 10
  69. }
  70. }
  71. },
  72. "sort": [
  73. {
  74. "_parent": { <3>
  75. "order": "desc"
  76. }
  77. }
  78. ],
  79. "script_fields": {
  80. "parent": {
  81. "script": "doc['_parent']" <4>
  82. }
  83. }
  84. }
  85. --------------------------
  86. // AUTOSENSE
  87. <1> Querying on the `_parent` field (also see the <<query-dsl-has-parent-query,`has_parent` query>> and the <<query-dsl-has-child-query,`has_child` query>>)
  88. <2> Aggregating on the `_parent` field (also see the <<search-aggregations-bucket-children-aggregation,`children`>> aggregation)
  89. <3> Sorting on the `_parent` field
  90. <4> Accessing the `_parent` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)
  91. ==== Parent-child restrictions
  92. * The parent and child types must be different -- parent-child relationships
  93. cannot be established between documents of the same type.
  94. * The `_parent.type` setting can only point to a type that doesn't exist yet.
  95. This means that a type cannot become a parent type after it is has been
  96. created.
  97. * Parent and child documents must be indexed on the same shard. The `parent`
  98. ID is used as the <<mapping-routing-field,routing>> value for the child,
  99. to ensure that the child is indexed on the same shard as the parent.
  100. This means that the same `parent` value needs to be provided when
  101. <<docs-get,getting>>, <<docs-delete,deleting>>, or <<docs-update,updating>>
  102. a child document.
  103. ==== Global ordinals
  104. Parent-child uses <<global-ordinals,global ordinals>> to speed up joins.
  105. Global ordinals need to be rebuilt after any change to a shard. The more
  106. parent id values are stored in a shard, the longer it takes to rebuild the
  107. global ordinals for the `_parent` field.
  108. Global ordinals, by default, are built lazily: the first parent-child query or
  109. aggregation after a refresh will trigger building of global ordinals. This can
  110. introduce a significant latency spike for your users. You can use
  111. <<fielddata-loading,eager_global_ordinals>> to shift the cost of building global
  112. ordinals from query time to refresh time, by mapping the `_parent` field as follows:
  113. [source,js]
  114. --------------------------------------------------
  115. PUT my_index
  116. {
  117. "mappings": {
  118. "my_parent": {},
  119. "my_child": {
  120. "_parent": {
  121. "type": "my_parent",
  122. "fielddata": {
  123. "loading": "eager_global_ordinals"
  124. }
  125. }
  126. }
  127. }
  128. }
  129. --------------------------------------------------
  130. // AUTOSENSE
  131. The amount of heap used by global ordinals can be checked as follows:
  132. [source,sh]
  133. --------------------------------------------------
  134. # Per-index
  135. GET _stats/fielddata?human&fields=_parent
  136. # Per-node per-index
  137. GET _nodes/stats/indices/fielddata?human&fields=_parent
  138. --------------------------------------------------
  139. // AUTOSENSE