playframework-elasticsearch icon indicating copy to clipboard operation
playframework-elasticsearch copied to clipboard

Unparseable date. java.utils.Date could not be parsed back.

Open vkolotov opened this issue 13 years ago • 5 comments

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
}

}

vkolotov avatar Nov 28 '11 08:11 vkolotov

Which version of the module are you using?

bgooren avatar Nov 28 '11 12:11 bgooren

I tried both versions: head and 0.3.

vkolotov avatar Nov 28 '11 13:11 vkolotov

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?

bgooren avatar Nov 28 '11 23:11 bgooren

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)

bgooren avatar Dec 05 '11 10:12 bgooren

Ok, I have fixed this by using the date formatter used by elastic search.

bgooren avatar Dec 05 '11 12:12 bgooren