2

I get the following exception when i try to index a document.

Caused by: org.elasticsearch.index.mapper.MapperParsingException: failed to parse date field [J], tried both date format [dateOptionalTime], and timestamp number with locale []

and that is all correct, but is it possible to ignore the exception and index the rest of the fields?

2 Answers 2

9

I think this can be done without re-indexing by adding the "ignore_malformed" flag to the mapping for the field. Here's what I tried and it worked:

Create new index with a "date" field

POST events 
{
  "mappings" : {
    "dates" : {
      "properties" : {
        "lenient_date" : {
          "type" : "date"
        }
      }
    }
  }
}

Try adding an malformed date value

PUT events/dates/1
{
  "lenient_date" : "1/32/2014"
}

Result: parsing error (as expected)

{
   "error": "MapperParsingException[failed to parse [lenient_date]]; nested: MapperParsingException[failed to parse date field [1/32/2014], tried both date format [dateOptionalTime], and timestamp number with locale []]; nested: IllegalArgumentException[Invalid format: \"1/32/2014\" is malformed at \"/32/2014\"]; ",
   "status": 400
}

GET events/dates/1 Result: date not found (as expected)

{
   "_index": "events",
   "_type": "dates",
   "_id": "1",
   "found": false
}

Change mapping to allow malformed values

PUT events/dates/_mapping
{
  "dates" : {
    "properties" : {
      "lenient_date" : {
        "type" : "date",
        "ignore_malformed" : true
      }
    }
  }
}

Try adding the same bad date

PUT events/dates/1
{
  "lenient_date" : "1/32/2014"
}
GET events/dates/1
Result: now works
{
   "_index": "events",
   "_type": "dates",
   "_id": "1",
   "_version": 1,
   "found": true,
   "_source": {
      "lenient_date": "1/32/2014"
   }
}

Note:

I'm running ES version 1.3.2. I have not checked how the updated mapping affects sorting and filtering.

Sign up to request clarification or add additional context in comments.

Comments

1

Actually, you get this exception either because you mapped this field as a date or ElasticSearch dynamically identified this field as a date (the first document you used to create the index had a date value for this field).

You can't ignore this exception and index the other information as the document doesn't match the defined mapping.

There are several cases :

  • In fact, you don't have date values in this field : you can fix the mapping to what you really need.

  • You have date values in this field, but it's not a valid date according to the elasticsearch date formats : you can add this format to the one used by default (using dynamic_date_formats) or define the specific format of this field.

  • You have sometimes date values, sometimes string values : if it suits your need, you could set the mapping to string to prevent this exception from occuring. You won't get parsing exceptions as it won't get parsed as a date but it won't be possible to perform a range query on this field for example.

1 Comment

Hoped i did't had to change the mapping and just make elastic ignore it automaticly when i couldn't parse it. Actualy changed the mapping this morning and reindex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.