# Set mandatory field in template mapping

**URL:** <https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100>\
**Category:** Elasticsearch\
**Created:** [May 3, 2016, 8:07pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100 "2016-05-03T20:07:55Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lilu1](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lilu1](https://discuss.elastic.co/u/lilu1)\
**Post date:** [May 3, 2016, 8:07pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/1 "2016-05-03T20:07:55Z")

</div>

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.

> [@Possibility to constrain certain field(s) as mandatory field(s)](https://discuss.elastic.co/t/possibility-to-constrain-certain-field-s-as-mandatory-field-s/11975):
>
> Hi All, We are using elastic-search for our project. We would like to implement a feature in our project using elastic search. We want to make sure that all the saved documents in our project must contain certain field(s) e.g. { USER\_ID:"123" USER\_NAME:"Ashutosh" } In other words, if the document does not contain the mandatory fields e.g. USER\_ID, it will not be saved. It will be helpful, if you share your ideas whether it is possible to define mandatory filed(s) of a document in ela…

Thanks !

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 4, 2016, 12:47am UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/2 "2016-05-04T00:47:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![lilu1](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lilu1](https://discuss.elastic.co/u/lilu1)\
**Post date:** [May 4, 2016, 4:25pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/3 "2016-05-04T16:25:26Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 4, 2016, 9:21pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/4 "2016-05-04T21:21:03Z")

</div>

You need to use templates, with [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/dynamic.html](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.

---

<div class="post-metadata">

**Author:** ![lilu1](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lilu1](https://discuss.elastic.co/u/lilu1)\
**Post date:** [May 4, 2016, 9:45pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/5 "2016-05-04T21:45:15Z")

</div>

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](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 !

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [May 4, 2016, 11:28pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/6 "2016-05-04T23:28:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lilu1](https://avatars.discourse-cdn.com/v4/letter/l/a4c791/32.png) [@lilu1](https://discuss.elastic.co/u/lilu1)\
**Post date:** [May 4, 2016, 11:42pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/7 "2016-05-04T23:42:05Z")

</div>

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 !

---

<div class="post-metadata">

**Author:** ![pmusa](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pmusa/32/3498_2.png) [@pmusa](https://discuss.elastic.co/u/pmusa)\
**Post date:** [December 13, 2016, 2:03pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/8 "2016-12-13T14:03:01Z")

</div>

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"
      }
    }
  ]
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:05pm UTC](https://discuss.elastic.co/t/set-mandatory-field-in-template-mapping/49100/9 "2017-07-05T22:05:01Z")

</div>


