Using a has_child query/filter
ElasticSearch does not only support simple documents, but it also lets you define a hierarchy based on parent and children. The has_child
query allows you to query for the parent documents of which children match some queries.
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_child
queries/filters, perform the following steps:
Search for the parents,
test-type
, for which the children,test-type2
, have a term in the field value asvalue1
. We can create this kind of query as follows:curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{ "query": { "has_child" : { "type" : "test-type2", "query" : { "term" : { "value" : "value1" } } } } }'
If scoring is not important for performances, it's better to reformulate the query as...