# Conditional scripting during bulk update

**URL:** https://discuss.elastic.co/t/conditional-scripting-during-bulk-update/13263
**Category:** Elasticsearch
**Created:** [August 20, 2013, 12:59am UTC](https://discuss.elastic.co/t/conditional-scripting-during-bulk-update/13263 "2013-08-20T00:59:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![GivP](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/givp/32/2171_2.png) [@GivP](https://discuss.elastic.co/u/GivP)
#### Post date: [August 20, 2013, 12:59am UTC](https://discuss.elastic.co/t/conditional-scripting-during-bulk-update/13263/1 "2013-08-20T00:59:02Z")

</div>

I'm trying to have a script that will update a particular field (vcounter)  
only if the index version is "1" during a bulk request. Here's a single  
index in the bulk that will be upserted.

I only want to set the vcounter field to 1 ONLY if the index is at version  
1 - else it will be incremented:

{"update": {"\_id": "1"}}  
{"upsert": {"vcounter": 0}, "script": "if (ctx.\_version == '1') {  
ctx.\_source.vcounter = 1 } else { ctx.\_source.vcounter += 1"}  
{"update": {"\_id": "1"}}  
{"doc": {"field2": 3, "field1": "value1"}, "doc\_as\_upsert": true}

Can't figure out what the problem is. Not even sure if we can script  
against the \_version field? The above command always leaves vcounter at 0.

Any pointers would be much appreciated.

G

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Anand\_Nalya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anand_nalya/32/571_2.png) [@Anand\_Nalya](https://discuss.elastic.co/u/Anand_Nalya)
#### Post date: [October 28, 2013, 5:29am UTC](https://discuss.elastic.co/t/conditional-scripting-during-bulk-update/13263/2 "2013-10-28T05:29:14Z")

</div>

Is \_version field supported in scripts.

On Tuesday, 20 August 2013 06:29:02 UTC+5:30, GivP wrote:

> I'm trying to have a script that will update a particular field (vcounter)  
> only if the index version is "1" during a bulk request. Here's a single  
> index in the bulk that will be upserted.
> 
> I only want to set the vcounter field to 1 ONLY if the index is at version  
> 1 - else it will be incremented:
> 
> {"update": {"\_id": "1"}}  
> {"upsert": {"vcounter": 0}, "script": "if (ctx.\_version == '1') {  
> ctx.\_source.vcounter = 1 } else { ctx.\_source.vcounter += 1"}  
> {"update": {"\_id": "1"}}  
> {"doc": {"field2": 3, "field1": "value1"}, "doc\_as\_upsert": true}
> 
> Can't figure out what the problem is. Not even sure if we can script  
> against the \_version field? The above command always leaves vcounter at 0.
> 
> Any pointers would be much appreciated.
> 
> G

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:10am UTC](https://discuss.elastic.co/t/conditional-scripting-during-bulk-update/13263/3 "2017-07-06T02:10:21Z")

</div>


