# How to use \`top\_hits\` hits as input of another elasticsearch pipeline aggregation

**URL:** <https://discuss.elastic.co/t/how-to-use-top-hits-hits-as-input-of-another-elasticsearch-pipeline-aggregation/143660>\
**Category:** Elasticsearch\
**Created:** [August 9, 2018, 9:08am UTC](https://discuss.elastic.co/t/how-to-use-top-hits-hits-as-input-of-another-elasticsearch-pipeline-aggregation/143660 "2018-08-09T09:08:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![letrunghieu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/letrunghieu/32/18928_2.png) [@letrunghieu](https://discuss.elastic.co/u/letrunghieu)\
**Post date:** [August 9, 2018, 9:08am UTC](https://discuss.elastic.co/t/how-to-use-top-hits-hits-as-input-of-another-elasticsearch-pipeline-aggregation/143660/1 "2018-08-09T09:08:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 6, 2018, 9:08am UTC](https://discuss.elastic.co/t/how-to-use-top-hits-hits-as-input-of-another-elasticsearch-pipeline-aggregation/143660/2 "2018-09-06T09:08:41Z")

</div>

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