I am facing a weird issue with Elasticsearch. My mapping specifies that a certain field is of type long
. Now accidentally I was trying to index some documents which
If you set ignore_malformed string to false. It would not index the document if it is malformed but throws an exception. At least in elasticsearch 1.6.0.
Example:
put test
put test/test/_mapping
{
"properties" : {
"title" : {"type" : "string"},
"data" : {"type": "long" ,"ignore_malformed":false}
}
}
put test/test/1
{
"data" : "1",
"title" : "valid coerce string to number"
}
put test/test/2
{
"data" : "hello",
"title" : "invalid number"
}
#Failed Response
{
"error": "MapperParsingException[failed to parse [data]]; nested: NumberFormatException[For input string: \"hello\"]; ",
"status": 400
}
Query with Get fails
get test/test/2
{
"_index": "test",
"_type": "test",
"_id": "2",
"found": false
}
I suspect your mapping looks similar to this:
{
"long_field": {
"type": "long"
}
}
If that's the case, you can set the coerce
flag to false
as it is true by default and will always try to convert strings to numbers and truncate fractions for integers.
{
"long_field": {
"type": "long",
"coerce": false
}
}
If you do this, the next time you try to index a long field as a string, ES will tell you this:
MapperParsingException[failed to parse [long_field]]; nested: IllegalArgumentException[Long value passed as String];