2016-01-17 16:49:07 -05:00
|
|
|
[[query-dsl-parent-id-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Parent ID query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Parent ID</titleabbrev>
|
|
|
|
++++
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
Returns child documents <<parent-join,joined>> to a specific parent document.
|
|
|
|
You can use a <<parent-join,join>> field mapping to create parent-child
|
|
|
|
relationships between documents in the same index.
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
[[parent-id-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
[[parent-id-index-setup]]
|
|
|
|
===== Index setup
|
|
|
|
To use the `parent_id` query, your index must include a <<parent-join,join>>
|
|
|
|
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 <<parent-join,join>> field mapping.
|
|
|
|
+
|
|
|
|
--
|
2016-04-14 05:25:26 -04:00
|
|
|
[source,js]
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
|
|
|
PUT /my-index
|
2016-04-14 05:25:26 -04:00
|
|
|
{
|
2019-07-19 11:03:06 -04:00
|
|
|
"mappings": {
|
|
|
|
"properties" : {
|
|
|
|
"my-join-field" : {
|
|
|
|
"type" : "join",
|
|
|
|
"relations": {
|
|
|
|
"my-parent": "my-child"
|
|
|
|
}
|
|
|
|
}
|
2016-05-12 06:29:18 -04:00
|
|
|
}
|
2016-04-14 04:37:18 -04:00
|
|
|
}
|
2016-04-14 05:25:26 -04:00
|
|
|
}
|
2016-05-18 15:10:01 -04:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
|
|
|
--
|
|
|
|
|
|
|
|
. Index a parent document with an ID of `1`.
|
|
|
|
+
|
|
|
|
--
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
PUT /my-index/_doc/1?refresh
|
2017-07-05 06:30:19 -04:00
|
|
|
{
|
2019-07-19 11:03:06 -04:00
|
|
|
"text": "This is a parent document.",
|
|
|
|
"my-join-field": "my-parent"
|
2017-07-05 06:30:19 -04:00
|
|
|
}
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
--
|
2016-04-14 05:25:26 -04:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
. Index a child document of the parent document.
|
|
|
|
+
|
|
|
|
--
|
|
|
|
[source,js]
|
|
|
|
----
|
|
|
|
PUT /my-index/_doc/2?routing=1&refresh
|
2016-05-12 06:29:18 -04:00
|
|
|
{
|
2019-07-19 11:03:06 -04:00
|
|
|
"text": "This is a child document.",
|
2017-07-05 06:30:19 -04:00
|
|
|
"my_join_field": {
|
2019-07-19 11:03:06 -04:00
|
|
|
"name": "my-child",
|
2017-07-05 06:30:19 -04:00
|
|
|
"parent": "1"
|
|
|
|
}
|
2016-05-12 06:29:18 -04:00
|
|
|
}
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
2017-07-05 06:30:19 -04:00
|
|
|
// CONSOLE
|
2019-07-19 11:03:06 -04:00
|
|
|
--
|
|
|
|
|
|
|
|
[[parent-id-query-ex-query]]
|
|
|
|
===== Example query
|
|
|
|
|
|
|
|
The following search returns child documents for a parent document with an ID of
|
|
|
|
`1`.
|
2016-01-17 16:49:07 -05:00
|
|
|
|
|
|
|
[source,js]
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
|
|
|
GET /my-index/_search
|
2016-01-17 16:49:07 -05:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
2019-07-19 11:03:06 -04:00
|
|
|
"parent_id": {
|
|
|
|
"type": "my-child",
|
|
|
|
"id": "1"
|
|
|
|
}
|
2016-05-17 14:55:56 -04:00
|
|
|
}
|
2016-01-17 16:49:07 -05:00
|
|
|
}
|
2019-07-19 11:03:06 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
[[parent-id-top-level-params]]
|
|
|
|
==== Top-level parameters for `parent_id`
|
|
|
|
|
|
|
|
`type`::
|
|
|
|
(Required, string) Name of the child relationship mapped for the
|
|
|
|
<<parent-join,join>> field.
|
2017-07-05 06:30:19 -04:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
`id`::
|
|
|
|
(Required, string) ID of the parent document. The query will return child
|
|
|
|
documents of this parent document.
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
`ignore_unmapped`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, boolean) Indicates whether to ignore an unmapped `type` and not
|
|
|
|
return any documents instead of an error. Defaults to `false`.
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
If `false`, {es} returns an error if the `type` is unmapped.
|
2016-04-14 04:37:18 -04:00
|
|
|
|
2019-07-19 11:03:06 -04:00
|
|
|
You can use this parameter to query multiple indices that may not contain the
|
|
|
|
`type`.
|
|
|
|
--
|