parent-id-query.asciidoc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. [[query-dsl-parent-id-query]]
  2. === Parent ID query
  3. ++++
  4. <titleabbrev>Parent ID</titleabbrev>
  5. ++++
  6. Returns child documents <<parent-join,joined>> to a specific parent document.
  7. You can use a <<parent-join,join>> field mapping to create parent-child
  8. relationships between documents in the same index.
  9. [[parent-id-query-ex-request]]
  10. ==== Example request
  11. [[parent-id-index-setup]]
  12. ===== Index setup
  13. To use the `parent_id` query, your index must include a <<parent-join,join>>
  14. field mapping. To see how you can set up an index for the `parent_id` query, try
  15. the following example.
  16. . Create an index with a <<parent-join,join>> field mapping.
  17. +
  18. --
  19. [source,js]
  20. ----
  21. PUT /my-index
  22. {
  23. "mappings": {
  24. "properties" : {
  25. "my-join-field" : {
  26. "type" : "join",
  27. "relations": {
  28. "my-parent": "my-child"
  29. }
  30. }
  31. }
  32. }
  33. }
  34. ----
  35. // CONSOLE
  36. // TESTSETUP
  37. --
  38. . Index a parent document with an ID of `1`.
  39. +
  40. --
  41. [source,js]
  42. ----
  43. PUT /my-index/_doc/1?refresh
  44. {
  45. "text": "This is a parent document.",
  46. "my-join-field": "my-parent"
  47. }
  48. ----
  49. // CONSOLE
  50. --
  51. . Index a child document of the parent document.
  52. +
  53. --
  54. [source,js]
  55. ----
  56. PUT /my-index/_doc/2?routing=1&refresh
  57. {
  58. "text": "This is a child document.",
  59. "my_join_field": {
  60. "name": "my-child",
  61. "parent": "1"
  62. }
  63. }
  64. ----
  65. // CONSOLE
  66. --
  67. [[parent-id-query-ex-query]]
  68. ===== Example query
  69. The following search returns child documents for a parent document with an ID of
  70. `1`.
  71. [source,js]
  72. ----
  73. GET /my-index/_search
  74. {
  75. "query": {
  76. "parent_id": {
  77. "type": "my-child",
  78. "id": "1"
  79. }
  80. }
  81. }
  82. ----
  83. // CONSOLE
  84. [[parent-id-top-level-params]]
  85. ==== Top-level parameters for `parent_id`
  86. `type`::
  87. (Required, string) Name of the child relationship mapped for the
  88. <<parent-join,join>> field.
  89. `id`::
  90. (Required, string) ID of the parent document. The query will return child
  91. documents of this parent document.
  92. `ignore_unmapped`::
  93. +
  94. --
  95. (Optional, boolean) Indicates whether to ignore an unmapped `type` and not
  96. return any documents instead of an error. Defaults to `false`.
  97. If `false`, {es} returns an error if the `type` is unmapped.
  98. You can use this parameter to query multiple indices that may not contain the
  99. `type`.
  100. --