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

Executing the top hit aggregation


The top hit aggregation is different from the other aggregation types. All the previous aggregations have metric (simple values) or bucket values; the top hit aggregation returns buckets of search hits.

Generally, the top hit aggregation is used as a subaggregation so that the top matching documents can be aggregated in buckets.

Getting ready

You need a working ElasticSearch cluster and an index populated with the script (chapter_06/executing_top_hit_aggregations.sh) available at https://github.com/aparo/elasticsearch-cookbook-second-edition.

How to do it...

To execute a top hit aggregation, we will perform the steps given as follows:

  1. We want to aggregate the documents hits by tag (tags) and return only the name field of documents with a maximum age (top_tag_hits). We'll execute the search and aggregation with the following command:

    curl -XGET 'http://127.0.0.1:9200/test-index/test-type/_search' -d '{
      "query": {
        "match_all": {}
      },
      "size": 0,
      "aggs...