Aggregation over top hits

Hello,
This is my query

{
  "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?

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