| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | [[query-dsl-has-parent-query]]=== Has parent query++++<titleabbrev>Has parent</titleabbrev>++++Returns child documents whose <<parent-join,joined>> parent document matches aprovided query. You can create parent-child relationships between documents inthe 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 setupTo use the `has_parent` query, your index must include a <<parent-join,join>>field mapping. For example:[source,console]----PUT /my-index{    "mappings": {        "properties" : {            "my-join-field" : {                "type" : "join",                "relations": {                    "parent": "child"                }            },            "tag" : {                "type" : "keyword"            }        }    }}----// TESTSETUP[[has-parent-query-ex-query]]===== Example query[source,console]----GET /my-index/_search{    "query": {        "has_parent" : {            "parent_type" : "parent",            "query" : {                "term" : {                    "tag" : {                        "value" : "Elasticsearch"                    }                }            }        }    }}----[[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 returnsits child documents.`score`::+--(Optional, boolean) Indicates whether the <<query-filter-context,relevancescore>> of a matching parent document is aggregated into its child documents.Defaults to `false`.If `false`, {es} ignores the relevance score of the parent document. {es} alsoassigns each child document a relevance score equal to the `query`'s `boost`,which defaults to `1`.If `true`, the relevance score of the matching parent document is aggregatedinto its child documents' relevance scores.--`ignore_unmapped`::+--(Optional, boolean) Indicates whether to ignore an unmapped `parent_type` andnot return any documents instead of an error. Defaults to `false`.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]]===== SortingYou cannot sort the results of a `has_parent` query using standard<<request-body-search-sort,sort options>>.If you need to sort returned documents by a field in their parent documents, usea `function_score` query and sort by `_score`. For example, the following querysorts returned documents by the `view_count` field of their parent documents.[source,console]----GET /_search{    "query": {        "has_parent" : {            "parent_type" : "parent",            "score" : true,            "query" : {                "function_score" : {                    "script_score": {                        "script": "_score * doc['view_count'].value"                    }                }            }        }    }}----
 |