# Using inner\_hits inside an aggregation

**URL:** <https://discuss.elastic.co/t/using-inner-hits-inside-an-aggregation/33631>\
**Category:** Elasticsearch\
**Created:** [November 3, 2015, 1:19pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-an-aggregation/33631 "2015-11-03T13:19:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![voiceofthemany](https://avatars.discourse-cdn.com/v4/letter/v/9de053/32.png) [@voiceofthemany](https://discuss.elastic.co/u/voiceofthemany)\
**Post date:** [November 3, 2015, 1:19pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-an-aggregation/33631/1 "2015-11-03T13:19:23Z")

</div>

I have a collection of documents which all contain an array of nested objects with important data. I want do to an aggregation on these which returns me the first document, last document, and all of the nested objects in that group. I can achieve everything in that list except for the nested objects.

Mapping:

```
"instances": {
   "properties": {
       "aggField": {
          "type": "string",
          "index": "not_analyzed"
       },
       "id": {
          "type": "integer"
       },
       "nestedObjs": {
          "type": "nested",
          "properties": {
             "key": {
                "type": "string",
                "index": "not_analyzed"
             },
             "value": {
                "type": "integer"
             }
          }
       },
       "timestamp": {
          "type": "date",
          "format": "dateOptionalTime"
       }
   }
}

```

Query:

```
{
"size" : 0,
"aggs" : {
    "agg-buckets" : {
        "terms" : {
            "field" : "aggField",
            "size" : 10
        },
        "aggs": {
            "last-report": {
                "top_hits": {
                    "sort": [
                        {
                            "timestamp": {
                                "order": "desc"
                            }
                        }
                    ],
                    "size": 1
                }
            },
            "first-report": {
                "top_hits": {
                    "sort": [
                        {
                            "timestamp": {
                                "order": "asc"
                            }
                        }
                    ],
                    "size": 1
                }
            },
            "nested-objs": {
                "nested": {
                    "path": "nestedObjs",
                    "inner_hits": {}
                }
            }
        }
    }
}

```

But this fails with:

```
   "error": {
      "root_cause": [
         {
            "type": "search_parse_exception",
            "reason": "Unexpected token START_OBJECT in [nested-objs].",
            "line": 12,
            "col": 45
         }
      ],

```

If I remove the "inner\_hits" field it works ok. But it just gives me the document count and not the documents themselves.

What am I doing wrong?

I have tried this on ES 1.7.1 and 2.0.0

---

<div class="post-metadata">

**Author:** ![voiceofthemany](https://avatars.discourse-cdn.com/v4/letter/v/9de053/32.png) [@voiceofthemany](https://discuss.elastic.co/u/voiceofthemany)\
**Post date:** [November 5, 2015, 9:59am UTC](https://discuss.elastic.co/t/using-inner-hits-inside-an-aggregation/33631/2 "2015-11-05T09:59:41Z")

</div>

Anyone? ☹

---

<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, 11:40pm UTC](https://discuss.elastic.co/t/using-inner-hits-inside-an-aggregation/33631/3 "2017-07-05T23:40:23Z")

</div>


