2013-08-28 19:24:34 -04:00
|
|
|
[[query-dsl-has-parent-query]]
|
2019-07-18 10:18:11 -04:00
|
|
|
=== Has parent query
|
|
|
|
++++
|
|
|
|
<titleabbrev>Has parent</titleabbrev>
|
|
|
|
++++
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
Returns child documents whose <<parent-join,joined>> parent document matches a
|
|
|
|
provided query. You can create parent-child relationships between documents in
|
|
|
|
the same index using a <<parent-join,join>> field mapping.
|
|
|
|
|
|
|
|
[WARNING]
|
|
|
|
====
|
|
|
|
Because it performs a join, the `has_parent` query is slow compared to other queries.
|
|
|
|
Its performance degrades as the number of matching parent documents increases.
|
|
|
|
Each `has_parent` query in a search can increase query time significantly.
|
|
|
|
====
|
|
|
|
|
|
|
|
[[has-parent-query-ex-request]]
|
|
|
|
==== Example request
|
|
|
|
|
|
|
|
[[has-parent-index-setup]]
|
|
|
|
===== Index setup
|
|
|
|
To use the `has_parent` query, your index must include a <<parent-join,join>>
|
|
|
|
field mapping. For example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
|
|
|
PUT /my-index
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2019-07-19 10:50:36 -04:00
|
|
|
"mappings": {
|
|
|
|
"properties" : {
|
|
|
|
"my-join-field" : {
|
|
|
|
"type" : "join",
|
|
|
|
"relations": {
|
|
|
|
"parent": "child"
|
2016-05-24 05:58:43 -04:00
|
|
|
}
|
2019-07-19 10:50:36 -04:00
|
|
|
},
|
|
|
|
"tag" : {
|
|
|
|
"type" : "keyword"
|
2017-03-13 10:52:28 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|
|
|
|
// TESTSETUP
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
[[has-parent-query-ex-query]]
|
|
|
|
===== Example query
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
|
|
|
GET /my-index/_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2016-05-24 05:58:43 -04:00
|
|
|
"query": {
|
|
|
|
"has_parent" : {
|
2019-07-19 10:50:36 -04:00
|
|
|
"parent_type" : "parent",
|
2017-03-13 10:52:28 -04:00
|
|
|
"query" : {
|
|
|
|
"term" : {
|
2019-07-19 10:50:36 -04:00
|
|
|
"tag" : {
|
|
|
|
"value" : "Elasticsearch"
|
|
|
|
}
|
2016-05-24 05:58:43 -04:00
|
|
|
}
|
2017-03-13 10:52:28 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
2016-05-24 05:58:43 -04:00
|
|
|
// CONSOLE
|
2016-04-14 04:37:18 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
[[has-parent-top-level-params]]
|
|
|
|
==== Top-level parameters for `has_parent`
|
|
|
|
|
|
|
|
`parent_type`::
|
|
|
|
(Required, string) Name of the parent relationship mapped for the
|
|
|
|
<<parent-join,join>> field.
|
|
|
|
|
|
|
|
`query`::
|
|
|
|
(Required, query object) Query you wish to run on parent documents of the
|
|
|
|
`parent_type` field. If a parent document matches the search, the query returns
|
|
|
|
its child documents.
|
|
|
|
|
|
|
|
`score`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, boolean) Indicates whether the <<query-filter-context,relevance
|
|
|
|
score>> of a matching parent document is aggregated into its child documents.
|
|
|
|
Defaults to `false`.
|
2016-04-14 04:37:18 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
If `false`, {es} ignores the relevance score of the parent document. {es} also
|
|
|
|
assigns each child document a relevance score equal to the `query`'s `boost`,
|
|
|
|
which defaults to `1`.
|
2016-08-26 04:55:23 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
If `true`, the relevance score of the matching parent document is aggregated
|
|
|
|
into its child documents' relevance scores.
|
|
|
|
--
|
2016-08-26 04:55:23 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
`ignore_unmapped`::
|
|
|
|
+
|
|
|
|
--
|
|
|
|
(Optional, boolean) Indicates whether to ignore an unmapped `parent_type` and
|
|
|
|
not return any documents instead of an error. Defaults to `false`.
|
2016-08-26 04:55:23 -04:00
|
|
|
|
2019-07-19 10:50:36 -04:00
|
|
|
If `false`, {es} returns an error if the `parent_type` is unmapped.
|
|
|
|
|
|
|
|
You can use this parameter to query multiple indices that may not contain the
|
|
|
|
`parent_type`.
|
|
|
|
--
|
|
|
|
|
|
|
|
[[has-parent-query-notes]]
|
|
|
|
==== Notes
|
|
|
|
|
|
|
|
[[has-parent-query-performance]]
|
|
|
|
===== Sorting
|
|
|
|
You cannot sort the results of a `has_parent` query using standard
|
|
|
|
<<search-request-sort,sort options>>.
|
|
|
|
|
|
|
|
If you need to sort returned documents by a field in their parent documents, use
|
|
|
|
a `function_score` query and sort by `_score`. For example, the following query
|
|
|
|
sorts returned documents by the `view_count` field of their parent documents.
|
2016-08-26 04:55:23 -04:00
|
|
|
|
|
|
|
[source,js]
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
2016-08-26 04:55:23 -04:00
|
|
|
GET /_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"has_parent" : {
|
2019-07-19 10:50:36 -04:00
|
|
|
"parent_type" : "parent",
|
2016-08-26 04:55:23 -04:00
|
|
|
"score" : true,
|
|
|
|
"query" : {
|
|
|
|
"function_score" : {
|
|
|
|
"script_score": {
|
|
|
|
"script": "_score * doc['view_count'].value"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-19 10:50:36 -04:00
|
|
|
----
|
|
|
|
// CONSOLE
|