How to define a mapping in elastic search that does not accept fields other than mappings?

advertisements

Ok, in my elastisearch I am using the following mapping for an index:

{

    "mappings": {
        "mytype": {
            "type":"object",
            "dynamic" : "false",
            "properties": {
                "name": {
                    "type": "string"
                },
                "address": {
                    "type": "string"
                },
                "published": {
                    "type": "date"
                }

            }
        }
    }
}

it works. In fact if I put a malformed date in the field "published" it complains and fails. Also I've the following configuration:

...
node.name : node1
index.mapper.dynamic : false
index.mapper.dynamic.strict : true
...

And without the mapping, I can't really use the type. The problem is that if I insert something like:

{ "name":"boh58585", "address": "hiohio", "published": "2014-4-4", "test": "hophiophop" }

it will happily accept it. Which is not the behaviour that I expect, because the field test is not in the mapping. How can I restrict the fields of the document to only those that are in the mapping???


The use of "dynamic": false tells Elasticsearch to never allow the mapping of an index to be changed. If you want an error thrown when you try to index new documents with fields outside of the defined mapping, use "dynamic": "strict" instead.

From the docs: "The dynamic parameter can also be set to strict, meaning that not only new fields will not be introduced into the mapping, parsing (indexing) docs with such new fields will fail."

Since you've defined this in the settings, I would guess that leaving out the dynamic from the mapping definition completely will default to "dynamic": "strict".