An easy way to do it is by using script_fields like this:
GET test/_search
{
"script_fields": {
"date_iso": {
"script": {
"source": "doc.date.value"
}
}
}
}
Results =>
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"date" : "1635972005107"
},
"fields" : {
"date_iso" : [
"2021-11-03T20:40:05.107Z"
]
}
}
As you can see, even though the date field is stored in epoch format, the date_iso script field is returned in ISO8601 format.
You can also use the Fields API to return the date with the given format:
GET test/_search
{
"fields": [
{
"field": "date",
"format": "date_time"
}
]
}
Results =>
{
"_index" : "test",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"date" : "1635972005107"
},
"fields" : {
"date" : [
"2021-11-03T20:40:05.107Z"
]
}
}