Saving a value to a field of a different field type e.g. string to integer doesn't throw an exception?

If I configure a field to be a certain type, for example, an integer, I'm not sure why I can then save a string value to that field? I expect code like this to throw an error:

curl -XPUT 'localhost:9200/my_index?pretty' -H 'Content-Type: application/json' -d'
{
    "mappings" : {
        "my_type": {
            "properties": {
                "keyword_field" : {
                    "type": "keyword"
                },
                "integer_field": {
                    "type": "integer"
                }
            }
        }
    }
}
'

curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "keyword_field": false,
  "integer_field": "10"
}
'

curl -XPUT 'localhost:9200/my_index/my_type/1?pretty' -H 'Content-Type: application/json' -d'
{
  "keyword_field": "text",
  "integer_field": 11
}
'

I thought Elasticsearch might be doing a data conversion on the integer field, but after looking at the saved document it looks like Elasticsearch is saving the string "10"...which would mean numeric comparisons with that field would fail in the future. Not sure what's going on with saving json false to a keyword field.

Thanks,

Troy

Elasticsearch does not alter the source, but as it is able to convert the string to an integer and index this according to the mapping, no error is thrown.

Got it...thanks Christian. Not what I was expecting...especially w/ the boolean value, but the document is findable using "10", 10, "false" or false, so at least I can get the doc.

On one hand this seems helpful as it allows some latitude for getting data into elasticsearch, but on the other hand it seems like it could introduce errors when the document is brought into the application and the _source value isn't the correct data type. With Python:

r = requests.get('http://localhost:9200/my_index/my_type/1').json()
type(r['_source']['integer_field'])
<type 'unicode'>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.