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_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:

  1. Search for the test-type2 children of which the test-type parents have a term joe in the parsedtext 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"
            }
          }
        }
      }
    }'
    
  2. 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...