# Bug with field mappings?

**URL:** <https://discuss.elastic.co/t/bug-with-field-mappings/139735>\
**Category:** Elasticsearch\
**Created:** [July 12, 2018, 11:08am UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735 "2018-07-12T11:08:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![peter-ponzel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter-ponzel/32/28664_2.png) [@peter-ponzel](https://discuss.elastic.co/u/peter-ponzel)\
**Post date:** [July 12, 2018, 11:08am UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735/1 "2018-07-12T11:08:24Z")

</div>

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:

```auto
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

---

<div class="post-metadata">

**Author:** ![peter-ponzel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter-ponzel/32/28664_2.png) [@peter-ponzel](https://discuss.elastic.co/u/peter-ponzel)\
**Post date:** [July 13, 2018, 7:22am UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735/2 "2018-07-13T07:22:06Z")

</div>

can anyone help me understand this, please ?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [July 19, 2018, 2:32pm UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735/3 "2018-07-19T14:32:25Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/coerce.html).

---

<div class="post-metadata">

**Author:** ![peter-ponzel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter-ponzel/32/28664_2.png) [@peter-ponzel](https://discuss.elastic.co/u/peter-ponzel)\
**Post date:** [July 20, 2018, 6:31am UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735/4 "2018-07-20T06:31:22Z")

</div>

Thank you very much for clarifying this.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 17, 2018, 6:31am UTC](https://discuss.elastic.co/t/bug-with-field-mappings/139735/5 "2018-08-17T06:31:24Z")

</div>

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