[[query-dsl-parent-id-query]]
=== Parent ID query
++++
Parent ID
++++
Returns child documents <> to a specific parent document.
You can use a <> field mapping to create parent-child
relationships between documents in the same index.
[[parent-id-query-ex-request]]
==== Example request
[[parent-id-index-setup]]
===== Index setup
To use the `parent_id` query, your index must include a <>
field mapping. To see how you can set up an index for the `parent_id` query, try
the following example.
. Create an index with a <> field mapping.
+
--
[source,js]
----
PUT /my-index
{
"mappings": {
"properties" : {
"my-join-field" : {
"type" : "join",
"relations": {
"my-parent": "my-child"
}
}
}
}
}
----
// CONSOLE
// TESTSETUP
--
. Index a parent document with an ID of `1`.
+
--
[source,js]
----
PUT /my-index/_doc/1?refresh
{
"text": "This is a parent document.",
"my-join-field": "my-parent"
}
----
// CONSOLE
--
. Index a child document of the parent document.
+
--
[source,js]
----
PUT /my-index/_doc/2?routing=1&refresh
{
"text": "This is a child document.",
"my_join_field": {
"name": "my-child",
"parent": "1"
}
}
----
// CONSOLE
--
[[parent-id-query-ex-query]]
===== Example query
The following search returns child documents for a parent document with an ID of
`1`.
[source,js]
----
GET /my-index/_search
{
"query": {
"parent_id": {
"type": "my-child",
"id": "1"
}
}
}
----
// CONSOLE
[[parent-id-top-level-params]]
==== Top-level parameters for `parent_id`
`type`::
(Required, string) Name of the child relationship mapped for the
<> field.
`id`::
(Required, string) ID of the parent document. The query will return child
documents of this parent document.
`ignore_unmapped`::
+
--
(Optional, boolean) Indicates whether to ignore an unmapped `type` and not
return any documents instead of an error. Defaults to `false`.
If `false`, {es} returns an error if the `type` is unmapped.
You can use this parameter to query multiple indices that may not contain the
`type`.
--