Return documents with Aggregations

I'm trying to create a query, which will use functions aggregations, such as min, max, avg etc, which will
return the document itself, or at least its _id.

for example, when i'm running:

  "query": {
    "filtered": {
      "filter": {}
    }
  },
  "aggs": {
    "group_by_id": {
      "terms": {
        "field": "EntityId",
        "size": 1000
      },
      "aggs": {
        "max": {
          "max": {
            "field": "timestamp"
          }
        }
      }
    }
  }

the result is something like:
"key: -entityID-"
"doc_count: "
"max : { value: 22/11/2015"

I need to return the document which contains the max timestamp, so i need to get the document id.
i tried to use the top hits, but i can't use functions there...

so is there any valid solution for this issue?