Set mandatory field in template mapping


(Li Lu) #1

Hi,
I know this is a old topic.
I found the similar question in the following link.
That question was asked in 2013. I just want to confirm the latest elasticsearch 2.3 still does not support setting the mandatory field.
Basically, we want to set some fields as mandatory. It will fail when inserting the document if the field is missing or null.

Thanks !


(Mark Walkom) #2

This should do what you want https://www.elastic.co/guide/en/elasticsearch/reference/2.3/dynamic-field-mapping.html#dynamic-field-mapping


(Li Lu) #3

Thanks Mark !
Could you elaborate on that? I go through the page you gave, but I did not find any related solution there.

For example, the type of id is string, it is a mandatory field.
how to configure it in template mapping?

Thanks !


(Mark Walkom) #4

You need to use templates, with https://www.elastic.co/guide/en/elasticsearch/reference/2.3/dynamic.html and dynamic: strict. Then when you add your mandatory field, documents that don' contain this will be rejected.


(Li Lu) #5

Hi, Mark
Thanks for the reply !
I am little confused there.
on the page, it said:
strict : If new fields are detected, an exception is thrown and the document is rejected.

for example, in the following case, my_index dost NOT allow the new fields to be added in if the new fields are not defined in the mapping before.

PUT my_index
{
"mappings": {
"my_type": {
"dynamic": strict,
"properties": {
"user": {
"properties": {
"name": {
"type": "string"
},

https://www.elastic.co/guide/en/elasticsearch/reference/2.3/dynamic.html

strict is for defining whether it is allowed to add the new fields; it is not for setting up the value of a field is mandatory, right?

I am asking , how to make sure, in my_index/my_type, user name has to have a value.

Thanks !


(Mark Walkom) #6

Oh right. Sorry for the misunderstanding.

Not sure you can do that to be honest, you can set a default value, but I think that is it.


(Li Lu) #7

thanks so much Mark !
yeah, I searched around in the forum, it is impossible to make a field mandatory at least before 2013.
I just want to know whether the new version can.

Thanks !


(Pablo Musa) #8

In the 5.X version you can use the Ingest Node to validate that as follows:

POST _ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "lang": "painless",
          "inline": "if (ctx.name == null) { throw new Exception('Document does not have the *name* field') }"
        }
      }
    ]
  },
  "docs": [
    {
      "_index": "index",
      "_type": "type",
      "_id": "1",
      "_source": {
        "name": "Pablo"
      }
    },
    {
      "_index": "index",
      "_type": "type",
      "_id": "2",
      "_source": {
        "company": "Elastic"
      }
    }
  ]
}

(system) #9