# Aggregate TopHit based on filter

**URL:** <https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634>\
**Category:** Kibana\
**Created:** [May 21, 2021, 8:19am UTC](https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634 "2021-05-21T08:19:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![snicoll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/snicoll/32/20285_2.png) [@snicoll](https://discuss.elastic.co/u/snicoll)\
**Post date:** [May 21, 2021, 8:19am UTC](https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634/1 "2021-05-21T08:19:44Z")

</div>

I am working on a index that stores statistics about issue tracking. Each document is a snapshot of the state of a given project (number of open issues, etc). Each document is categorized by an organization, a project name, and tags.

I am using the "Last Value" in Lens "Top Hit" (in ES) to build graphs and it's working pretty well as long as I have a filter for one particular project. If I don't have an active filter to identify a project, Kibana uses one document, usually the last project that was indexed for the period. I'd like rather it to aggregate all projects.

So I'd like to understand how I can build the same graphs when I aggregate those "Last Value" documents (basically, one per projects). Is there a way to use a filter to let it sum the top hit per project? We're starting so an alternative would be a completely different structure for our document.

---

<div class="post-metadata">

**Author:** ![jbudz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jbudz/32/45922_2.png) [@jbudz](https://discuss.elastic.co/u/jbudz)\
**Post date:** [May 21, 2021, 5:08pm UTC](https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634/2 "2021-05-21T17:08:31Z")

</div>

Hey - this should be doable. We'll want to run a terms aggregation on the project first, and then run a top hits aggregation on the resulting buckets.

Does dragging the project name to the x-axis (below timestamp) get you closer?

---

<div class="post-metadata">

**Author:** ![snicoll](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/snicoll/32/20285_2.png) [@snicoll](https://discuss.elastic.co/u/snicoll)\
**Post date:** [May 25, 2021, 3:04pm UTC](https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634/3 "2021-05-25T15:04:01Z")

</div>

> Does dragging the project name to the x-axis (below timestamp) get you closer?

Thanks for the suggestion.

This result in a "Break down by" configuration being added, which creates one line per project rather than aggregating them. I've looked in the option and didn't find something that would aggregate them.

---

<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 22, 2021, 3:04pm UTC](https://discuss.elastic.co/t/aggregate-tophit-based-on-filter/273634/4 "2021-06-22T15:04:55Z")

</div>

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