# Accessing doc value fields from painless in update

**URL:** https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771
**Category:** Elasticsearch
**Created:** [March 20, 2018, 1:28pm UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771 "2018-03-20T13:28:41Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [March 20, 2018, 1:28pm UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/1 "2018-03-20T13:28:41Z")

</div>

Hi,

I can do this:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#\_scripted\_updates](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html#_scripted_updates)  
which increments the previously stored counter field by 4.

But what if I want to exclude the counter field from the doc (and only store it in doc values to save massive amounts of space) and I would like to increase that similarly?

I couldn't make it work, because it seems elastic can fetch the actual version of the doc (and make it accessible to painless), but it can't do the same with doc values.

Is there a way to do this, or this needs to be implemented?

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [March 23, 2018, 4:56pm UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/2 "2018-03-23T16:56:26Z")

</div>

Can you please give a concrete example of a scripted update you would like to run, and the behavior you expect of that update?

---

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [March 26, 2018, 9:24am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/3 "2018-03-26T09:24:06Z")

</div>

Sure.  
I have the following mapping (it's used just for demonstation):  
{  
"\_source": {  
"excludes": ["counter"]  
},  
'dynamic': 'strict',  
'properties':  
{  
'counter': {'type':'long'},  
}  
}

And a script:  
`ctx._source.counter=doc['counter']+1;`

When I run this script on an existing doc with an integer in its counter field, it fails with:  
`RequestError(400, u'illegal_argument_exception', {u'status': 400, u'error': {u'caused_by': {u'lang': u'painless', u'script': u'counter', u'script_stack': [u"ctx._source.counter=doc['counter']+1;\n", u' ^---- HERE'], u'caused_by': {u'reason': None, u'type': u'null_pointer_exception'}, u'reason': u'runtime error', u'type': u'script_exception'}, u'root_cause': [{u'reason': u'[wdxwsRv][127.0.0.1:9300][indices:data/write/update[s]]', u'type': u'remote_transport_exception'}], u'type': u'illegal_argument_exception', u'reason': u'failed to execute script'}})`

I would expect that I can access the doc values in update scripts like I can access them in queries or I can access the source fields.  
The rationale in this is that I don't want to waste storage[1](https://github.com/elastic/elasticsearch/issues/27374#issuecomment-345180834) for storing these data in \_source, but I want to update the doc, and doc values could be used to transfer the previous values (either unmodified or modified, depending on the task).

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [March 29, 2018, 6:27am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/4 "2018-03-29T06:27:29Z")

</div>

`doc` is how doc values are accessed in scoring/search scripts. They are the columnar store used for fast lookup at search time. Update scripts (which I think is the type of script you are running) only have access to the `_source`. Their purpose is to update the source of a document _before_ being indexed. If you want to have a counter, you'll need to leave the field in `_source`.

Additionally, it is highly discouraged to use source excludes/includes. Doing so prevents other features like reindex from working.

---

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [March 29, 2018, 8:09am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/5 "2018-03-29T08:09:25Z")

</div>

Even reindex could use this feature (personally, I don't need it), you could do a scripted reindex this way.  
Storing data in \_source is a waste (memory, CPU and storage wise) in many cases and it would be nice if elastic could handle this.

How hard would it be to implement this?

---

<div class="post-metadata">

### Author: ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)
#### Post date: [March 29, 2018, 8:30am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/6 "2018-03-29T08:30:45Z")

</div>

I'm not sure exactly how difficult it would be (but my hunch is, quite difficult). It would also actually be much less performant for reindex, because every document would require N seeks (where N is the number of fields) vs accessing \_source requires just 1 seek to gather the entire document.

---

<div class="post-metadata">

### Author: ![Attila\_Nagy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/attila_nagy/32/46906_2.png) [@Attila\_Nagy](https://discuss.elastic.co/u/Attila_Nagy)
#### Post date: [March 29, 2018, 8:52am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/7 "2018-03-29T08:52:29Z")

</div>

For me, requiring some extra seeks worths the 1/3 size of the on disk representation of the database... 🙂

---

<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: [April 26, 2018, 8:52am UTC](https://discuss.elastic.co/t/accessing-doc-value-fields-from-painless-in-update/124771/8 "2018-04-26T08:52:54Z")

</div>

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