# Mappings get messed up - Any way to enforce mappings

**URL:** <https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449>\
**Category:** Elasticsearch\
**Created:** [August 23, 2019, 5:54am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449 "2019-08-23T05:54:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cawoodm/32/14083_2.png) [@cawoodm](https://discuss.elastic.co/u/cawoodm)\
**Post date:** [August 23, 2019, 5:54am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/1 "2019-08-23T05:54:59Z")

</div>

I create my indices with a nice tidy mapping where most fields are keywords but, over time, the mapping mutates into a monster of text fields with a ".keyword" property. How can this happen and is there any way to stop it.

I start with:

```
{
    "logs-prod3": {
        "mappings": {
            "properties": {
                "category": {"type": "keyword"},
                "contact": {"type": "keyword"},
                ...

```

Then I re-index my data to that index and it mutates into:

```
{
"logs-prod3": {
    "mappings": {
        "properties": {
            "category": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "contact": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            ...

```

Why is this happening?

How can I enforce a mapping - preferably reject any data not defined by the mapping!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 23, 2019, 5:56am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/2 "2019-08-23T05:56:58Z")

</div>

Create an [index template](https://www.elastic.co/guide/en/elasticsearch/reference/7.3/indices-templates.html) with the settings and mappings you want to apply to all matching indices.

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cawoodm/32/14083_2.png) [@cawoodm](https://discuss.elastic.co/u/cawoodm)\
**Post date:** [August 23, 2019, 6:41am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/3 "2019-08-23T06:41:25Z")

</div>

According to the docs "templates are only applied at index creation time". Since I create the indices myself with the correct mapping and they get corrupted later I don't see how this can help. Indeed ES Docs claim mappings cannot be changed which makes me wonder how this happens at all.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 23, 2019, 6:45am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/4 "2019-08-23T06:45:09Z")

</div>

You can disable dynamic mappings, but that will cause documents with new fields to fail.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [August 23, 2019, 6:46am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/5 "2019-08-23T06:46:21Z")

</div>

The second index has a different name so does not get the settings from the first. This is where an index template is useful.

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cawoodm/32/14083_2.png) [@cawoodm](https://discuss.elastic.co/u/cawoodm)\
**Post date:** [August 23, 2019, 7:16am UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/6 "2019-08-23T07:16:47Z")

</div>

The mismatch of names is just a typo (copy-paste error) posting there. I created the indices with the keyword mapping, verified this and then re-indexed.

Thanks for the tip about dynamic mappings - sounds like exactly what I want!

---

<div class="post-metadata">

**Author:** ![cawoodm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cawoodm/32/14083_2.png) [@cawoodm](https://discuss.elastic.co/u/cawoodm)\
**Post date:** [August 27, 2019, 12:41pm UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/7 "2019-08-27T12:41:45Z")

</div>

btw. if I disable dynamic mappings, then add data with new unmapped fields... these are not indexed but stored in source. Can I update the mapping on the index and reindex the new \_source data within the same index? I assume I have to make a new index but just checking...

---

<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:** [September 24, 2019, 12:42pm UTC](https://discuss.elastic.co/t/mappings-get-messed-up-any-way-to-enforce-mappings/196449/8 "2019-09-24T12:42:03Z")

</div>

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