2016-01-17 16:49:07 -05:00
|
|
|
[[query-dsl-parent-id-query]]
|
|
|
|
=== Parent Id Query
|
|
|
|
|
2016-02-29 10:21:39 -05:00
|
|
|
added[5.0.0]
|
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
|
|
|
--------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
PUT /my_index
|
2016-04-14 05:25:26 -04:00
|
|
|
{
|
2016-05-12 06:29:18 -04:00
|
|
|
"mappings": {
|
|
|
|
"blog_post": {
|
|
|
|
"properties": {
|
|
|
|
"name": {
|
|
|
|
"type": "keyword"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"blog_tag": {
|
|
|
|
"_parent": {
|
|
|
|
"type": "blog_post"
|
|
|
|
},
|
|
|
|
"_routing": {
|
|
|
|
"required": true
|
|
|
|
}
|
|
|
|
}
|
2016-04-14 04:37:18 -04:00
|
|
|
}
|
2016-04-14 05:25:26 -04:00
|
|
|
}
|
2016-05-18 15:10:01 -04:00
|
|
|
|
2016-05-17 15:12:06 -04:00
|
|
|
------------------------------------------
|
2016-05-11 08:37:19 -04:00
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
2016-04-14 05:25:26 -04:00
|
|
|
|
2016-05-12 06:29:18 -04:00
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
2016-05-17 09:40:53 -04:00
|
|
|
GET /my_index/_search
|
2016-05-12 06:29:18 -04:00
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"parent_id" : {
|
|
|
|
"type" : "blog_tag",
|
|
|
|
"id" : "1"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
// CONSOLE
|
|
|
|
|
2016-04-14 05:25:26 -04:00
|
|
|
The above is functionally equivalent to using the following
|
|
|
|
<<query-dsl-has-parent-query, `has_parent`>> query, but performs
|
|
|
|
better as it does not need to do a join:
|
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": {
|
|
|
|
"has_parent": {
|
|
|
|
"type": "blog_post",
|
|
|
|
"query": {
|
|
|
|
"term": {
|
|
|
|
"_id": "1"
|
|
|
|
}
|
2016-05-17 14:55:56 -04:00
|
|
|
}
|
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
|
|
|
|
|
|
|
==== Parameters
|
|
|
|
|
|
|
|
This query has two required parameters:
|
|
|
|
|
|
|
|
[horizontal]
|
2016-04-14 05:25:26 -04:00
|
|
|
`type`:: The **child** type. This must be a type with `_parent` field.
|
2016-01-17 16:49:07 -05:00
|
|
|
|
2016-04-14 04:37:18 -04:00
|
|
|
`id`:: The required parent id select documents must referrer to.
|
|
|
|
|
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.
|