# How to \_update a non existing field

**URL:** https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349
**Category:** Elasticsearch
**Created:** [April 16, 2012, 1:46pm UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349 "2012-04-16T13:46:31Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Andy\_Wick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andy_wick/32/44017_2.png) [@Andy\_Wick](https://discuss.elastic.co/u/Andy_Wick)
#### Post date: [April 16, 2012, 1:46pm UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349/1 "2012-04-16T13:46:31Z")

</div>

Using ElasticSearch as a DB I'm looking for a way to \_update a field that  
doesn't exist. I don't want to set the field to 0 when first indexed  
because I don't know the field names for other reasons. I assume I can't  
use null\_value in mapping either, since I don't know the field name BEFORE  
I index the document, and setting the null\_value afterwards I assume won't  
help.

curl '[http://localhost:9200/tests/test/test1?pretty](http://localhost:9200/tests/test/test1?pretty)' -d ' { }'

curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
{"script": "ctx.\_source.b += v", "params": {"v": 2}}'

Returns ElasticSearchIllegalArgumentException[failed to execute script];  
nested: PropertyAccessException[[Error: could not access: b; in class:  
java.util.LinkedHashMap]\n[Near : {... ctx.\_source.b+v ....}]

I tried using isdef, but that seems to always return false, I'm probably  
using it wrong.

curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
{"script": "if (isdef ctx.\_source.b) {ctx.\_source.b += v} else  
{ctx.\_source.b = v}", "params": {"v": 2}}'

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 17, 2012, 1:03pm UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349/2 "2012-04-17T13:03:27Z")

</div>

Try and check if \_ctx.\_source.b is null or not

On Mon, Apr 16, 2012 at 4:46 PM, Andy Wick [andywick@gmail.com](mailto:andywick@gmail.com) wrote:

> Using Elasticsearch as a DB I'm looking for a way to \_update a field that  
> doesn't exist. I don't want to set the field to 0 when first indexed  
> because I don't know the field names for other reasons. I assume I can't  
> use null\_value in mapping either, since I don't know the field name BEFORE  
> I index the document, and setting the null\_value afterwards I assume won't  
> help.
> 
> curl '[http://localhost:9200/tests/test/test1?pretty](http://localhost:9200/tests/test/test1?pretty)' -d ' { }'
> 
> curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
> {"script": "ctx.\_source.b += v", "params": {"v": 2}}'
> 
> Returns ElasticSearchIllegalArgumentException[failed to execute script];  
> nested: PropertyAccessException[[Error: could not access: b; in class:  
> java.util.LinkedHashMap]\n[Near : {... ctx.\_source.b+v ....}]
> 
> I tried using isdef, but that seems to always return false, I'm probably  
> using it wrong.
> 
> curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
> {"script": "if (isdef ctx.\_source.b) {ctx.\_source.b += v} else  
> {ctx.\_source.b = v}", "params": {"v": 2}}'

---

<div class="post-metadata">

### Author: ![Andy\_Wick](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andy_wick/32/44017_2.png) [@Andy\_Wick](https://discuss.elastic.co/u/Andy_Wick)
#### Post date: [April 18, 2012, 12:45am UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349/3 "2012-04-18T00:45:32Z")

</div>

Like this? I tried both ctx.\_source.b and \_ctx.\_source.b with the same  
result.

curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
{"script": "if (ctx.\_source.b == null) {ctx.\_source.b += v} else  
{ctx.\_source.b = v}", "params": {"v": 2}}'

{  
"error" :  
"RemoteTransportException[[moloches-mtc04][inet[/172.29.127.44:9300]][update]];  
nested: ElasticSearchIllegalArgumentException[failed to execute script];  
nested: PropertyAccessException[[Error: could not access: b; in class:  
java.util.LinkedHashMap]\n[Near : {... ctx.\_source.b+v ....}]\n  
^\n[Line: 1, Column: 1]]; ",

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [April 19, 2012, 2:32pm UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349/4 "2012-04-19T14:32:29Z")

</div>

Heya, this seems to work:

curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '{"script":  
"if (ctx.\_source.containsKey("b")) {ctx.\_source.b += v} else  
{ctx.\_source.b = v}", "params": {"v": 2}}'

(notes, I used " to escape it the "b" key, you won't need to do it if you  
don't use curl).

On Wed, Apr 18, 2012 at 3:45 AM, Andy Wick [andywick@gmail.com](mailto:andywick@gmail.com) wrote:

> Like this? I tried both ctx.\_source.b and \_ctx.\_source.b with the same  
> result.
> 
> curl '[http://localhost:9200/tests/test/test1/\_update?pretty](http://localhost:9200/tests/test/test1/_update?pretty)' -d '  
> {"script": "if (ctx.\_source.b == null) {ctx.\_source.b += v} else  
> {ctx.\_source.b = v}", "params": {"v": 2}}'
> 
> {  
> "error" :  
> "RemoteTransportException[[moloches-mtc04][inet[/172.29.127.44:9300]][update]];  
> nested: ElasticSearchIllegalArgumentException[failed to execute script];  
> nested: PropertyAccessException[[Error: could not access: b; in class:  
> java.util.LinkedHashMap]\n[Near : {... ctx.\_source.b+v ....}]\n  
> ^\n[Line: 1, Column: 1]]; ",

---

<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 6, 2017, 3:31am UTC](https://discuss.elastic.co/t/how-to--update-a-non-existing-field/7349/5 "2017-07-06T03:31:57Z")

</div>


