# Update\_by\_query by Groovy file

**URL:** https://discuss.elastic.co/t/update-by-query-by-groovy-file/83196
**Category:** Elasticsearch
**Created:** [April 21, 2017, 11:30am UTC](https://discuss.elastic.co/t/update-by-query-by-groovy-file/83196 "2017-04-21T11:30:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Eric\_Velasco](https://avatars.discourse-cdn.com/v4/letter/e/6bbea6/32.png) [@Eric\_Velasco](https://discuss.elastic.co/u/Eric_Velasco)
#### Post date: [April 21, 2017, 11:30am UTC](https://discuss.elastic.co/t/update-by-query-by-groovy-file/83196/1 "2017-04-21T11:30:29Z")

</div>

Hi,

I am trying to update the field mileage using the **groovy file script**. All the samples I saw so far utilizes this kind of way to update. I can't seem to update it using the groovy file.

> "script" : {"inline":"ctx.\_source.mileage += 1"},

Here is my sample data that I am trying to query.

> {  
> "\_index": "carmojodev2",  
> "\_type": "inventory",  
> "\_id": " **62A646B5-A1F4-E611-8119-005056A41824**",  
> "\_version": 16,  
> "found": true,  
> "\_source": {  
> "doors": 4,  
> "series": null,  
> "discountpercent": 0,  
> "model": "TRAILBLAZER",  
> "exteriorcolor": null,  
> "totalcost": 7000,  
> "modelidentifier": "348696",  
> "retailbook": 10900,  
> "location": null,  
> "state": "LA",  
> "drivetrain": "4WD",  
> "maxdiscount": 0,  
> "vehicleclass": 2,  
> "essource": "estool",  
> "buyprogramwholesale": 8350,  
> "companyid": "25C45D4E-3130-4DDB-90E0-BF256DEA2B62",  
> "lastsynctoes\_date": "2017-03-22T00:42:52.1073969",  
> "distance": null,  
> "askingprice": 8000,  
> "description": null,  
> "bodytype": "SUV",  
> "vin": "1GNDT13S082242922",  
> "bookcomparisonvalue": 10600,  
> "iswestlakevalued": 0,  
> "highwaympg": 0,  
> "year": 2008,  
> "trim": "LT SPORT UTILITY 4D",  
> "mainexternaldocumentid": "YmlVQjBTUG5BdCtEbDdncEpQclB1cll5aDBzNGp1Szdqdmo2aWFIMVpEK2p3S3pvbmNhQTFsTlFNdzVRR2h4TFpwZ3JLc1QxQ08zOU43bUNXMzZvS3c9PQ%3d%3d",  
> "interiorcolor": null,  
> "inventoryid": "62A646B5-A1F4-E611-8119-005056A41824",  
> "advertisingprice": 10000,  
> "specialprice": 0,  
> "mileage": **57008** ,  
> "cylinders": 6,  
> "westlakeclasscode": 0,  
> "showonwebsites": "WIDGET,CARMOJO",  
> "autocheckscore": null,  
> "engine": "6-CYL, 4.2 LITER",  
> "specialpriceenddate": "2017-02-16T00:00:00",  
> "fueltype": "GASOLINE",  
> "esdbname": "dckiss12",  
> "vehicleinvoiceprice": 4,  
> "transmission": "AUTOMATIC, 4-SPD W/OVERDRIVE",  
> "stockno": "WHY WOULD YOU ADD A SPACE ",  
> "vehicleprice": 10000,  
> "transmissiontype": "AUTOMATIC",  
> "inventoryminfrontgross": null,  
> "bookvaluedefaultbystate": "NADA",  
> "specialpricestartdate": "2017-02-16T00:00:00",  
> "citympg": 0,  
> "make": "CHEVROLET",  
> "bodystyle": "SUV"  
> }  
> }

Here is my post address that I am using right now

> [https://10.209.0.40:2900/carmojodev2/inventory/\_update\_by\_query](https://10.209.0.40:2900/carmojodev2/inventory/_update_by_query)

And it contains this json body that calls the groovy script file.

> {  
> "from": 0,  
> "size": 50,  
> "script": {  
> "file": " **mileage\_add\_by\_one**",  
> "lang": "groovy",  
> "params": {}  
> },  
> "query": {  
> "constant\_score": {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "\_id": "62A646B5-A1F4-E611-8119-005056A41824"  
> }  
> },  
> {  
> "range": {  
> "mileage": {  
> "gt": 0  
> }  
> }  
> }  
> ]  
> }  
> }  
> ]  
> }  
> }  
> }  
> }  
> }

On this json body, there is a **mileage\_add\_by\_one** in the file inside the script.  
And here is a simple implementation of the groovy file. It justs add a +1 mileage for now.

> tempMileage = 0  
> if (doc.containsKey('mileage') == false || doc['mileage'].value == 0)  
> {  
> tempMileage = 0  
> }  
> else  
> {  
> tempMileage = doc['mileage'].value + 1  
> }  
> tempMileage

Here is the error that i got.  
{  
"error": {  
"root\_cause": [  
{  
"type": "script\_exception",  
"reason": "failed to run file script [mileage\_add\_by\_one] using lang [groovy]"  
}  
],  
"type": "script\_exception",  
"reason": "failed to run file script [mileage\_add\_by\_one] using lang [groovy]",  
"caused\_by": {  
"type": "missing\_property\_exception",  
"reason": "No such property: doc for class: 44b47ca9838dd911303aab40815b4ed2e6eb6043"  
}  
},  
"status": 500  
}

Any thoughts? thanks a lot!

---

<div class="post-metadata">

### Author: ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)
#### Post date: [April 22, 2017, 12:10am UTC](https://discuss.elastic.co/t/update-by-query-by-groovy-file/83196/2 "2017-04-22T00:10:24Z")

</div>

You need to use the `ctx._source` instead of `doc[]`. For example, `ctx._source.mileage` instead of `doc['mileage']`

---

<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: [May 20, 2017, 12:17am UTC](https://discuss.elastic.co/t/update-by-query-by-groovy-file/83196/3 "2017-05-20T00:17:08Z")

</div>

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