# Aggregation over collapsed field inner\_hits?

**URL:** <https://discuss.elastic.co/t/aggregation-over-collapsed-field-inner-hits/305980>\
**Category:** Elasticsearch\
**Created:** [May 30, 2022, 10:42pm UTC](https://discuss.elastic.co/t/aggregation-over-collapsed-field-inner-hits/305980 "2022-05-30T22:42:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Evelyn\_Gillie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/evelyn_gillie/32/87812_2.png) [@Evelyn\_Gillie](https://discuss.elastic.co/u/Evelyn_Gillie)\
**Post date:** [May 30, 2022, 10:42pm UTC](https://discuss.elastic.co/t/aggregation-over-collapsed-field-inner-hits/305980/1 "2022-05-30T22:42:14Z")

</div>

```auto
Listing:
  item_id
  seller_id
  price

```

I want to group together all listings for the same item, and show the average price across all sellers.

I'm using field collapsing on item\_id, but is there a way to to compute the average price of all items collapsed together?

```auto
  "collapse": {
    "field": "item_id",
    "inner_hits": {
      "name": "hits",                  
      "size": 200
    }  
  },

```

I started off bucketing by item\_id and applying aggregations, but the cardinality of top\_hits is sometimes large and queries are very slow, so I'm looking for optimizations and wondering if field collapsing could offer the same functionality.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 1, 2022, 11:24am UTC](https://discuss.elastic.co/t/aggregation-over-collapsed-field-inner-hits/305980/2 "2022-06-01T11:24:44Z")

</div>

I'm not sure but according to the [NOTE](https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html) here, collapsing does not affect aggregations.

What is the slow query? Could you share some queries you tried?

If you need average on sample 200 documents per item\_id, how about using [sampler aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-sampler-aggregation.html) between aggregations?

---

<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:** [June 29, 2022, 11:25am UTC](https://discuss.elastic.co/t/aggregation-over-collapsed-field-inner-hits/305980/3 "2022-06-29T11:25:39Z")

</div>

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