How to use `top_hits` hits as input of another elasticsearch pipeline aggregation

I tried to process my data as below:

  1. group documents by its country field with terms aggregation.
  2. in each bucket, I select the document with the highest price value by using top_hits aggregation.

Now, I want to sort aggregated buckets by the vendor_name of the selected documents of each bucket. I tried with this query

{
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "main_aggs": {
      "terms": {
        "field": "country",
        "size": 1000000000
      },
      "aggs": {
        "docs_aggs": {
          "top_hits": {
            "size": 1,
            "_source": true,
            "sort": [
              {
                "price": {
                  "order": "desc"
                }
              }
            ]
          }
        },
        "main_bucket_sort": {
          "bucket_sort": {
            "sort": [
              {
                "docs_aggs.hits.vendor_name": "desc"
              }
              ],
            "size": 5
          }
        }
      }
    }
  }
}

However, Elasticsearch throws and error telling that:

No aggregation found for path [docs_aggs.hits.vendor]

My question is how we can sort aggregated buckets based on the output of the top_hits aggregation?

I really appreciate any help you can provide.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.