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:
- We want to aggregate the documents hits by tag (
tags
) and return only thename
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...