# How to aggregate inner\_hits results?

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-inner-hits-results/52633>\
**Category:** Elasticsearch\
**Created:** [June 13, 2016, 3:57pm UTC](https://discuss.elastic.co/t/how-to-aggregate-inner-hits-results/52633 "2016-06-13T15:57:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![apanimesh061](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/apanimesh061/32/927_2.png) [@apanimesh061](https://discuss.elastic.co/u/apanimesh061)\
**Post date:** [June 13, 2016, 3:57pm UTC](https://discuss.elastic.co/t/how-to-aggregate-inner-hits-results/52633/1 "2016-06-13T15:57:03Z")

</div>

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`.

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [June 13, 2016, 6:24pm UTC](https://discuss.elastic.co/t/how-to-aggregate-inner-hits-results/52633/2 "2016-06-13T18:24:13Z")

</div>

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.

---

<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 5, 2017, 10:44pm UTC](https://discuss.elastic.co/t/how-to-aggregate-inner-hits-results/52633/3 "2017-07-05T22:44:15Z")

</div>


