I want to group by sysCode by max date that is get the latest documents for each sysCode and then aggregate on employeeId and type field.
The below query does not return me desired results, for GER it returns employeeId=1 and for IND it returns employeeId=3 which I do not want.
sample json document
{
"sysCode": "GER",
"employeeId": 1,
"date": "2014-06-14",
"categories": {
"pb": [
{
"metric": "OVERDUE",
"type": "LATE"
}
]
}
}
{
"sysCode": "GER",
"employeeId": 2,
"date": "2014-06-15",
"categories": {
"pb": [
{
"metric": "OVERDUE",
"type": "LATE"
}
]
}
}
{
"sysCode": "IND",
"employeeId": 3,
"date": "2014-06-16",
"categories": {
"pb": [
{
"metric": "OVERDUE",
"type": "LATE"
}
]
}
}
{
"sysCode": "IND",
"employeeId": 3,
"date": "2014-06-16",
"categories": {
"pb": [
{
"metric": "OVERDUE",
"type": "MISSED"
}
]
}
}
aggregation query
{
"aggs": {
"result_by_sys_code": {
"terms": {
"field": "sysCode"
},
"aggs": {
"max_as_of_date": {
"max": {
"field": "date"
}
},
"employees": {
"terms": {
"field": "employeeId"
},
"aggs": {
"nested": {
"nested": {
"path": "categories.pb"
},
"aggs": {
"metrics": {
"terms": {
"field": "categories.pb.type.keyword"
}
}
}
}
}
}
}
}
}
}
mappings
{
"mappings": {
"properties": {
"date": {
"type": "date"
},
"categories": {
"properties": {
"pb": {
"type": "nested",
"properties": {
"metric": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"type": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
},
"controlCode": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}