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
|
|
|
|
2016-05-17 15:12:06 -04:00
|
|
|
The `parent_id` query can be used to find child documents which belong to a particular parent.
|
|
|
|
Given the following mapping definition:
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2016-04-14 05:25:26 -04:00
|
|
|
[source,js]
|
2016-05-17 15:12:06 -04:00
|
|
|
--------------------------------------------
|
2019-01-22 09:13:52 -05:00
|
|
|
PUT my_index
|
2016-04-14 05:25:26 -04:00
|
|
|
{
|
2017-07-05 06:30:19 -04:00
|
|
|
"mappings": {
|
2019-01-22 09:13:52 -05:00
|
|
|
"properties": {
|
|
|
|
"my_join_field": {
|
|
|
|
"type": "join",
|
|
|
|
"relations": {
|
|
|
|
"my_parent": "my_child"
|
2016-05-12 06:29:18 -04:00
|
|
|
}
|
2017-07-05 06:30:19 -04:00
|
|
|
}
|
2016-04-14 04:37:18 -04:00
|
|
|
}
|
2017-07-05 06:30:19 -04:00
|
|
|
}
|
2016-04-14 05:25:26 -04:00
|
|
|
}
|
2016-05-18 15:10:01 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/1?refresh
|
2017-07-05 06:30:19 -04:00
|
|
|
{
|
|
|
|
"text": "This is a parent document",
|
|
|
|
"my_join_field": "my_parent"
|
|
|
|
}
|
2016-04-14 05:25:26 -04:00
|
|
|
|
2017-12-14 11:47:53 -05:00
|
|
|
PUT my_index/_doc/2?routing=1&refresh
|
2016-05-12 06:29:18 -04:00
|
|
|
{
|
2017-07-05 06:30:19 -04:00
|
|
|
"text": "This is a child document",
|
|
|
|
"my_join_field": {
|
|
|
|
"name": "my_child",
|
|
|
|
"parent": "1"
|
|
|
|
}
|
2016-05-12 06:29:18 -04:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
--------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
2016-01-17 16:49:07 -05:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
GET /my_index/_search
|
2016-01-17 16:49:07 -05:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
2017-07-05 06:30:19 -04:00
|
|
|
"parent_id": {
|
|
|
|
"type": "my_child",
|
|
|
|
"id": "1"
|
2016-01-17 16:49:07 -05:00
|
|
|
}
|
2016-05-17 14:55:56 -04:00
|
|
|
}
|
2016-01-17 16:49:07 -05:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2017-07-05 06:30:19 -04:00
|
|
|
|
2016-01-17 16:49:07 -05:00
|
|
|
==== Parameters
|
|
|
|
|
|
|
|
This query has two required parameters:
|
|
|
|
|
|
|
|
[horizontal]
|
2017-07-05 06:30:19 -04:00
|
|
|
`type`:: The **child** type name, as specified in the <<parent-join,`join` field>>.
|
|
|
|
`id`:: The ID of the parent document.
|
2016-04-14 04:37:18 -04:00
|
|
|
|
2016-05-18 15:10:01 -04:00
|
|
|
`ignore_unmapped`:: When set to `true` this will ignore an unmapped `type` and will not match any
|
2016-04-14 04:37:18 -04:00
|
|
|
documents for this query. This can be useful when querying multiple indexes
|
|
|
|
which might have different mappings. When set to `false` (the default value)
|
|
|
|
the query will throw an exception if the `type` is not mapped.
|