# Using doc\_value returns error with update and \_update\_by\_query in elasticsearch :: ScriptException\[runtime error\]; nested: NullPointerException (Elasticsearch 6.4)

**URL:** <https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958>\
**Category:** Kibana\
**Created:** [September 26, 2018, 8:40am UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958 "2018-09-26T08:40:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rahul\_Ranjan1](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@Rahul\_Ranjan1](https://discuss.elastic.co/u/Rahul_Ranjan1)\
**Post date:** [September 26, 2018, 8:40am UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/1 "2018-09-26T08:40:33Z")

</div>

The update gets committed when I use this query.

> POST my\_collegues\_names/\_update\_by\_query?error\_trace=true  
> {  
> "script": {  
> "lang": "painless",  
> "source": "ctx.\_source.age +=params.last",  
> "params": {  
> "last": 5  
> }  
> },  
> "query": {  
> "match": {  
> "\_id": 21  
> }  
> }  
> }

but I get error when I try to run this query.

> POST my\_collegues\_names/\_update\_by\_query?error\_trace=true  
> {  
> "script\_fields":{  
> "scriptname":{  
> "script": {  
> "lang": "painless",  
> "source": "doc['age']+=params.last",  
> "params": {  
> "last": 20  
> }  
> }  
> }  
> },  
> "query": {  
> "match": {  
> "\_id": 21  
> }  
> }  
> }

Error\_trace  
ScriptException[runtime error]; nested: NullPointerException;\r\n\tat org.elasticsearch.painless.PainlessScript.convertToScriptException(PainlessScript.java:94)\r\n\tat org.elasticsearch.painless.PainlessScript$Script.execute(doc['age']= doc['age'] +params.last:4)\r\n\tat org.elasticsearch.painless.ScriptImpl.run(ScriptImpl.java:105)\r\n\tat org.elasticsearch.index.reindex.AbstractAsyncBulkByScrollAction$ScriptApplier.apply(AbstractAsyncBulkByScrollAction.java:819)\r\n\tat org.elasticsearch.index.reindex.AbstractAsyncBulkByScrollAction$ScriptApplier.apply(AbstractAsyncBulkByScrollAction.java:768)\r\n\tat org.elasticsearch.index.reindex.AbstractAsyncBulkByScrollAction.buildBulk(AbstractAsyncBulkByScrollAction.java:216)\r\n\tat org.elasticsearch.index.reindex.AbstractAsyncBulkByScrollAction.prepareBulkRequest(AbstractAsyncBulkByScrollAction.java:326)\r\n\tat org.elasticsearch.index.reindex.AbstractAsyncBulkByScrollAction$1.doRun(AbstractAsyncBulkByScrollAction.java:289)\r\n\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723)\r\n\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)\r\n\tat org.elasticsearch.index.reindex.WorkerBulkByScrollTaskState$RunOnce.doRun(WorkerBulkByScrollTaskState.java:321)\r\n\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)\r\n\tat org.elasticsearch.index.reindex.WorkerBulkByScrollTaskState$DelayedPrepareBulkRequest$1.doRun(WorkerBulkByScrollTaskState.java:257)\r\n\tat org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723)\r\n\tat org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)\r\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\r\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\r\n\tat java.lang.Thread.run(Thread.java:748)\r\nCaused by: java.lang.NullPointerException\r\n\tat org.elasticsearch.painless.PainlessScript$Script.execute(doc['age']= doc['age'] +params.last:17)\r\n\t... 16 more\r\n"

I am not able to understand why can't I use doc['age'] in my script.

Please help

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [September 26, 2018, 4:54pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/2 "2018-09-26T16:54:05Z")

</div>

Hi, is `age` a numeric field? You will want to use `doc['age'].value` ([documentation](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-expression.html#_numeric_field_api)).

---

<div class="post-metadata">

**Author:** ![Rahul\_Ranjan1](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@Rahul\_Ranjan1](https://discuss.elastic.co/u/Rahul_Ranjan1)\
**Post date:** [September 26, 2018, 5:04pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/3 "2018-09-26T17:04:27Z")

</div>

Yes Jen,  
age is an integer type.  
I have used doc['age'].value but still get an error.  
I have also modified the expression like

> "source":"doc['age'].value =doc['age'].value+params.last",

but still facing the error

> {  
> "error": {  
> "root\_cause": [  
> {  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "doc['age'].value =doc['age'].value+params.last",  
> " ^---- HERE"  
> ],  
> "script": "doc['age'].value =doc['age'].value+params.last",  
> "lang": "painless"  
> }  
> ],  
> "type": "script\_exception",  
> "reason": "runtime error",  
> "script\_stack": [  
> "doc['age'].value =doc['age'].value+params.last",  
> " ^---- HERE"  
> ],  
> "script": "doc['age'].value =doc['age'].value+params.last",  
> "lang": "painless",  
> "caused\_by": {  
> "type": "null\_pointer\_exception",  
> "reason": null  
> }  
> },  
> "status": 500  
> }

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [September 26, 2018, 5:12pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/4 "2018-09-26T17:12:20Z")

</div>

I am sorry, I misunderstood the intention of your original post. `doc` can't be used for updating documents, `ctx._source` needs to be used to refer to the source document. Since you already got it working with using `ctx._source`, is there another reason for using `doc`?

---

<div class="post-metadata">

**Author:** ![Rahul\_Ranjan1](https://avatars.discourse-cdn.com/v4/letter/r/b4bc9f/32.png) [@Rahul\_Ranjan1](https://discuss.elastic.co/u/Rahul_Ranjan1)\
**Post date:** [September 26, 2018, 5:20pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/5 "2018-09-26T17:20:55Z")

</div>

Yes, Actually Elasticsearch documentation says that `doc_value` is the most efficient way to access a field value from a script, and also they say that we should always prefer `doc_value` over stored fields such as `_source`(a special stored field) because they are must faster than stored fields when it comes to update a value of a specific field.  
[Scripting doc\_values](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html#modules-scripting-doc-vals)

Thanks for your reply Jen.

---

<div class="post-metadata">

**Author:** ![jen-huang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jen-huang/32/74327_2.png) [@jen-huang](https://discuss.elastic.co/u/jen-huang)\
**Post date:** [September 26, 2018, 5:39pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/6 "2018-09-26T17:39:33Z")

</div>

I understand; this is true for _accessing_ a field via painless script. I just confirmed with the ES team that for _updating_ a field with painless script, `doc` is not available, see: [https://www.elastic.co/guide/en/elasticsearch/painless/6.4/painless-update-by-query-context.html](https://www.elastic.co/guide/en/elasticsearch/painless/6.4/painless-update-by-query-context.html)

---

<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:** [October 24, 2018, 5:39pm UTC](https://discuss.elastic.co/t/using-doc-value-returns-error-with-update-and-update-by-query-in-elasticsearch-scriptexception-runtime-error-nested-nullpointerexception-elasticsearch-6-4/149958/7 "2018-10-24T17:39:39Z")

</div>

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