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 top_children query


In the previous recipe, the has_child query consumes a huge amount of memory because it requires you to fetch all child IDs. To bypass this limitation in huge data contexts, the top_children query allows you to fetch only the top child results. This scenario is very common: think of a blog with the latest 10 comments.

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 top_children query, perform the following steps:

  • Search the test-type parent of which the test-type2 top child has a term in the field value as value1. We can create a query, as follows:

    curl -XPOST 'http://127.0.0.1:9200/test-index/test-type/_search?pretty=true' -d '{
      "query": {
        "top_children" : {
          "type" : "test-type2",
          "query" : {
            "term" : {
              "value" : "value1"
            }
          },
          "score" : "max",
          "factor...