4
4
<titleabbrev>Has parent</titleabbrev>
5
5
++++
6
6
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:
12
25
13
26
[source,js]
14
- --------------------------------------------------
15
- GET /_search
27
+ ----
28
+ PUT /my-index
16
29
{
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 "
23
36
}
37
+ },
38
+ "tag" : {
39
+ "type" : "keyword"
24
40
}
25
41
}
26
42
}
27
43
}
28
- --------------------------------------------------
29
- // CONSOLE
30
44
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
40
48
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
48
51
49
52
[source,js]
50
- --------------------------------------------------
51
- GET /_search
53
+ ----
54
+ GET /my-index/ _search
52
55
{
53
56
"query": {
54
57
"has_parent" : {
55
- "parent_type" : "blog",
56
- "score" : true,
58
+ "parent_type" : "parent",
57
59
"query" : {
58
60
"term" : {
59
- "tag" : "something"
61
+ "tag" : {
62
+ "value" : "Elasticsearch"
63
+ }
60
64
}
61
65
}
62
66
}
63
67
}
64
68
}
65
- --------------------------------------------------
69
+ ----
66
70
// CONSOLE
67
71
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`.
70
90
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`.
76
94
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
+ --
79
98
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`.
84
104
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.
86
122
87
123
[source,js]
88
- --------------------------------------------------
124
+ ----
89
125
GET /_search
90
126
{
91
127
"query": {
92
128
"has_parent" : {
93
- "parent_type" : "blog ",
129
+ "parent_type" : "parent ",
94
130
"score" : true,
95
131
"query" : {
96
132
"function_score" : {
@@ -102,5 +138,5 @@ GET /_search
102
138
}
103
139
}
104
140
}
105
- --------------------------------------------------
106
- // CONSOLE
141
+ ----
142
+ // CONSOLE
0 commit comments