# Aggregation on top\_hits aggregation results?

**URL:** https://discuss.elastic.co/t/aggregation-on-top-hits-aggregation-results/104598
**Category:** Elasticsearch
**Created:** [October 19, 2017, 4:42pm UTC](https://discuss.elastic.co/t/aggregation-on-top-hits-aggregation-results/104598 "2017-10-19T16:42:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![fpamba](https://avatars.discourse-cdn.com/v4/letter/f/a88e4f/32.png) [@fpamba](https://discuss.elastic.co/u/fpamba)
#### Post date: [October 19, 2017, 4:42pm UTC](https://discuss.elastic.co/t/aggregation-on-top-hits-aggregation-results/104598/1 "2017-10-19T16:42:25Z")

</div>

Hi,

I've an offer data model containing a `productId`, a `price` and a `rating`.

I want to:

1. Only retain the cheapest offer for each `productId`
2. Compute the average rating for remaining offers

Here is what I've done below.

```
"aggs": {
  "reference_ids": {
    "terms": {
      "field": "catalogProductId",
      "size": 1000
    },
    "aggs": {
      "min_price": {
        "top_hits": {
          "sort": [
            {
              "price": {
                "order": "asc"
              }
            }
          ],
          "size": 1
        }
      }
    }
  }
}

```

Is it possible to make another aggregation (`avg` on `rating`) after the `top_hits` one?

Thanks,  
Fidel

---

<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: [November 16, 2017, 4:42pm UTC](https://discuss.elastic.co/t/aggregation-on-top-hits-aggregation-results/104598/2 "2017-11-16T16:42:33Z")

</div>

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