Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a mapping in elasticsearch that doesn't accept fields other that the mapped ones?

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???

like image 439
gotch4 Avatar asked Aug 27 '13 17:08

gotch4


People also ask

Can Elasticsearch index have multiple mappings?

From Elasticsearch version 6.0 by default index doesn't allow multiple types per index. The better option is to always have one document type per index. The single responsibility of the index is to maintain the single document type/mapping type per index.

Can I change mapping for existing index Elasticsearch?

It is not possible to update the mapping of an existing field. If the mapping is set to the wrong type, re-creating the index with updated mapping and re-indexing is the only option available. In version 7.0, Elasticsearch has deprecated the document type and the default document type is set to _doc.

How do I capture a specific field in Elasticsearch?

There are two recommended methods to retrieve selected fields from a search query: Use the fields option to extract the values of fields present in the index mapping. Use the _source option if you need to access the original data that was passed at index time.


1 Answers

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".

like image 114
robhudson Avatar answered Nov 04 '22 12:11

robhudson