Using a has_parent query/filter
In the previous recipes, we have seen the has_child
query. ElasticSearch provides a query to search child documents based on the parent query: the has_parent
query.
Getting ready
You need a working ElasticSearch cluster and an index populated with the script chapter_05/populate_query.sh
, available in the code bundle for this book.
How to do it...
In order to execute the has_parent
query/filter, perform the following steps:
Search for the
test-type2
children of which thetest-type
parents have a termjoe
in theparsedtext
field. Create the query as follows:curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search?pretty=true' -d '{ "query": { "has_parent" : { "type" : "test-type", "query" : { "term" : { "parsedtext" : "joe" } } } } }'
If scoring is not important, then it's better to reformulate the query as a filter in this way:
curl -XPOST 'http://127.0.0.1:9200/test-index/test-type2/_search?pretty...