I'm attempting to index data in elasticsearch that includes dates with time zones.
My date mapping is:
"date": {
"type": "date",
"format": "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
}
I use the following code to index the data:
client.prepareIndex(INDEX, TYPE, id))
.setSource(gson.toJson(object), XContentType.JSON)
.setRefreshPolicy(RefreshPolicy.IMMEDIATE)
.get();
I've created my own ZonedDateTime adapter as follows:
public class ZonedDateTimeAdapter implements JsonSerializer<ZonedDateTime> {
public JsonElement serialize(ZonedDateTime date, Type typeOfSrc, JsonSerializationContext context) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
return new JsonPrimitive(date.format(formatter));
}
}
The resulting date is like 2005-01-01T13:35:50.596-0500
. Which to me seems to match my format of yyyy-MM-dd'T'HH:mm:ss.SSSZ
. However I receive an error stating:
Exception in thread "main" MapperParsingException[failed to parse [startDate]]; nested: IllegalArgumentException[Invalid format: "2005-01-01T13:35:50.596-0500" is malformed at ".596-0500"]
What's interesting is that if I change format in ZonedDateTimeAdapter to read yyyy-MM-dd'T'HH:mm:ss.000Z
(forcing the second fraction to always be 000) I get a resulting date like 2005-01-01T13:31:06.000-0500
. An object with this date is successfully indexed into elasticsearch.
Do you know why 2005-01-01T13:31:06.000-0500
index successfully but 2005-01-01T13:35:50.596-0500
does not? Aren't these the same format?