In the previous recipes, we have seen the has_child
query. ElasticSearch provides a query to search a child based on the has_parent
parent query.
You need a working ElasticSearch cluster and the data populated with the populate script.
For executing the has_parent
query/filter, we need to perform the following steps:
test-type2
children of which the test-type
parents have a term joe
in the parsedtext
field. We can create this kind of query as follows:curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search' -d '{ "query": { "has_parent" : { "type" : "test-type", "query" : { "term" : { "parsedtext" : "joe" } } } } } }'
curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search' -d '{ "query": { "filtered": { "filter": { "has_parent" : { "type" : "test-type", "query" : { "term" : { "parsedtext" : "joe" } } } }, "query": { "match_all": {} } } } }'
This kind of query works returning children that match a parent query.
Internally this subquery is executed on the parents and all the IDs of the matching parents are used to filter the children. A system must have enough memory to store parent IDs.
The parameters that are used to control this process are as follows:
type
: This parameter defines the type of the parent.query
: This parameter defines the query that can be executed for selecting the parents. Every kind of query can be used.score_type
(default none
, available values are none
and score
): By using the default configuration of none
, ElasticSearch ignores the scores for the parent document reducing the memory usage and increasing performance. If it's set to score, the parent query score is aggregated into the children.18.226.17.168