Bug with field mappings?


(Peter Ponzel) #1

hello,

I just came across a strange behavior regarding field mappings and wanted to ask the professional, if that is really intended.
I indexed a document to a newly created index that didn't had any mappings.
Then of course due to the dynamic mappings, all relevant fields mappings where created.
In my case it was field of type text.
By accident I created new documents that had a different type for that field, in this case float.
And elasticsearch was not complaining about it.
So I did a test and also tried boolean and even an array of text.
Non of them failed.. elasticsearch just accepted whatever was coming.
I also tried a new object which luckily failed, but still.. the mapping seems a little broken to me or is this really intended ?
Btw. the other way around didn't work.. creating the initial mapping as float for instance wouldn't allow a text afterwards.

The dynamically created mapping looks like:

"mappings": {
"test": {
    "properties": {
        "id": {
            "type": "keyword"
        },
        "meta": {
            "properties": {
                "foo": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

The response, when queried my index was:

hits": [
    {
        "_index": "test",
        "_type": "test",
        "_id": "3eee04e3-aaed-4a47-b013-e6a3ca408f2b",
        "_score": 1.0,
        "_source": {
            "id": "3eee04e3-aaed-4a47-b013-e6a3ca408f2b",
            "meta": {
                "foo": true
            }
        }
    },
    {
        "_index": "test",
        "_type": "test",
        "_id": "74229be1-99bf-44bf-a811-08b30e4db346",
        "_score": 1.0,
        "_source": {
            "id": "74229be1-99bf-44bf-a811-08b30e4db346",
            "meta": {
                "foo": "bar"
            }
        }
    },
    {
        "_index": "test",
        "_type": "test",
        "_id": "df68d464-f1ac-4cce-8e69-ff08f763de45",
        "_score": 1.0,
        "_source": {
            "id": "df68d464-f1ac-4cce-8e69-ff08f763de45",
            "meta": {
                "foo": [
                    ""
                ]
            }
        }
    },
    {
        "_index": "test",
        "_type": "test",
        "_id": "cf837e2c-b74d-4391-9963-00f3bf689c95",
        "_score": 1.0,
        "_source": {
            "id": "cf837e2c-b74d-4391-9963-00f3bf689c95",
            "meta": {
                "foo": 1.1
            }
        }
    }
]

Interesting is also, that I can do all kinds of search operations, such as

  • ?q=meta.foo:true
  • ?q=meta.foo:>5
  • ?q=meta.foo:[1 TO 9]
  • ?q=meta.foo:bar

I've been using elasticsearch 6.0.1

Looking forward to hear from you guys.

Best regards,
Peter


(Peter Ponzel) #2

can anyone help me understand this, please ?


(Abdon Pijpelink) #3

The mapping only defines how Elasticsearch will treat the field when it gets indexed. When a document arrives with a value of another data type, Elasticsearch will try to cast that value to the correct type. Sometimes that works (for example, true and 1.1 can be easily cast to a string). Sometimes it does not (for example casting "abc" to a float will fail).

The _source that Elasticsearch returns with the hits will contain your original document - none of this casting will apply to what you see in the _source.

If you only want to accept document with the correct datatypes, you could set coerce to false in your mapping.


(Peter Ponzel) #4

Thank you very much for clarifying this.


(system) #5

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