Format date in elasticsearch query (during retrieval)

I have a elasticsearch index with a field "aDate" (and lot of other fields)
with the following mapping

"aDate" : {
"type" : "date",
"format" : "date_optional_time"}

When i query for a document i get a result like

"aDate" : 1421179734000,

I know this is the epoch, the internal java/elasticsearch date format, but
i want to have a result like:

"aDate" : "2015-01-13T20:08:54",

I play around with scripting

{
"query":{
"match_all":{

}
},
"script_fields":{
"aDate":{
"script":"if (!_source.aDate?.equals('null')) new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss').format(new java.util.Date(_source.aDate));"
}
}}

but it give strange results (script works basically, but aDate is the only
field returned and _source is missing). This looks like

"hits": [{
"_index": "idx1",
"_type": "type2",
"_id": "8770",
"_score": 1.0,
"fields": {
"sentDate": ["2015-01-12T17:15:47"]
}
},

I would prefer a solution without scripting if possible.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/4c2d9986-1ba7-4161-b633-4df9cfd16b10%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

answer is
here http://stackoverflow.com/questions/27931241/format-date-in-elasticsearch-query-during-retrieval/27932753

Am Dienstag, 13. Januar 2015 21:56:36 UTC+1 schrieb Hendrik Dev:

I have a elasticsearch index with a field "aDate" (and lot of other
fields) with the following mapping

"aDate" : {
"type" : "date",
"format" : "date_optional_time"}

When i query for a document i get a result like

"aDate" : 1421179734000,

I know this is the epoch, the internal java/elasticsearch date format, but
i want to have a result like:

"aDate" : "2015-01-13T20:08:54",

I play around with scripting

{
"query":{
"match_all":{

}
},
"script_fields":{
"aDate":{
"script":"if (!_source.aDate?.equals('null')) new java.text.SimpleDateFormat('yyyy-MM-dd\'T\'HH:mm:ss').format(new java.util.Date(_source.aDate));"
}
}}

but it give strange results (script works basically, but aDate is the only
field returned and _source is missing). This looks like

"hits": [{
"_index": "idx1",
"_type": "type2",
"_id": "8770",
"_score": 1.0,
"fields": {
"sentDate": ["2015-01-12T17:15:47"]
}
},

I would prefer a solution without scripting if possible.

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/a6633b58-6b3c-46df-b0fa-373c5f73bb89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.