# Aggregation over top hits

**URL:** <https://discuss.elastic.co/t/aggregation-over-top-hits/308169>\
**Category:** Elasticsearch\
**Created:** [June 25, 2022, 9:33pm UTC](https://discuss.elastic.co/t/aggregation-over-top-hits/308169 "2022-06-25T21:33:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nowrin\_Hossain](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nowrin_hossain/32/99918_2.png) [@Nowrin\_Hossain](https://discuss.elastic.co/u/Nowrin_Hossain)\
**Post date:** [June 25, 2022, 9:33pm UTC](https://discuss.elastic.co/t/aggregation-over-top-hits/308169/1 "2022-06-25T21:33:23Z")

</div>

Hello,  
This is my query

```auto
{
  "aggs": {
    "by_agency_id": {
      "terms": {
        "field": "agency_id.keyword",
        "size": 1000
      },
      "aggs": {
        "by_uuid": {
          "terms": {
            "field": "search_summary_uuid.keyword",
            "size": 1000
          },
          "aggs": {
            "success_count": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "data_retrieval_status.keyword": {
                      "order": "desc"
                    }
                  }
                ],
                "_source": [
                  "agency_id",
                  "data_retrieval_status"
                ]
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

```

I want to run term and then cardinality aggregation over all the top hits of a single "by\_agency\_id" output bucket separately. can anyone help me how to do this?

---

<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:** [July 23, 2022, 9:33pm UTC](https://discuss.elastic.co/t/aggregation-over-top-hits/308169/2 "2022-07-23T21:33:37Z")

</div>

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