# Aggregate whole groups when search machtes only a few items of that group

**URL:** <https://discuss.elastic.co/t/aggregate-whole-groups-when-search-machtes-only-a-few-items-of-that-group/316082>\
**Category:** Elasticsearch\
**Created:** [October 7, 2022, 1:30pm UTC](https://discuss.elastic.co/t/aggregate-whole-groups-when-search-machtes-only-a-few-items-of-that-group/316082 "2022-10-07T13:30:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lanwin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lanwin/32/76510_2.png) [@lanwin](https://discuss.elastic.co/u/lanwin)\
**Post date:** [October 7, 2022, 1:30pm UTC](https://discuss.elastic.co/t/aggregate-whole-groups-when-search-machtes-only-a-few-items-of-that-group/316082/1 "2022-10-07T13:30:02Z")

</div>

Hi, i am looking for a solution of the following problem.

I have 20 million "Item" objects. Each Item Object have a GroupId property where 1-5000 Items a grouped together. Each Item has some other properties we can use to filter.

My problem is now that I want to get and aggregate all Items of a group where my complex search (on the other properties) matches at least one item of that group.

If I would only get the items, I could do a search where I get all GroupIds from my complex search. And then do a simple query to get all data where GroupIds in that list.

But how to aggregate that? Especially when there can be 1mio Items in the resultset?

---

<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 4, 2022, 1:30pm UTC](https://discuss.elastic.co/t/aggregate-whole-groups-when-search-machtes-only-a-few-items-of-that-group/316082/2 "2022-11-04T13:30:37Z")

</div>

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