Executing a 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 (documents).
Generally, the top hit aggregation is used as a sub-aggregation, so that the top matching documents can be aggregated in buckets. The most common scenario for this aggregation is to have, for example, the top n
documents grouped by category (very common in search results in e-commerce websites).
Getting ready
You need an up and running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe in Chapter 1, Getting Started.
To execute these commands, any HTTP client can be used, such as cURL (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. Using Kibana Console is recommended, as it provides code completion and better character escaping for Elasticsearch.
To correctly...