# How to apply aggregations to collapsed/grouped results?

**URL:** <https://discuss.elastic.co/t/how-to-apply-aggregations-to-collapsed-grouped-results/146673>\
**Category:** Elasticsearch\
**Created:** [August 30, 2018, 10:20am UTC](https://discuss.elastic.co/t/how-to-apply-aggregations-to-collapsed-grouped-results/146673 "2018-08-30T10:20:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheoVitkovskiy](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@TheoVitkovskiy](https://discuss.elastic.co/u/TheoVitkovskiy)\
**Post date:** [August 30, 2018, 10:20am UTC](https://discuss.elastic.co/t/how-to-apply-aggregations-to-collapsed-grouped-results/146673/1 "2018-08-30T10:20:06Z")

</div>

In the documentation for field collapsing it states: The collapsing is applied to the top hits only and does not affect aggregations.

My question:

On my eCommerce store I want to only include the first item in each group (grouped by item\_id) in the final results. At the same time I don't want to lose my aggregations (little numbers next to attributes that indicate how many items with that attribute are found).

Here is a little example:

Suppose I make a search for items and only 25 show up. This is the result for the color aggregation that I currently get:

- black (65)
- green (32)
- white (13)

And I want it to be:

- black (14)
- green (6)
- white (5)

The numbers should amount to the total number (25) the user actually sees on the page.

How could I achieve that with Elasticsearch? I have tried both Grouping (Top Hits) and Field Collapsing and both don't seem to fit my use case. Solr does it almost by default with its Grouping functionality.

---

<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 27, 2018, 10:34am UTC](https://discuss.elastic.co/t/how-to-apply-aggregations-to-collapsed-grouped-results/146673/2 "2018-09-27T10:34:04Z")

</div>

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