PUT mapping doesn't actually replace the mapping but only "updates" it with new fields

good morning,

I have elasticsearch 7.17. I noticed that if I have a mapping in an existing index and then i use PUT mapping, it doesn’t replace the mapping with the new one as I would expect, but simply updates it with the new keys. You can find below a way to reproduce the error. I would expect that the final GET returns the last schema I put but that’s not the case

PUT my-index-000001

PUT /my-index-000001/_mapping
{
  "dynamic": "strict",
  "properties": {
    "main_property": {
      "properties": {
        "old_property": {
          "type": "long"
        }
      }
    }
  }
}

PUT /my-index-000001/_mapping
{
  "dynamic": "strict",
  "properties": {
    "main_property": {
      "properties": {
        "new_property_1": {
          "properties": {
            "sub_property": {
              "type": "long"
            }
          }
        },
        "new_property_2": {
          "properties": {
            "sub_property": {
              "type": "long"
            }
          }
        }
      }
    }
  }
}

GET my-index-000001

if this method doesn’t work, how can I replace a mapping?

You can not change or remove existing mappings in an index in Elasticsearch, only add new ones. If you do need to remove something you will need to create a new index with the correct mapping and reindex the data.

1 Like