Company
Date Published
Author
Elastic Engineering
Word count
2744
Language
-
Hacker News points
None

Summary

The top_hits aggregation introduced in Elasticsearch 1.3.0 allows users to track the top matching hits or documents instead of computing standard metrics like sum or average. This feature is particularly useful when combined with bucket aggregators such as terms or histograms, which group data into categories and allow the top_hits to display the most relevant documents per group. In the example provided, programming questions from Stack Exchange are grouped by programming language, with the top_hits aggregation identifying the most relevant questions related to web topics within each language. This approach provides deeper insights compared to using top_hits alone, which merely replicates results found in regular hits. To optimize sorting within buckets, a max_score aggregation can be utilized, ordering groups by the highest score, thus ensuring the most relevant documents are prioritized. The top_hits aggregation also supports sorting by fields other than relevancy, adding flexibility to data analysis in Elasticsearch.