Top hits aggregation

Top hits aggregation #

Keeps track of the most relevant documents being aggregated — useful inside a bucket aggregation (e.g. terms) to return the best-matching documents per bucket.

Examples #

Top 3 sales per merchant:

POST /sales/_search
{
  "aggs": {
    "top_merchants": {
      "terms": { "field": "merchant" },
      "aggs": {
        "top_sales": {
          "top_hits": {
            "size": 3,
            "sort": [{ "field": "amount", "order": "desc" }]
          }
        }
      }
    }
  }
}

Parameters for top_hits #

  • size
    (Optional, integer, default: 3) Number of top hits to return per bucket.
  • sort
    (Optional) Sort order for the top hits. Accepts "field", ["a", "b"] and [{ "field": "asc" }] forms — all normalize to an object array.
  • _source
    (Optional) Fields to include in the returned source. If empty, the full source is returned.
Calendar September 26, 2026
Edit Edit this page