Reputation: 844
I'm using below code to insert some documents in elasticsearch index. But when inserting in the es the time is coming as UTC format rather than original GMT+5:30Z format. Where as the Sysout before indexing is giving me correct format as 2014-08-11T18:23:13.447+05:30 . I want the original timestamp for analytics purpose. Please let me know how to keep the orizinal time format in document
public static IndexResponse addDocumentsQuota(Client client, String index,
String type, String categoryName) {
IndexResponse indexResponse = null;
DateTime date = new DateTime();
System.out.println(date);
try {
indexResponse = client.prepareIndex(index, type)
.setSource(jsonBuilder()
.startObject()
.field("@timestamp", date)
.field("category_name", categoryName)
.field("alert_message", "alert message")
.field("creation_time", date)
.endObject())
.execute()
.actionGet();
} catch (ElasticsearchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return indexResponse;
}
After indexing:
"_source": {
"@timestamp": "2014-08-11T11:57:59.839Z",
"category_name": "newCat1",
"alert_message": "alert message",
"creation_time": "2014-08-11T11:57:59.852Z"
}
Thanks, Subhadip
Upvotes: 1
Views: 4888
Reputation: 5924
Set the date format in the mapping, i.e.
{
"yourType": {
"properties": {
"creation_time": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZZ"
}
}
}
}
Upvotes: 0
Reputation: 17155
If you want to retain specific date formatting in your _source
document, you'll need to pass the date into the .field
as a String
. Since you've said the System.out.println()
prints it in the format you want, you simply need to change to:
...
.field("creation_time", date.toString())
...
In any event internally, ES stores the timestamps in GMT.
Upvotes: 2