"group_by_name":{ "terms": { "field": "name" }, "aggs": { "group_by_id": { "terms": { "script": "doc['id'].value" } } } }