Skip to content

Commit f3ddd36

Browse files
authored
[DOCS] Reformat has_parent query docs (#44443)
1 parent a6b12b5 commit f3ddd36

File tree

1 file changed

+91
-55
lines changed

1 file changed

+91
-55
lines changed

docs/reference/query-dsl/has-parent-query.asciidoc

Lines changed: 91 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,93 +4,129 @@
44
<titleabbrev>Has parent</titleabbrev>
55
++++
66

7-
The `has_parent` query accepts a query and a parent type. The query is
8-
executed in the parent document space, which is specified by the parent
9-
type. This query returns child documents which associated parents have
10-
matched. For the rest `has_parent` query has the same options and works
11-
in the same manner as the `has_child` query.
7+
Returns child documents whose <<parent-join,joined>> parent document matches a
8+
provided query. You can create parent-child relationships between documents in
9+
the same index using a <<parent-join,join>> field mapping.
10+
11+
[WARNING]
12+
====
13+
Because it performs a join, the `has_parent` query is slow compared to other queries.
14+
Its performance degrades as the number of matching parent documents increases.
15+
Each `has_parent` query in a search can increase query time significantly.
16+
====
17+
18+
[[has-parent-query-ex-request]]
19+
==== Example request
20+
21+
[[has-parent-index-setup]]
22+
===== Index setup
23+
To use the `has_parent` query, your index must include a <<parent-join,join>>
24+
field mapping. For example:
1225

1326
[source,js]
14-
--------------------------------------------------
15-
GET /_search
27+
----
28+
PUT /my-index
1629
{
17-
"query": {
18-
"has_parent" : {
19-
"parent_type" : "blog",
20-
"query" : {
21-
"term" : {
22-
"tag" : "something"
30+
"mappings": {
31+
"properties" : {
32+
"my-join-field" : {
33+
"type" : "join",
34+
"relations": {
35+
"parent": "child"
2336
}
37+
},
38+
"tag" : {
39+
"type" : "keyword"
2440
}
2541
}
2642
}
2743
}
28-
--------------------------------------------------
29-
// CONSOLE
3044
31-
Note that the `has_parent` is a slow query compared to other queries in the
32-
query dsl due to the fact that it performs a join. The performance degrades
33-
as the number of matching parent documents increases. If you care about query
34-
performance you should not use this query. However if you do happen to use
35-
this query then use it as less as possible. Each `has_parent` query that gets
36-
added to a search request can increase query time significantly.
37-
38-
[float]
39-
==== Scoring capabilities
45+
----
46+
// CONSOLE
47+
// TESTSETUP
4048

41-
The `has_parent` also has scoring support. The default is `false` which
42-
ignores the score from the parent document. The score is in this
43-
case equal to the boost on the `has_parent` query (Defaults to 1). If
44-
the score is set to `true`, then the score of the matching parent
45-
document is aggregated into the child documents belonging to the
46-
matching parent document. The score mode can be specified with the
47-
`score` field inside the `has_parent` query:
49+
[[has-parent-query-ex-query]]
50+
===== Example query
4851

4952
[source,js]
50-
--------------------------------------------------
51-
GET /_search
53+
----
54+
GET /my-index/_search
5255
{
5356
"query": {
5457
"has_parent" : {
55-
"parent_type" : "blog",
56-
"score" : true,
58+
"parent_type" : "parent",
5759
"query" : {
5860
"term" : {
59-
"tag" : "something"
61+
"tag" : {
62+
"value" : "Elasticsearch"
63+
}
6064
}
6165
}
6266
}
6367
}
6468
}
65-
--------------------------------------------------
69+
----
6670
// CONSOLE
6771

68-
[float]
69-
==== Ignore Unmapped
72+
[[has-parent-top-level-params]]
73+
==== Top-level parameters for `has_parent`
74+
75+
`parent_type`::
76+
(Required, string) Name of the parent relationship mapped for the
77+
<<parent-join,join>> field.
78+
79+
`query`::
80+
(Required, query object) Query you wish to run on parent documents of the
81+
`parent_type` field. If a parent document matches the search, the query returns
82+
its child documents.
83+
84+
`score`::
85+
+
86+
--
87+
(Optional, boolean) Indicates whether the <<query-filter-context,relevance
88+
score>> of a matching parent document is aggregated into its child documents.
89+
Defaults to `false`.
7090

71-
When set to `true` the `ignore_unmapped` option will ignore an unmapped `type`
72-
and will not match any documents for this query. This can be useful when
73-
querying multiple indexes which might have different mappings. When set to
74-
`false` (the default value) the query will throw an exception if the `type`
75-
is not mapped.
91+
If `false`, {es} ignores the relevance score of the parent document. {es} also
92+
assigns each child document a relevance score equal to the `query`'s `boost`,
93+
which defaults to `1`.
7694

77-
[float]
78-
==== Sorting
95+
If `true`, the relevance score of the matching parent document is aggregated
96+
into its child documents' relevance scores.
97+
--
7998

80-
Child documents can't be sorted by fields in matching parent documents via the
81-
regular sort options. If you need to sort child documents by field in the parent
82-
documents then you should use the `function_score` query and then just sort
83-
by `_score`.
99+
`ignore_unmapped`::
100+
+
101+
--
102+
(Optional, boolean) Indicates whether to ignore an unmapped `parent_type` and
103+
not return any documents instead of an error. Defaults to `false`.
84104

85-
Sorting tags by parent document' `view_count` field:
105+
If `false`, {es} returns an error if the `parent_type` is unmapped.
106+
107+
You can use this parameter to query multiple indices that may not contain the
108+
`parent_type`.
109+
--
110+
111+
[[has-parent-query-notes]]
112+
==== Notes
113+
114+
[[has-parent-query-performance]]
115+
===== Sorting
116+
You cannot sort the results of a `has_parent` query using standard
117+
<<search-request-sort,sort options>>.
118+
119+
If you need to sort returned documents by a field in their parent documents, use
120+
a `function_score` query and sort by `_score`. For example, the following query
121+
sorts returned documents by the `view_count` field of their parent documents.
86122

87123
[source,js]
88-
--------------------------------------------------
124+
----
89125
GET /_search
90126
{
91127
"query": {
92128
"has_parent" : {
93-
"parent_type" : "blog",
129+
"parent_type" : "parent",
94130
"score" : true,
95131
"query" : {
96132
"function_score" : {
@@ -102,5 +138,5 @@ GET /_search
102138
}
103139
}
104140
}
105-
--------------------------------------------------
106-
// CONSOLE
141+
----
142+
// CONSOLE

0 commit comments

Comments
 (0)