# How to add a new field with a default value to all docs in an index?

**URL:** <https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555>\
**Category:** Elasticsearch\
**Created:** [November 6, 2018, 1:46pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555 "2018-11-06T13:46:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elhamroshan](https://avatars.discourse-cdn.com/v4/letter/e/4af34b/32.png) [@elhamroshan](https://discuss.elastic.co/u/elhamroshan)\
**Post date:** [November 6, 2018, 1:46pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/1 "2018-11-06T13:46:20Z")

</div>

Hi,

I need to add a new field with null default value to all documents in an index.  
I know the following query, but how to add a default value to it?

PUT customer/\_mapping/\_doc  
{  
"properties": {  
"major": {  
"type": "keyword"  
}  
}  
}

Best regards,  
Ellie

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2018, 2:06pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/2 "2018-11-06T14:06:05Z")

</div>

I believe it's useless as providing a null value does not add any value...  
Why not just updating the schema as you did?

---

<div class="post-metadata">

**Author:** ![elhamroshan](https://avatars.discourse-cdn.com/v4/letter/e/4af34b/32.png) [@elhamroshan](https://discuss.elastic.co/u/elhamroshan)\
**Post date:** [November 6, 2018, 2:21pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/3 "2018-11-06T14:21:09Z")

</div>

Because later I want to do an update and change those values that are NULL to something else like:

POST customer/\_doc/\_update\_by\_query  
{  
"script": {  
"inline": "ctx.\_source.major = 'student'"  
},  
"query":  
{"bool":  
{"must": {"match\_all": {}},  
"filter": {"term": {"major": "NULL"}}  
}  
},  
"size": 1  
}  
}

and this query does not work.

Thought maybe that's because the value of the new field is not NULL? you have any other ideas?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [November 6, 2018, 2:33pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/4 "2018-11-06T14:33:02Z")

</div>

Can't you use a bool -\> must\_not -\> exist query?

---

<div class="post-metadata">

**Author:** ![elhamroshan](https://avatars.discourse-cdn.com/v4/letter/e/4af34b/32.png) [@elhamroshan](https://discuss.elastic.co/u/elhamroshan)\
**Post date:** [November 6, 2018, 2:54pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/5 "2018-11-06T14:54:11Z")

</div>

Yes I can 🙂

Thanks for the tip!

/Ellie

---

<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:** [December 4, 2018, 2:54pm UTC](https://discuss.elastic.co/t/how-to-add-a-new-field-with-a-default-value-to-all-docs-in-an-index/155555/6 "2018-12-04T14:54:23Z")

</div>

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