How to get only n documents for each value

Check out the top hits aggregation.

PUT top_hits_example
POST top_hits_example/doc/1
{
  "name": "name1",
  "state": "NY"
}
POST top_hits_example/doc/2
{
  "name": "name2",
  "state": "NY"
}
POST top_hits_example/doc/3
{
  "name": "name3",
  "state": "NY"
}
POST top_hits_example/doc/4
{
  "name": "name4",
  "state": "NJ"
}
POST top_hits_example/doc/5
{
  "name": "name5",
  "state": "NJ"
}
POST top_hits_example/doc/6
{
  "name": "name6",
  "state": "NJ"
}
GET top_hits_example/_search?size=0
{
  "query": {
    "function_score": {
      "random_score": {}
    }
  },
  "aggs": {
    "names": {
      "terms": {
        "field": "state"
      },
      "aggs": {
        "top_tag_hits": {
          "top_hits": {
            "size": 2
          }
        }
      }
    }
  }
}