How to aggregate inner_hits results?

I used to inner_hits to get number of child documents that matched under a parent document.

For that under the query I have

"inner_hits": {
    "size": 0
}

which gives a response type

"inner_hits": {
  "post": {
    "hits": {
      "total": 1334,
      "max_score": 0,
      "hits": []
    }
  }
}

under the parent document matched but the total is actually the number of child documents that have been matched using has_child query.

Suppose, I have a query where I try to match some terms:

{
  "bool": {
    "should": [
      {
        "query_string": {
          "query": "java",
          "fields": [
            "postContent"
          ]
        }
      },
      {
        "query_string": {
          "query": "javascript",
          "fields": [
            "postContent"
          ]
        }
      }
    ]
  }
}

For the top most parent matched I get total no. of children matched is 1334. How can I get the no. of documents that matched java and javascript separately for each parent document?

It is something like aggregation on inner_hits.

Inner hits can only include hits, but no aggregations.

The best way to achieve this is by sending a subsequent search request that contains the has_child's inner query with a term query, that only selects a particular child docs for a particular parent (_parent: [id]) as main query and your required aggregations.