playframework-elasticsearch
playframework-elasticsearch copied to clipboard
Unparseable date. java.utils.Date could not be parsed back.
For some reason date filed could not be parsed back from elastic DB to an entity which has this date field.
I have very straightforward entity like:
@Entity @ElasticSearchable class Article extends Model {
.... public String title; .... public Date issueDate;
}
Indexing of this class performs well. But when you make query you've got following exception:
java.text.ParseException: Unparseable date: "2007-01-01T00:00:00.000Z" at java.text.DateFormat.parse(DateFormat.java:337) at play.modules.elasticsearch.util.ReflectionUtil.getDate(ReflectionUtil.java:520) at play.modules.elasticsearch.util.ReflectionUtil.convertToDate(ReflectionUtil.java:502) at play.modules.elasticsearch.util.ReflectionUtil.setFieldValue(ReflectionUtil.java:367) at play.modules.elasticsearch.util.ReflectionUtil.setFieldValue(ReflectionUtil.java:339) at play.modules.elasticsearch.transformer.Transformer.toSearchResults(Transformer.java:68) at play.modules.elasticsearch.Query.fetch(Query.java:173) at play.modules.elasticsearch.ElasticSearch.search(ElasticSearch.java:145) at play.modules.elasticsearch.ElasticSearch.search(ElasticSearch.java:96) at utils.SimpleQueryBuilderTest.testShouldAndMust(SimpleQueryBuilderTest.java:64)
The entity is stored in elastic DB like that:
{
_index: models_article
_type: models_article
_id: 11
_score: 1
_source: {
identifier: 11_article
title: very test 11 article title
body: <div>the very 11 article body</div>
issueDate: 2011-11-22T00:00:00.000Z
section: 11 section
coverStory: true
authors: [
Billy Gates
]
editions: [ ]
photos: [ ]
id: 11
}
}
Which version of the module are you using?
I tried both versions: head and 0.3.
Ok, just to log the result of my short search for the problem:
- apparently the default format of elastic search is dateOptionalTime (http://www.elasticsearch.org/guide/reference/mapping/root-object-type.html)
- according to http://elasticsearch-users.115913.n3.nabble.com/Mapped-date-field-returned-as-string-td2812934.html the date is returned as a string in this format too
- the date format currently used in ReflectionUtil is MM/dd/yyyy HH:mm:ss.SSS and should be yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
Felipe: any idea if the date format was changed in elasticsearch? I assume you chose this date format for a reason?
With 0.3 I cannot index a date field without an exception, so let me fix that first. I'm pretty sure it will also fix your issue.
- Ok, that had to do with a problem in ES, fixed it by upgrading to at least 0.18.3 (see https://github.com/elasticsearch/elasticsearch/issues/1465)
Ok, I have fixed this by using the date formatter used by elastic search.