parent-id-query.asciidoc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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,console]
  20. ----
  21. PUT /my-index-000001
  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. // TESTSETUP
  36. --
  37. . Index a parent document with an ID of `1`.
  38. +
  39. --
  40. [source,console]
  41. ----
  42. PUT /my-index-000001/_doc/1?refresh
  43. {
  44. "text": "This is a parent document.",
  45. "my-join-field": "my-parent"
  46. }
  47. ----
  48. --
  49. . Index a child document of the parent document.
  50. +
  51. --
  52. [source,console]
  53. ----
  54. PUT /my-index-000001/_doc/2?routing=1&refresh
  55. {
  56. "text": "This is a child document.",
  57. "my_join_field": {
  58. "name": "my-child",
  59. "parent": "1"
  60. }
  61. }
  62. ----
  63. --
  64. [[parent-id-query-ex-query]]
  65. ===== Example query
  66. The following search returns child documents for a parent document with an ID of
  67. `1`.
  68. [source,console]
  69. ----
  70. GET /my-index-000001/_search
  71. {
  72. "query": {
  73. "parent_id": {
  74. "type": "my-child",
  75. "id": "1"
  76. }
  77. }
  78. }
  79. ----
  80. [[parent-id-top-level-params]]
  81. ==== Top-level parameters for `parent_id`
  82. `type`::
  83. (Required, string) Name of the child relationship mapped for the
  84. <<parent-join,join>> field.
  85. `id`::
  86. (Required, string) ID of the parent document. The query will return child
  87. documents of this parent document.
  88. `ignore_unmapped`::
  89. +
  90. --
  91. (Optional, boolean) Indicates whether to ignore an unmapped `type` and not
  92. return any documents instead of an error. Defaults to `false`.
  93. If `false`, {es} returns an error if the `type` is unmapped.
  94. You can use this parameter to query multiple indices that may not contain the
  95. `type`.
  96. --