Dynamic mapping strict to true

I have a index template that I define with strict dynamic mapping.

Now I would like to add a field I have remove template, recreated with dynamic=true but it still gives me error on adding field

'error': {'type': 'strict_dynamic_mapping_exception', 'reason': 'mapping set to strict, dynamic introduction of [field1] within [_doc] is not allowed'},

But when I go to kibana and check index template it is set to dynamic=true.

it does gives me same error even when I don't have any template set (when I remove it)

how do I add a field?

It'd be useful if you could share an example to replicate what you are seeing.

First I created index pattern with this

PUT _index_template/hou_jobs
{
  "index_patterns": ["hou_jobs-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas" : "1"
    },
    "mappings": {
      "dynamic": "strict",
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "elm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "job": {
          "type": "long"
        },

Then I am trying to add a field called field1 but it won't allow.
hence I did two different test. first remove this mapping and it give me above error.
then I did create mapping again with dynamic=true and added this field1 in to mix.
it still gives me same error

PUT _index_template/hou_vpmh_jobs_elm
{
  "index_patterns": ["hou_vpmh_jobs_elm-*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas" : "1"
    },
    "mappings": {
      "dynamic": "true",
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "elm": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "job": {
          "type": "long"
        },
        "field1:{
           "type": "boolean"
         },

Here's what I did;

PUT test
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": "1"
  },
  "mappings": {
    "dynamic": "true",
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "number": {
        "type": "long"
      },
      "word": {
        "type": "keyword"
      }
    }
  }
}

And then to test it;

POST test/_doc/1
{
  "number": 5,
  "word": "test",
  "extrafield": "yes"
}
{
  "_index" : "test",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

I can't seem to replicate what you are seeing with this.

Sorry I might not have explain properly
I am talking about creating template

Here is exactly what happens. using your example. it must be some bug or I am doing something wrong

Works - Created template

PUT _index_template/test
{
  "index_patterns": ["test*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": "1"
    },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "number": {
        "type": "long"
      },
      "word": {
        "type": "keyword"
      }
    }
  }
}
}

Works - Put one record to index

POST test/_doc/1
{
  "number": 5,
  "word": "test"
}

Failed as it suppose to fail, because I don't have extrafield define.
works as expected

POST test/_doc/1
{
  "number": 5,
  "word": "test",
  "extrafield": "yes"
}

Now change template to dynamic=true, and added new field called extrafield

PUT _index_template/test
{
  "index_patterns": ["test*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": "1"
    },
  "mappings": {
    "dynamic": "true",
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "number": {
        "type": "long"
      },
      "word": {
        "type": "keyword"
      },
      **"extrafield"**:{
        "type": "boolean"
      }
    }
  }
}
}

This should work Right? because I have extrafield define and mapping dynamic = true but it fails

POST test/_doc/1
{
  "number": 5,
  "word": "test",
  "extrafield": "yes"
}

ERROR, Even though dynamic=true.

GET _index_template/test  --> this tells me dynamic=true

{
  "error" : {
    "root_cause" : [
      {
        "type" : "strict_dynamic_mapping_exception",
        "reason" : "mapping set to strict, dynamic introduction of [extrafield] within [_doc] is not allowed"
      }
    ],
    "type" : "strict_dynamic_mapping_exception",
    "reason" : "mapping set to strict, dynamic introduction of [extrafield] within [_doc] is not allowed"
  },
  "status" : 400
}

I just did the same thing, but added a DELETE _index_template/test before I put the second template, and it worked fine.

it is so weired I still get same failed result.

I am using 7.12.0

Also test on 7.10.2 and same here is exact sequence

GET _index_template/test

GET test/_search

PUT _index_template/test
{
  "index_patterns": ["test*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": "1"
    },
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "number": {
        "type": "long"
      },
      "word": {
        "type": "keyword"
      }
    }
  }
}
}

POST test/_doc/1
{
  "number": 5,
  "word": "test"
  
}

DELETE _index_template/test


PUT _index_template/test
{
  "index_patterns": ["test*"],
  "template": {
    "settings": {
      "number_of_shards": 1,
      "number_of_replicas": "1"
    },
  "mappings": {
    "dynamic": "true",
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "number": {
        "type": "long"
      },
      "word": {
        "type": "keyword"
      },
      "extrafield":{
        "type": "keyword"
      }
    }
  }
}
}

Failed

POST test/_doc/2
{
  "number": 6,
  "word": "test1",
  "extrafield": "test5"
}

I jut tried and it worked fine.
I don't have 7.10, but 7.12 works.

is this relate to this issue?
but again 7.10.2 cluster I have not run any upgrade yet.

I also test this on fresh 7.12 install and one with upgrade install. both same error. hmm..

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