Book Image

ElasticSearch Cookbook

By : Alberto Paro
Book Image

ElasticSearch Cookbook

By: Alberto Paro

Overview of this book

Table of Contents (20 chapters)
ElasticSearch Cookbook Second Edition
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. Search for the parents, test-type, for which the children, test-type2, have a term in the field value as value1. 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"
            }
          }
        }
      }
    }'
    
  2. If scoring is not important for performances, it's better to reformulate the query as...